PHP5 + UTF8 multi-File Upload class

Source: Internet
Author: User

Some features are not added, such as automatic renaming and image processing. You can add them as needed.
USE:
$ Up = new upfile (ROOT_PATH. 'Data /'. date ("Ym", time (), array ('gif', 'jpg ', 'jpeg'), true );
$ Fileimg = $ up-> upload ($ _ FILES ['img ']); // returns the array of uploaded file names. $ _ FILES ['img'] indicates the uploaded file.
You can use $ up-> log to view the uploaded information.
<? Php
// ================================================ ====================
// FileName: upfile. class. php
// Summary: File Upload class
// Author: millken (lost Lincoln)
// LastModifed: 2008-6-4
// Copyright (c) 2008 millken@gmail.com
// ================================================ ====================
If (! Defined ('OK') exit (_ FILE _. 'Access Denied ');
Class upfile {
Public $ ExtensionFileFormat = array ();
Public $ returninfo = array ();
Private $ ImageFileFormat = array ('gif', 'bmp ', 'jpg', 'jpe ', 'jpeg', 'png ');
Private $ OtherFileFormat = array ('zip ', 'pdf', 'rar ', 'xls', 'Doc', 'ppt ', 'csv ');
Private $ savePath;
Private $ attachment_path = './upfiles /';
Private $ overwrite = false; # Whether to overwrite data with the same name
Private $ maxSize = 0; # maximum file size. If the value is 0, no size limit is imposed.
Private $ ext;
Private $ errno = 0;
/* Constructor
* (String) $ save path of the savePath file. The default value is $ attachment_path.
* (Array) $ extensionFileFormat: Customize the extension of the uploaded file. If not set, $ ImageFileFormat | $ OtherFileFormat
* (Bool) $ overwrite indicates whether to overwrite files of the same name
*/
Public function _ construct ($ savePath = '', $ extensionFileFormat = array (), $ overwrite = false ){
$ This-> savePath = empty ($ savePath )? $ This-> attachment_pathsavePath .'/';
$ This-> extensionFileFormat = is_array ($ extensionFileFormat )? $ ExtensionFileFormat: array ();
$ This-> overwrite = is_bool ($ overwrite )? $ Overwrite: false;
}
/* Upload function
* (Array) $ array of files to be uploaded $ _ FILES ['Attach ']
* (Number) $ maximum number of bytes of the maxSize file. The default value is 0, which does not limit the upload size.
*/
Public function upload ($ files, $ maxSize = 0 ){
$ This-> maxSize = is_numeric ($ maxSize )? $ MaxSize: 0;
If (isset ($ files) & is_array ($ files )){
If (is_array ($ files ['name']) {
Foreach ($ files as $ key => $ var ){
Foreach ($ var as $ id => $ val ){
$ Attachments [$ id] [$ key] = $ val;
}
}
} Else {
$ Attachments [] = $ files;
}
}
Self: check_file_type ($ attachments );
If (empty ($ this-> filelist )){
$ This-> log. = "the file list to be uploaded is empty. \ N ";
Return array ();
}
If (! Self: makeDirectory () |! @ Is_writable ($ this-> savePath )){
$ This-> log. = $ this-> savePath. "cannot be created or its permission is not writable. \ N ";
Return array ();
}
$ Filearray = array ();
Foreach ($ this-> filelist as $ k => $ f ){
If ($ this-> maxSize & $ f ['SIZE']> $ this-> maxSize ){
$ This-> log. = $ f ['name']. "The size exceeds the set value:". $ this-> maxSize. "\ n ";
} Elseif ($ this-> overwrite = false & file_exists ($ this-> savePath. $ f ['name']) {
$ This-> log. = $ f ['name']. "already exists in the directory:". $ this-> savePath. "\ n ";
} Else {
@ Unlink ($ this-> savePath. $ f ['name']);
If (@ move_uploaded_file ($ f ['tmp _ name'], $ this-> savePath. mb_convert_encoding ($ f ['name'], 'gbk', 'utf-8') {// if no encoding conversion is performed, Chinese characters cannot be supported.
$ This-> log. = $ f ['name']. "successfully uploaded to the directory:". $ this-> savePath. "\ n ";
$ Filearray [$ k] = $ this-> savePath. $ f ['name'];
} Else {
$ This-> log. = $ f ['name']. "Upload Failed. \ N ";
}
}
}
Return $ filearray;
}
/* Check the file type
* (Array) $ files file array
*/
Private function check_file_type ($ files ){
$ This-> filelist = array ();
Foreach ($ files as $ key => $ file ){
If ($ file ['error'] = 0 ){
$ Ext = strtolower (substr ($ file ['name'], strrpos ($ file ['name'], '.') + 1 ));
$ Str = @ file_get_contents ($ file ['tmp _ name'], FALSE, NULL, 0, 20 );
If (in_array ($ ext, array ('jpg ', 'jpeg') & substr ($ str, 0, 3 )! = "\ XFF \ xD8 \ xFF") | ($ ext = 'gif' & substr ($ str, 0, 4 )! = 'Gif8') | ($ ext = 'png '& substr ($ str, 0, 8 )! = "\ X89 \ x50 \ x4E \ x47 \ x0D \ x0A \ x1A \ x0A") | ($ ext = 'bmp '& substr ($ str, 0, 2 )! = 'Bm ') | ($ ext = 'swf' & (substr ($ str, 0, 3 )! = 'Done' | substr ($ str, 0, 3 )! = 'Fws ') | ($ ext = 'zip' & substr ($ str, 0, 4 )! = "PK \ x03 \ x04") | ($ ext = 'rar '& substr ($ str, 0, 4 )! = 'Rar! ') | ($ Ext = 'pdf' & substr ($ str, 0, 4 )! = "\ X25PDF") | ($ ext = 'chm' & substr ($ str, 0, 4 )! = 'Itsf ') | ($ ext = 'rm' & substr ($ str, 0, 4 )! = "\ X2ERMF") | ($ ext = 'exe '& substr ($ str, 0, 2 )! = "MZ") | (in_array ($ ext, array ('Doc', 'xls ', 'ppt') & substr ($ str, 0, 4 )! = "\ XD0 \ xCF \ x11 \ xE0 ")){
$ This-> log. = $ file ['name']. "The file type does not match the file content. \ N ";
} Elseif ((! Empty ($ this-> extensionFileFormat) & in_array ($ ext, $ this-> extensionFileFormat) | (empty ($ this-> extensionFileFormat) & (in_array ($ ext, $ this-> ImageFileFormat) | in_array ($ ext, $ this-> OtherFileFormat )))){
$ This-> filelist [$ key] = $ file;
} Else {
$ This-> log. = $ file ['name']. "does not match the type of the uploaded file. \ N ";
@ Unlink ($ file ['tmp _ name']);
}
}
}
}
/* Generate the upload directory
*
*/
Private function makeDirectory (){
$ DirectoryName = str_replace ("\", "/", $ this-> savePath );
$ DirNames = explode ('/', $ directoryName );
$ Total = count ($ dirNames );
$ Temp = '';
For ($ I = 0; $ I <$ total; $ I ++)
{
$ Temp. = $ dirNames [$ I]. '/';
If (! Is_dir ($ temp ))
{
$ Oldmask = @ umask (0 );
If (! @ Mkdir ($ temp, 0777) return false;
@ Umask ($ oldmask );
}
};
If (is_dir ($ this-> savePath )){
Return true;
} Else {
Return false;
};
}
}
?>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.