php upload files and upload files for simple authentication (error message format (security equipment), size, whether http upload)

<body> 
? < PHP
 / * * 
* validation error 
* If there is error, it returns an error, if yes, it returns null 
* / 
function the Check ( $ File ) {
     // 1: Verify wrong 
    IF ( $ File [ 'error'] = 0! ) {
         Switch ( $ file [ 'error' ]) {
             Case . 1:
                 return 'php.ini file size exceeds the maximum allowed, the maximum value is:'. the ini_get ( 'the upload_max_filesize' ) ;
             Case 2:
                 return 'file size exceeds the maximum permissible form' ;
             Case . 3:
                 return'Only part of the file to upload' ;
             Case 4:
                 return 'no file upload' ;
             Case 6:
                 return 'can not find the temporary file' ;
             Case 7:
                 return 'file write failed' ;
             default :
                 return 'unknown error' ; 
        } 
    } 
    // 2. verify the form 
   // Step 1: create finfo resource
$ info = finfo_open (FILEINFO_MIME_TYPE);
// Step two: finfo resource files and compare the
$ MIME = finfo_file ( $ info , $ file [ 'tmp_name']);
   // third step, comparing the legality
$ the allow = Array ( 'Image / JPEG', 'Image / PNG', 'Image / GIF'); // allow categories IF ! ( The in_array ( $ MIME , $ the allow )) { return 'only upload'. The implode ( ',', $ the allow ) 'format' ; } // 3, verify the size of $ size = 123456789 ; IF ( $ File [ 'size']> $ size ) { return 'file size can not exceed'. number_format ( $ size /1024,1).'K ' ; } //4. Verify is http upload IF (! Is_uploaded_file ( $ File [ 'tmp_name' ])) return 'is not a HTTP POST file upload <br>' ; return null ; // no error } // form submission IF (! Empty ( $ _POST )) { // upload error during error is displayed IF ( $ error = Check ( $ _FILES [ 'face' ])) { echo $ error ; } the else { // file upload, upload files to save to the day of the folder $ FolderName = DATE( 'Ym of-D'); // folder name $ FolderPath = "./ uploads / { $ FolderName }"; // folder path IF (! Is_dir ( $ FolderPath )) mkdir ( $ FolderPath ); $ filename = of uniqid ( '', to true .) The strrchr ( $ _FILES [ 'face'] [ 'name'], ''.); // file name $ filepath = " $ FolderPath / $ filename "; // file path IF ( move_uploaded_file ( $ _FILES['face']['tmp_name'],$filepath)) echo "上传成功,路径是:{$foldername}/{$filename}"; else echo '上传失败<br>'; } } ?> <form method="post" action="" enctype='multipart/form-data'> <input type="file" name="face"> <input type="submit" name="button" value="上传"> </form> </body>

operation result

 

 

Prevention and treatment need to open the file camouflage fileinfo.dll extension in php.ini, open function after fileinfo extension, you can use the finfo_ *.

Guess you like

Origin www.cnblogs.com/ruoruchujian/p/11120761.html