Support multifile Upload PHP file upload code _ php Tutorial-PHP Tutorial

Source: Internet
Author: User
Tags php file upload upload php
Supports uploading php files to multiple files. Below is a file image upload class, which calls the upfile method ($ path ., $ format, $ maxsize0, $ over0): constructor parameter (save path, upload format, upload maximum word. Below is a file image upload class, call method upfile ($ path = ". /", $ format =" ", $ maxsize = 0, $ over = 0): constructor parameter (save path, upload format, maximum number of bytes for upload, whether to overwrite FILES of the same name). If you want to upload multiple FILES, you can use foreach ($ _ FILES as $ key => $ val) then $ filear [] = $ f-> upload ($ key); // returns the array of uploaded file names.

Below is a file image upload class, calling method upfile ($ path = ". /", $ format =" ", $ maxsize = 0, $ over = 0): constructor parameter (save path, upload format, maximum number of bytes for upload, whether to overwrite files of the same name). If you want to upload multiple files, you can use foreach ($ _ files as $ key => $ val) then $ filear [] = $ f-> upload ($ key); // returns the array of uploaded file names.

*/

Class upfile {
// Upload file information
Var $ filename;
// Save the name
Var $ savename;
// Save path
Var $ savepath;
// The file format is limited. if it is null, the format is not limited.
Var $ format = "";
// Overwrite mode
Var $ overwrite = 0;
/* $ Overwrite = 0 does not overwrite files of the same name
* $ Overwrite = 1 overwrites the file with the same name
*/
// Maximum file bytes
Var $ maxsize = 210000000;
// File extension
Var $ ext;

/* Constructor
* $ Path: save path
* $ Format file format (separated by commas)
* $ Maxsize: maximum file size. 0 is the default value.
* $ Over override parameters
*/
Function upfile ($ path = "./", $ format = "", $ maxsize = 0, $ over = 0 ){
If (! File_exists ($ path )){
$ This-> halt ("The specified directory [". $ path. "] does not exist. ");
}

If (! Is_writable ($ path )){
$ This-> halt ("The specified directory [". $ path. "] cannot be written. ");
}
$ Path = str_replace ("", "/", $ path );
$ This-> savepath = substr ($ path,-1) = "/"? $ Path: $ path. "/"; // save path

$ This-> overwrite = $ over; // whether to overwrite the file with the same name
$ This-> maxsize =! $ Maxsize? $ This-> maxsize: $ maxsize; // maximum file size
$ This-> format = $ format;
}

/*
* Function: detects and organizes files.
* $ Form file domain name
* $ Filename: name of the file to be uploaded. The name is automatically generated by the system when it is null or multiple files are uploaded.
* $ Filename = 1. when multiple files with the same file domain name are uploaded, the file is saved as the original file name.
*/
Function upload ($ form, $ filename = ""){
If (! Isset ($ _ files [$ form]) {
$ This-> halt ("The specified file domain name does not exist. ");
} Else {
$ Filear =$ _ files [$ form];
}

If (is_array ($ filear ["name"]) {// upload multiple files with the same file domain name
$ Outfile = array (); // array of returned file names
For ($ I = 0; $ I <count ($ filear ["name"]); $ I ++ ){
$ Ar ["name"] = $ filear ["name"] [$ I];
$ Ar ["tmp_name"] = $ filear ["tmp_name"] [$ I];
$ Ar ["size"] = $ filear ["size"] [$ I];
$ Ar ["error"] = $ filear ["error"] [$ I];

$ This-> getext ($ ar ["name"]); // get the extension
$ This-> set_savename ($ filename = 1? $ Ar ["name"]: ""); // sets the file name to be saved.
$ Outfile [] = $ this-> copyfile ($ ar );
}
Return $ outfile;
} Else {// upload a single file
$ This-> getext ($ filear ["name"]); // get the extension
$ This-> set_savename ($ filename); // you can specify the file name to save.
Return $ this-> copyfile ($ filear );
}
Return false;
}

/*
* Function: detects and copies uploaded files.
* $ Filear uploads a file array
*/
Function copyfile ($ filear ){

If ($ filear ["size"]> $ this-> maxsize ){
$ This-> halt ("Upload file". $ filear ["name"]. "The size exceeds the system limit [". $ this-> maxsize. "byte] and cannot be uploaded. ");
}

If (! $ This-> overwrite & file_exists ($ this-> savename )){
$ This-> halt ($ this-> savename. "the file name already exists. ");
}

If (! $ This-> chkext ()){
$ This-> halt ($ this-> ext. "file format cannot be uploaded. ");
}

If (! Copy ($ filear ["tmp_name"], $ this-> savepath. $ this-> savename )){
$ Errors = array (0 => "file uploaded successfully ",
1 => "the uploaded file exceeds the limit of the upload_max_filesize option in php Tutorial. 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. ");
$ This-> halt ($ errors [$ filear ["error"]);
} Else {
@ Unlink ($ filear ["tmp_name"]); // delete a temporary file
Return $ this-> savename; // return the Upload file name
}
}

/*
* Function: get the file extension.
* $ Filename indicates the file name.
*/
Function getext ($ filename ){
If ($ filename = "") return;

$ Ext = explode (".", $ filename );

$ This-> ext = $ ext [count ($ ext)-1];
}

/*
* Function: checks whether the file type is allowed.
*/
Function chkext (){
If ($ this-> format = "" | in_array (strtolower ($ this-> ext), explode (",", strtolower ($ this-> format) return true;
Else return false;
}
/*
* Function: Set the file storage name.
* $ Savename: Save name. if it is null, the system automatically generates a random file name.
*/
Function set_savename ($ savename = ""){
If ($ savename = "") {// if no file name is set, a random file name is generated.
Srand (double) microtime () * 1000000 );
$ Rnd = revert (100,999 );
$ Name = date ('u') + $ rnd;
$ Name = $ name. ".". $ this-> ext;
} Else {
$ Name = $ savename;
}
$ This-> savename = $ name;
}

/*
* Function: Error message
* $ Msg indicates the output information.
*/
Function halt ($ msg ){
Echo"Note:". $ Msg;
Exit;
}

/*
*
* It is mainly used to delete uploaded files and does not return
* Parameter $ file: file path
*/

Function delete_file ($ file)
{
If (file_exists ($ file ))
{
$ Delete = chmod ($ file, 0777 );
$ Delete = unlink ($ file );
If (file_exists ($ file ))
{
$ Filesys = eregi_replace ("/", "", $ file );
$ Delete = system ("del $ filesys ");
Clearstatcache ();
If (file_exists ($ file ))
{
$ Delete = chmod ($ file, 0777 );
$ Delete = unlink ($ file );
$ Delete = system ("del $ filesys ");
}
}
Clearstatcache ();
}
}


}

/*
Usage

* File Upload class
Upfile ($ path = ". /", $ format =" ", $ maxsize = 0, $ over = 0): constructor parameter (save path, upload format, maximum number of uploaded bytes, whether to overwrite files of the same name)



* Instance:



// Upload the leaflet file
If (isset ($ _ files ["files"])
{
$ Filear = array ();
$ Filear = $ f-> upload ("files"); // return the uploaded file name
Echo $ filear;

}



// Upload multiple domain name files of different files

If (isset ($ _ files ){
Foreach ($ _ files as $ key => $ val)
$ Filear [] = $ f-> upload ($ key); // returns the array of uploaded file names
}


*/

Upfile ($ path = ". /", $ format =" ", $ maxsize = 0, $ over = 0): constructor parameter (save path, upload format, upload maximum word...

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.