Php easily implements the file upload function,
This article is divided into five parts for analysis and explanation of php File Uploading. The specific content is as follows:
- File Upload variable
- Move temporary files on the server to the specified directory
- Php. ini upload Configuration
- Error Code
- Single file upload instance
1. File Upload Variables
// $ _ FILES: file Upload variable # name file name # type file type # tmp_name temporary file name # size file size # error message $ filename = $ _ FILES ["myFile"] ["name "]; $ type = $ _ FILES ["myFile"] ["type"]; $ tmp_name = $ _ FILES ["myFile"] ["tmp_name"]; $ size = $ _ FILES ["myFile"] ["size"]; $ error = $ _ FILES ["myFile"] ["error"];
2. Move temporary files on the server to the specified directory
// 1. move_uploaded_file ($ tmp_name, $ destination): Move the temporary file on the server to the specified directory # Name of the uploaded file. If the file is successfully moved, true is returned. Otherwise, falsemove_uploaded_file ($ tmp_name, "D: /". $ filename); // 2. copy ($ tmp_name, $ destination) copy ($ tmp_name, "D :/". $ filename );
3. php. ini upload Configuration
# File_uploads = On support for HTTP upload # upload_tmp_dir = "" temporary file storage directory # upload_max_filesize = 2 M maximum number of files allowed to be uploaded # max_file_uploads = 20 maximum number of files allowed for one upload # post_max_size = maximum size of data sent in 8 M post mode # max_execution_time = 1 sets the maximum execution time allowed before the script is terminated by the parser, the Unit is seconds to prevent the program from writing poorly and occupy the server resources # max_input_time = 60 maximum time allowed by the script to parse the input data, unit: seconds # max_input_nesting_level = 64 set the nested depth of input variables # max_input_vars = 1000 how many input variables are accepted # memory_limit = 128 M maximum single-thread independent memory usage
4. error Code
#0. No error occurs. The file is 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 files are uploaded. #6. The Temporary Folder cannot be found. #7. File writing failed. #8. the uploaded file is interrupted by the PHP extension.
5. Upload a single file to an instance
Form.html
<! Doctype html>
FileUpload. php
Header ("content-type: text/html; charset: UTF-8"); $ fileInfo = $ _ FILES ["myFile"]; $ filename = $ fileInfo ["name"]; $ type = $ fileInfo ["type"]; $ error = $ fileInfo ["error"]; $ size = $ fileInfo ["size"]; $ tmp_name = $ fileInfo ["tmp_name"]; $ maxSize = 2*1024*1024; // maximum allowed value $ allowExt = array ("jpeg", "jpg ", "gif"); $ flag = true; // check whether the image type is true. // determine the error code if ($ error = 0) {// determine the size of the uploaded file if ($ size> $ maxSize) {exit ("the uploaded file is too large");} // check the file type // retrieve the file File Extension $ ext = pathinfo ($ filename, PATHINFO_EXTENSION); if (! In_array ($ ext, $ allowExt) {exit ("invalid file type");} // checks whether the image type is true if ($ flag) {if (@! Getimagesize ($ tmp_name) {exit ("not a positive image type") ;}}// create a directory $ path = "D:/test/"; if (! File_exists ($ path) {mkdir ($ path, 0777, true); chmod ($ path, 0777) ;}// ensure that the file name is unique, prevents duplicate names from overwriting $ uniName = md5 (uniqid (microtime (true), true )). ". ". $ ext; $ destination = $ path. $ uniName; if (@ move_uploaded_file ($ tmp_name, $ destination) {echo "Upload successful" ;}else {echo "Upload Failed" ;}} else {switch ($ error) {case 1: case 2: case 3: case 4: case 6: case 7: case 8: echo "Upload error"; break ;}}
I hope this article will help you learn php programming.
Articles you may be interested in:
- Php File Upload extension and file type table (covering almost all files)
- PHP Image File Upload implementation code
- Php file upload code
- Php File Upload instance code
- ThinkPHP File Upload example tutorial
- PHP implements a complete example of video file upload
- How to Use PHP and HTML5 FormData to upload a fresh object without refreshing
- ThinkPHP combined with AjaxFileUploader to implement the method of uploading non-refreshing files
- Implementation of Thinkphp multi-File Upload
- A classic PHP File Upload class sharing