<?PHP$allowtype=Array("GIF", "png", "JPG");//set the upload type to GIF, PNG, and JPG $size= 1000000;//set the file size to 1M (1000000 bytes) $path= "./uploads";//set the path to save files after uploading//determine if the file can be successfully uploaded to the server, $_files[' myfile ' [' Error '] is 0 for upload success if($_files[' MyFile '] [' ERROR '] > 0) { Echo' Upload error: '; Switch($_files[' MyFile '] [' Error ']) { Case1: die(' Upload file size exceeds the agreed value in PHP config file: upload_max_filesize ')); Case2: die(' Upload file size exceeds the contract value in the form: Max_file_size ')); Case3: die(' files are only partially uploaded '); Case4: die(' No files uploaded '); default: die(' Last known error '); } } //determine if the uploaded file is a file type that is not allowed, through the file's suffix name $hz=Array_pop(Explode(".",$_files[' MyFile '] [' Name '])); //Determine whether a file is a file type that is allowed to be uploaded by judging the file suffix method if(!In_array($hz,$allowtype)) { die("This suffix is <b>{$hz}</b>, not a permitted file type! "); } /*You can also restrict the type of file upload by obtaining the main type and subtype in the MIME type of the uploaded file list ($maintype, $subtype) =explode ("/", $_files[' myfile ' [' type ']); if ($maintype = = "text") {//The text file cannot be uploaded by the master type limit, such as. txt. html. PHP and other files die (' problem: cannot upload text files. ‘); } */ //determine if the uploaded file is size if($_files[' MyFile '] [' Size '] >$size ) { die("More than a <b>{$size}</b> byte Size "); } //For system security, also for files with the same name will not be overwritten, after uploading the file name using the system definition $filename=Date("Ymdhis").Rand(100,999). ".".$hz; //determine whether to upload files if(Is_uploaded_file($_files[' MyFile '] [' Tmp_name '])) { if(!Move_uploaded_file($_files[' MyFile '] [' Tmp_name '],$path.‘ /‘.$filename)) { die(' Problem: The file cannot be moved to the specified directory. ‘); } }Else{ die("Problem: uploading a file {$_files[' MyFile '] [' Name ']} is not a legal document: "); } //output If file upload is successful EchoFile$upfileUploaded successfully, saved in directory {$path}, the size is {$_files[' MyFile '] [' Size ']} Bytes ";
PHP File Upload