Php file upload code (supports batch file upload)

Source: Internet
Author: User
Tags file size file upload html form php file php file upload

This file Upload class is used to upload leaflets by default. You only need to modify $ inputname = 'Files' to your form name to conveniently Upload files in batches. $ Savename = '': Save the file name. $ alowexts = array (): Set the upload type. $ savepath ='' indicates the save path.
*/

The code is as follows: Copy code

Class upload
{
Public $ savepath;
Public $ files;
Private $ error;

Function _ construct ($ inputname = 'files', $ savepath = '', $ savename ='', $ alowexts = array (), $ maxsize = 1024000)
 {
If (! $ Alowexts) $ alowexts = explode ('|', upload_ftype );
$ File_array = array ();
$ Savepath = str_replace ('', '/', $ savepath );
$ Savename = preg_replace ('/[^ a-z0-9 _] +/I', '', $ savename );
$ This-> savepath = substr ($ savepath,-1) = '/'? $ Savepath: $ savepath. '/'; // The path name ends/.

If (! Make_dir ($ this-> savepath ))
  {
$ This-> error = 8;
$ This-> error ();
  }
// Exit ($ this-> savepath );
If (! Is_writeable ($ this-> savepath ))
  {
$ This-> error = 9;
$ This-> error ();
  }
If (sizeof ($ _ files [$ inputname] ['error'])> 10)
  {
$ This-> error = 13;
$ This-> error ();
  }
$ Max = sizeof ($ _ files [$ inputname] ['error'])-1;
// Exit ($ this-> savepath. $ savename );
Foreach ($ _ files [$ inputname] ['error'] as $ key => $ error)
  {
If ($ error = upload_err_ OK) // batch Upload
   {
$ Savename = $ savename? $ Savename: date ('ymmdms'). mt_rand (Random, 99999 );
$ Fileext = strtolower (get_fileext ($ _ files [$ inputname] ['name'] [$ key]);
$ Savename = $ savename. '.'. $ fileext;
$ Tmp_name = $ _ files [$ inputname] ['tmp _ name'] [$ key];
$ Filesize = $ _ files [$ inputname] ['size'] [$ key];
If (! In_array ($ fileext, $ alowexts ))
    {
$ This-> error = 10;
$ This-> error ();
    }
If ($ filesize> $ maxsize)
    {
$ This-> error = 11;
$ This-> error ();
    }
If (! $ This-> isuploadedfile ($ tmp_name ))
    {
$ This-> error = 12;
$ This-> error ();
    }

If (move_uploaded_file ($ tmp_name, $ this-> savepath. $ savename) | @ copy ($ tmp_name, $ this-> savepath. $ savename ))
    {
// Exit ($ this-> savepath. $ savename );
@ Chmod ($ savename, 0644 );
@ Unlink ($ tmp_name );
$ File_array [] = $ this-> savepath. $ savename;
    }
   }
Else
   {
$ This-> error = $ error;
$ This-> error ();
   }
Unset ($ savename );
  }
$ This-> files = $ file_array;
Return true;
 }

Function isuploadedfile ($ file) // remove the built-in backslash.
 {
Return (is_uploaded_file ($ file) | is_uploaded_file (str_replace ('\', '', $ file )));
 }

Function error ()
 {
$ Upload_error = array (0 => 'file uploaded successfully! ',
1 => 'The uploaded file exceeds the limit of the upload_max_filesize option in php. ini! ',
2 => 'The size of the uploaded file exceeds the value specified by the max_file_size option in the html form! ',
3 => 'only part of the file is uploaded! ',
4 => 'no file is uploaded! ',
5 => 'unknown error! ',
6 => 'The temporary folder cannot be found. ! ',
7 => 'an error occurred while writing the file to the temporary folder! ',
8 => 'attachment directory creation failed! ',
9 => 'The attachment Directory has no write permission! ',
10 => 'this type of file cannot be uploaded! ',
11 => 'the file size exceeds the administrator limit! ',
12 => 'upload file illegal! ',
13 => 'up to 10 files can be uploaded at the same time! '
);
Showmsg ($ upload_error [$ this-> error]);
 }

}

// Usage

New upload ();

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.