Table of Contents 1. File upload variable 2. Move temporary files on the server to the specified directory 3.php.ini upload related configuration 4.error error number 5. Single File upload instance 1. File Upload variable
//$_files: File upload variable #name of the name file #type of the type file #tmp_name Temporary file name #size of a size file #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 the temporary files on the server to the specified directory
// 1.move_uploaded_file ($tmp _name, $destination): Move temporary files on the server to the specified directory # after uploading the file name, the move succeeds returns True, otherwise returns false Move_uploaded_file ($tmp _name, "d:/". $filename ); // 2.copy ($tmp _name, $destination) Copy ($tmp _name, "d:/". $filename);
3.php.ini Uploading related configurations
#File_uploads=on supports HTTP uploads #upload_tmp_dir= "" Temporary File saved directory #upload_max_filesize=2m The maximum allowable upload file size #max_file_uploads=20 Maximum number of files allowed to be uploaded at one time #post_max_size=8m Post mode maximum value of data sent #Max_execution_time = 1 Sets the maximum allowable execution time before the script is terminated by the parser, in seconds, 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
<!DOCTYPE HTML><HTMLLang= "en"><Head> <MetaCharSet= "UTF-8"> <title>Document</title></Head><Body><formAction= "fileupload.php"Method= "POST"enctype= "Multipart/form-data">Please select the file you want to upload:<inputtype= "File"name= "MyFile"><BR> <inputtype= "Submit"value= "Upload file"> <!--limit the maximum value of the client upload file hidden field another row - <!--<input type= "hidden" name= "max_file_size" value= "bytes" > - <!--Accept to set the type of upload file - <!--<input type= "file" Name= "MyFile" accept= "Image/jpg,image/png,image/gif" > -</form></Body></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 allowable value$allowExt=Array("JPEG", "JPG", "gif");$flag=true;//detect whether the actual picture type//Determine the error numberif($error= = 0){ //determine the size of the uploaded file if($size>$maxSize){ Exit("Upload file too large"); } //detection file type//Check out file name extension $ext=PathInfo($filename,pathinfo_extension); if(!In_array($ext,$allowExt)){ Exit("Illegal file type"); } //detect if the picture type is true if($flag){ if(@!getimagesize($tmp _name)){ Exit("Not a positive picture type"); } } //Create a directory $path= "d:/test/"; if(!file_exists($path)){ mkdir($path, 0777,true); chmod($path, 0777); } //ensure the file name is unique to prevent overwriting with 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){ Case1: Case2: Case3: Case4: Case6: Case7: Case8:Echo"Upload Error"; Break; }}
PHP File Upload