Php file Upload class and explanation (1/2)

Source: Internet
Author: User
Tags datetime file upload http post mkdir php file upload php tutorial

The upload_tmp_dir parameter in the php Tutorial configuration is compared. If the file is under this directory, move_uploaded_file will be moved. And the comparison is case sensitive, and/is different under windows. When parsing the php configuration file, a realpath function is called, that is, before move_uploaded_file,


For $ file ['tmp _ name'] = realpath ($ file ['tmp _ name']); realpath.


In another case, you should note that if move_uploaded_file is configured as an inaccessible path, no matter how you handle it, move_uploaded_file will not be able to successfully move the file.


When uploading a file, you can use the move_uploaded_file function to move the file, but copy or rename is acceptable.


I have been confused. In this document, the description is vague, that is, the move_uploaded_file function, which adds a step-by-step check to check whether the file has been uploaded through http post,


Next I found a file Upload class on the Internet.

** File upload class **/
 
 

Class upload_file
 {
/** Declaration **/
Var $ upfile_type, $ upfile_size, $ upfile_name, $ upfile;
Var $ d_alt, $ extention_list, $ tmp, $ arri;
Var $ datetime, $ date;
Var $ filestr, $ size, $ ext, $ check;
Var $ flash_directory, $ extention, $ file_path, $ base_directory;
Var $ url; // The Jump path after the file is uploaded successfully;
 
Function upload_file ()
  {
/** Constructor **/
$ This-> set_url ("index. php"); // jump path after successful initialization Upload;
$ This-> set_extention (); // initialize the extension list;
$ This-> set_size (50); // initialize the size limit of kb for the uploaded file;
$ This-> set_date (); // you can specify the directory name;
$ This-> set_datetime (); // you can specify the file name prefix;
$ This-> set_base_directory ("attachmentfile"); // initialize the file upload root directory name, which can be modified !;
  }
 
/** File type **/
Function set_file_type ($ upfile_type)
  {
$ This-> upfile_type = $ upfile_type; // Obtain the file type;
  }
 
/** Get the file name **/
Function set_file_name ($ upfile_name)
  {
$ This-> upfile_name = $ upfile_name; // Obtain the file name;
  }
 
/** Get FILE **/
Function set_upfile ($ upfile)
  {
$ This-> upfile = $ upfile; // Obtain the temporary file name stored on the server;
  }
   
/** Get the file size **/
Function set_file_size ($ upfile_size)
  {
$ This-> upfile_size = $ upfile_size; // Obtain the file size;
  }
 
/** Set the jump path after the file is uploaded successfully **/
Function set_url ($ url)
  {
$ This-> url = $ url; // sets the redirect path after the file is uploaded successfully;
  }
 
/** Get the file extension **/
Function get_extention ()
  {
$ This-> extention = preg_replace ('/. *. (. * [^.]. *) */iu ', '1', $ this-> upfile_name); // Obtain the file extension;
  }
     
/** Set the file name **/
Function set_datetime ()
  {
$ This-> datetime = date ("ymdhis"); // Generate the file name by time;
  }
 
/** Set the directory name **/
Function set_date ()
  {
$ This-> date = date ("y-m-d"); // Generate a directory name by date;
  }
 
/** Initialize the file type that can be uploaded **/
Function set_extention ()
  {
$ This-> extention_list = "doc | xls | ppt | avi | txt | gif | jpg | jpeg | bmp | png"; // The default extended name that can be uploaded;
  } 
 
/** Set the maximum Upload kb limit **/
Function set_size ($ size)
  {
$ This-> size = $ size; // you can specify the maximum file size that can be uploaded;
  }
 
/** Initialize the root directory of the file storage **/
Function set_base_directory ($ directory)
  {
$ This-> base_directory = $ directory; // Generate the root directory of the file store;
  }
 
/** Initialize the file storage subdirectory **/
Function set_flash_directory ()
  {
$ This-> flash_directory = $ this-> base_directory. "/". $ this-> date; // Generate a file storage subdirectory;
  }
 
/** Handle errors **/
Function showerror ($ errstr = "unknown error! "){
Echo "<script language = webpage effect> alert ('$ errstr'); location = 'javascript: history. go (-1); '; </script> ";
Exit ();
  }
 
/** Jump **/
Function go_to ($ str, $ url)
  {
Echo "<script language = 'javascript '> alert (' $ str'); location = '$ url'; </script> ";
Exit ();
  }

/** Create a file storage directory if the root directory is not created **/
Function mk_base_dir ()
  {
If (! File_exists ($ this-> base_directory) {// check whether the root directory exists;
@ Mkdir ($ this-> base_directory, 0777); // this parameter is created if it does not exist;
   }
  }

/** Create a file storage directory if the subdirectory is not created **/
Function mk_dir ()
  {
If (! File_exists ($ this-> flash_directory) {// checks whether the subdirectory exists;
@ Mkdir ($ this-> flash_directory, 0777); // it is created if it does not exist;
   }
  } 
 
/** Obtain the types of files that can be uploaded after decomposition in the form of arrays **/
Function get_compare_extention ()
  {
$ This-> ext = explode ("|", $ this-> extention_list); // use "|" to break down the default extension;
  }
 
/** Check whether the extension is illegal **/
Function check_extention ()
  {
For ($ I = 0; each ($ this-> ext); $ I ++) // traverses the array;
   {
If ($ this-> ext [$ I] = strtolower ($ this-> extention) // compare whether the file extension is consistent with the default allowed extension;
    {
$ This-> check = true; // mark if the match exists;
Break;
    }
   }
If (! $ This-> check) {$ this-> showerror ("The correct extension must be". $ this-> extention_list. "one of them! ");}
// Warning if it does not match
  }
 
/** Check whether the file size exceeds the standard **/
Function check_size ()
  {
If ($ this-> upfile_size> round ($ this-> size * 1024) // whether the file size exceeds the default size;
   {
$ This-> showerror ("The attachment to be uploaded cannot exceed". $ this-> size. "kb"); // warning if the attachment is exceeded;
   }
  }

/** Complete file access path **/
Function set_file_path ()
  {
$ This-> file_path = $ this-> flash_directory. "/". $ this-> datetime. ".". $ this-> extention; // Generate the complete file access path;
  }
 
/** Upload a file **/
Function copy_file ()
  {
If (copy ($ this-> upfile, $ this-> file_path) {// upload a file;
Print $ this-> go_to ("The file has been successfully uploaded! ", $ This-> url); // Upload successful;
} Else {
Print $ this-> showerror ("unexpected error, please try again! "); // Upload failed;
   }
  }
 
/** Save **/
Function save ()
  {
$ This-> set_flash_directory (); // initialize the file upload subdirectory name;
$ This-> get_extention (); // get the file extension;
$ This-> get_compare_extention (); // use "|" to break down the default extension;
$ This-> check_extention (); // checks whether the file extension is invalid;
$ This-> check_size (); // check whether the file size exceeds the upper limit;
$ This-> mk_base_dir (); // create if the root directory does not exist;
$ This-> mk_dir (); // it is created if the subdirectory does not exist;
$ This-> set_file_path (); // The Complete access path of the generated file;
$ This-> copy_file (); // upload a file;
  }
 
 }

Homepage 1 2 Last page

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.