PHP File Upload principles (source code)
PHP File Upload principles (source code)
1. File Upload Principle
Upload the client files to the server, and then upload the temporary files from the server to the specified directory.
2. Client Configuration
- Submit Form
- Form sending method is post
- Add enctype = "multipart/form-data"
3. Server Configuration
- File_uploads = On, supports HTTP upload
- Uoload_tmp_dir =, temporary file storage directory
- Upload_max_filesize = 2 M, maximum file size allowed to be uploaded
- Max_file_uploads = 20, maximum number of files that can be uploaded at a time
- Post_max_size = 8 M, maximum value of data sent in post Mode
- Max_execution_time =-1, sets the maximum execution time allowed before the script is terminated by the parser, in seconds, to prevent programs from occupying server resources due to poor write performance. -1 represents infinity
- Max_input_time = 60. maximum time allowed for parsing input data by script, in seconds
- Max_input_nesting_level = 64, set the nested depth of input variables
- Max_input_vars _ = 1000, the number of input variables that are accepted (the restriction is applied to $ _ GET, $ _ POST, and $ _ COOKIE superglobal variables, which will lead to the generation of E_WARNING, more input variables will be truncated from the request.
- Memory_limit = 128 M, maximum independent memory usage for a single thread. That is, a web Request defines the maximum memory usage of the thread.
4. Error Message description
- UPLOAD_ERR_ OK: The value is 0. No error occurs. The file is uploaded successfully.
- UPLOAD_ERR_INI_SIZE: The value is 1. the uploaded file exceeds the limit of the upload_max_filesize option in php. ini.
- UPLOAD_ERR_FORM_SIZE: The value is 2. The size of the uploaded file exceeds the value specified by the MAX_FILE_SIZE option in the HTML form.
- UPLOAD_ERR_PARTIAL: The value is 3, and only part of the file is uploaded.
- UPLOAD_ERR_NO_FILE: The value is 4. No file is uploaded.
- UPLOAD_ERR_NO_TMP_DIR: The value is 6 and the Temporary Folder cannot be found.
- UPLOAD_ERR_CANT_WRITE: The value is 7, and file writing fails.
- UPLOAD_ERR_EXTENSION: The value is 8. the uploaded file is interrupted by the PHP extension.
5. Client restrictions
6. Restrictions on the client, users can modify the code on the web page and upload the code, so there is no practical significance. Restrictions should be imposed on the server side
- Restrict the size of uploaded files
- Restrict the types of uploaded files
- Check whether the image type is real
- Checks whether http post is used for upload.
7. The complete code is as follows:
Upload. php
<! DOCTYPE html>
<Html>
<Head>
<Title> upload a file to www.bkjia.com </title>
<Meta charset = "UTF-8">
</Head>
<Body>
<Form action = "test. php" method = "post" enctype = "multipart/form-data">
Select the file to upload:
<Input type = "file" name = "myFile"/> <br/>
<Input type = "submit" value = "Upload File"/>
</Form>
</Body>
</Html>
Test. php
<? Php
Header ('content-type: text/html; charset = UTF-8 ');
Include_once 'upload. func. php ';
// Five parameters of the upload encapsulation function: $ fileInfo, $ maxsize, $ uploadPath, $ allowExt, $ flag
$ FileInfo = $ _ FILES ['myfile'];
// Print_r ($ _ FILES );
// Exit;
// $ Filename = $ _ FILES ['myfile'] ['name'];
// $ Type = $ _ FILES ['myfile'] ['type'];
// $ Tmp_name = $ _ FILES ['myfile'] ['tmp _ name'];
// $ Size = $ _ FILES ['myfile'] ['SIZE'];
// $ Error = $ _ FILES ['myfile'] ['error'];
$ Maxsize = 2097152;
$ UploadPath = 'uploads ';
$ AllowExt = array ('jpeg ', 'jpg', 'png ', 'gif ');
$ Flag = true;
$ NewName = uploadFile ($ fileInfo, $ maxsize, $ uploadPath, $ allowExt, $ flag );
Echo "Upload succeeded, path:". $ newName;
?>
Upload. func. php
<? Php
Function uploadFile ($ fileInfo, $ maxsize, $ uploadPath, $ allowExt, $ flag ){
// Identify the error code
If ($ fileInfo ['error']> 0 ){
Switch ($ fileInfo ['error']) {
Case '1 ':
$ Mes = "the uploaded file exceeds the value of the upload_max_filesize option in the PHP configuration file ";
Break;
Case '2 ':
$ Mes = "the size of the form MAX_FILE_SIZE is exceeded ";
Break;
Case '3 ':
$ Mes = "partially uploaded files ";
Break;
Case '4 ':
$ Mes = "No File Upload selected ";
Break;
Case '6 ':
$ Mes = "no temporary directory found ";
Break;
Case '7 ':
Case '8 ':
$ Mes = "system error ";
Break;
}
Exit ($ mes );
}
$ Ext = pathinfo ($ fileInfo ['name'], PATHINFO_EXTENSION );
// Check the type of the uploaded file
If (! @ In_array ($ ext, $ allowExt )){
Exit ('invalid file type ');
}
// Check whether the size of the uploaded file complies with the specifications
// $ Maxsize = 2097152; // The default value is 2 M.
If ($ fileInfo ['SIZE']> $ maxsize ){
Exit ('upload file exceeds '. ($ maxsize/1024/1024). 'M ');
}
// Check whether the image is of the actual image type. Set flag to false if no detection is required.
// $ Flag = true;
If ($ flag ){
If (! @ Getimagesize ($ fileInfo ['tmp _ name']) {
Exit ('non-Real Image type ');
}
}
// Check whether the file is uploaded through HTTP POST
If (! @ Is_uploaded_file ($ fileInfo ['tmp _ name']) {
Exit ('file not uploaded through http post ');
}
// Storage directory
If (! @ File_exists ($ uploadPath )){
Mkdir ($ uploadPath, 0777, true );
Chmod ($ uploadPath, 0777 );
}
$ UniName = MD5 (uniqid (microtime (true), true). '.'. $ ext;
$ Destination = $ uploadPath. '/'. $ uniName;
If (! @ Move_uploaded_file ($ fileInfo ['tmp _ name'], $ destination )){
Exit ('file upload failed ');
}
// Echo "File Uploaded successfully ";
// Return array {
// 'Newname' => $ destination;
// 'SIZE' => $ fileInfo ['SIZE'];
// 'Type' => $ fileInfo ['type']
//};
Return $ destination;
}
?>