PHP Generic File Upload class

Source: Internet
Author: User
Tags arrays file upload
Because the project often encountered file upload, so encapsulates a common file upload class, the role of supporting the group file upload, and different upload fields can upload different file types, different file types can limit the size of the file may also not be the same. For example: Users can upload an exhibit and upload a thumbnail for the exhibit, the thumbnail file limit type may be jpg,gif,png, and the exhibit file limit type may be mov,avi,mpeg, and the picture size may be limited to 100KB, The audio video size may be limited to 2MB. The class code is as follows:











/* File Upload class */
class Upload
{
Public   $InputName ; // file Upload domain control name

/* *
* File types allowed to upload
* Form Array (' image/jpeg ', ' ' image/png ', ' ' image/gif '), or arrays containing such arrays (corresponding to each upload domain control)
*/
Public   $FileType ;

/* *
* Maximum upload file size (in bytes)
* Form Array (' image ' => $size, ' audio ' => $size) (indicates the upload size for each application file type) Or an array containing such an array (corresponding to each uploaded domain control) or a value (indicating that all uploaded files are limited to this size)
*/
Public   $FileMaxSize ;

Public   $FileSavePath ; // File Save path (can be in array form, representing different uploaded domains uploading files to different paths)
     Public   $FileSaveName ; // File Save name (does not include suffix name) (can be in the form of an array, representing different uploaded fields to save the different names of uploaded files)
     Public   $NoteFileFalse ;  // file Error hint
     Public   $NoteFileType ; // file type does not match the prompt
     Public   $NoteFileSize ; // file size exceeded hint

/* upload file and return filename information (including suffix name) */
Public   function UploadFile ()
{
$this -> checkfile (); // Inspection Documents
         $file   =   $_files [ $this -> InputName];
$file _number   =   Count ( $file [ '' name '' ]); // number of files to upload
         $file _save_full_name   =   Array ();  // File Save name (contains suffix name)

for  ( $i   =   0 ;  $i   <   $file _number ;  $i ++ )
{
$name   =   $file [ '' name '' ][ $i ];

if  ( ! Empty ( $name ))  // file is not empty
            {
/* determine file save path */
if  ( Is_array ( $this -> Filesavepath))
{
$file _save_path   =   $this -> filesavepath[ $i ];
}
Else
{
$file _save_path   =   $this -> Filesavepath;
}

/* determine file Save name (does not include suffix name) */
if  ( Is_array ( $this -> filesavename))
{
$file _save_name   =   $this -> filesavename[ $i ];
}
Else
{
$file _save_name   =   $this -> Filesavename;
}

/* Start Saving */
$this -> Createpath ( $file _save_path ); // Create a path if the path does not exist
                 if  ( ! Move_uploaded_file ( $file [ " Tmp_name " ][ $i ] ,   $file _save_path   .   $file _save_name   .   $this -> Getsuffix ( $file [ '' name '' ][ $i ])))
{
die ( $name   .   '' File upload failed '' );
}
Else
{
$file _save_full_name []  =   $file _save_name   .   $this -> Getsuffix ( $file [ '' name '' ][ $i ]);
}
}
Else
{
$file _save_full_name []  =   NULL ;
}
}

unlink ( $file );

/* If there is only one file, a single filename is returned */
if  ( Count ( $file _save_full_name )  ==   1 )
{
$file _save_full_name   =   $file _save_full_name [ 0 ];
}

return   $file _save_full_name ;
}

/* Inspection Documents */
Private   function Checkfile ()
{
$file   =   $_files [ $this -> InputName];
$file _number   =   Count ( $file [ '' name '' ]); // number of files to upload

for  ( $i   =   0 ;  $i   <   $file _number ;  $i ++ )
{
if  ( ! Empty ( $file [ '' name '' ][ $i ]))  // file is not empty
{
$name    =   $file [ '' name '' ][ $i ];
$type    =   $file [ '' type '' ][ $i ];
$size    =   $file [ '' size '' ][ $i ];
$error   =   $file [ '' Error '' ][ $i ];

/* determine the list of allowed file types to upload */
if  ( Is_array ( $this -> FileType [ 0 ]))
{
$file _type   =   $this -> FileType [ $i ];
}
Else
{
$file _type   =   $this -> FileType ;
}

/* determine the maximum upload file size */
if  ( Is_array ( $this -> Filemaxsize))
{
$file _max_size_key   =   Explode ( '' / '' ,   $type );
$file _max_size_key   =   $file _max_size_key [ 0 ];
if  ( Is_array ( $this -> filemaxsize[ 0 ]))
{
$file _max_size   =   $this -> filemaxsize[ $i ][ $file _max_size_key ];
}
Else
{
$file _max_size   =   $this -> filemaxsize[ $file _max_size_key ];
}
}
Else
{
$file _max_size   =   $this -> Filemaxsize;
}

/* File Error */
if  ( $error   >   0 )
{
die ( $name   . $this->notefilefalse );
}

/* file type does not match */
if  ( ! In_array ( $type ,   $file _type ))
{
die ( $name   . $this-> Notefiletype );
}

/* file size exceeds maximum upload file size */
if  ( ! Is_null ( $file _max_size )  &&   $size   >   $file _max_size )
{
die ( $name   . $this-> notefilesize );
}
}
}
}

/* get file suffix name */
Private   function Getsuffix ( $fileName )
{
return   substr ( $fileName ,   Strrpos ( $fileName ,   " . " ));
}

/* Create a path if the path does not exist */
Private   function Createpath ( $filePath )
{
if  ( ! file_exists ( $filePath ))
{
mkdir ( $filePath );
}
}
}





How to use:Then we illustrate the invocation method of the class using the examples given at the beginning of this article (hehe, the call is very convenient):


$upload _obj = new Upload (); File Upload Object


$upload _obj-&gt;inputname = ' upload_test '; File Upload domain control name


$upload _obj-&gt;filetype = Array (' Image/jpeg ', ' image/png '), Array (' Audio/mpeg ', ' Video/x-msvideo ')); File types allowed to upload


$upload _obj-&gt;filemaxsize = Array (' image ' =&gt; * 1024, ' audio ' =&gt; 2 * 1024 * 1024, ' video ' =&gt; 2 * 1024 * 1024);


$upload _obj-&gt;filesavepath = Array (' upload/files/s/', ' upload/files/z/');


$upload _obj-&gt;filesavename = time ();


$upload _obj-&gt;notefilefalse = ' file error ';


$upload _obj-&gt;notefiletype = ' file type mismatch ';


$upload _obj-&gt;notefilesize = ' file size exceeds ';


$file _save_full_name = $upload _obj-&gt;uploadfile (); Upload and get the full name of the file (base name plus extension) (in the case of multiple files as an array) (full name for storing information in the database)





Summary:This can easily achieve a number of file uploads, in fact, in the final analysis of the PHP group file upload, you should pay attention to the name of the control is not forget to add [], the advantage is to encounter multiple file uploads do not have to loop in the call layer or a processing upload, our application is also easy.





Related Article

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.