PHP easy to implement file upload function,
This article is divided into five parts for the PHP upload files for analysis and explanation, the details are as follows
- File Upload variables
- Move temporary files on the server to the specified directory
- PHP.ini Uploading related configurations
- Error number
- Single File Upload instance
1. File Upload variables
2. Move the temporary files on the server to the specified directory
3.php.ini Uploading related configurations
# File_uploads=on support HTTP Upload # upload_tmp_dir= "" Temporary File saved directory # upload_max_filesize=2m allow upload of file maximum # max_file_uploads=20 allow one upload Maximum number of files # post_max_size=8m post send data max # max_execution_time = 1 Sets the maximum allowable execution time, in seconds, before the script is terminated by the parser, preventing the program from writing poorly and taking up server resources # Max_ input_time = 60 script resolves the maximum time allowed for input data, in seconds # Max_input_nesting_level = 64 Sets the nesting depth of the input variable # max_input_vars = 1000 How many input variables are accepted # memory _limit = 128M Maximum single-thread independent memory usage
4.error Error number
# 0, no error occurred, file upload succeeded. # 1, the uploaded file exceeds the value of the Upload_max_filesize option limit 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, the file is only partially uploaded. # 4, no files were uploaded. # 6, unable to find the Temp folder. # 7, File write failed. # 8, uploaded file was interrupted by PHP extension program
5. Single File Upload instance
Form.html
Document
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 allowable value $allowext=array ("JPEG", "JPG", "gif"), $flag = true;//detection is true picture type// Determine the error number if ($error = = 0) {//Determine the size of the upload file if ($size > $maxSize) {exit ("Upload file too Large");} Detection file type//remove file extension $ext = PathInfo ($filename, pathinfo_extension); if (!in_array ($ext, $allowExt)) {exit ("illegal file Type"),}//Detect if the true picture type if ($flag) {if (@!getimagesize ($tmp _name)) {exit (" Not a positive image type "); }}//create directory $path = "d:/test/"; if (!file_exists ($path)) {mkdir ($path, 0777,true); chmod ($path, 0777);}//Make sure the file name is unique and prevent overwriting of duplicate names $uniName = MD5 (Uniqid ( Microtime (True), true)). ".". $ext; $destination = $path. $uniName; if (@move_uploaded_file ($tmp _name, $destination)) {echo "Upload succeeded";} 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 suffix name and file type comparison table (almost all files covered)
- PHP image File Upload implementation code
- PHP File Upload Class code
- PHP File Upload Instance code
- thinkphp File Upload Example Tutorial
- PHP implementation of video file upload complete instance
- Use PHP and HTML5 formdata for no-refresh file upload Tutorial
- Thinkphp combining Ajaxfileuploader to realize the method of no-refresh file uploading
- thinkphp Multi-File Upload implementation method
- A classic PHP file upload class to share
http://www.bkjia.com/PHPjc/1106130.html www.bkjia.com true http://www.bkjia.com/PHPjc/1106130.html techarticle PHP Easy to implement the file upload function, this article is divided into five parts for the PHP upload files for analysis, the specific contents of the following file upload variables to move the temporary files on the server to refer to ...