This is my own write a file upload function, mainly when doing some corporate website often use to upload pictures, uh, I wrote such a function, I hope you master your valuable comments!
/ ************ File upload function ****************** / / * * $ formname Html Upload form name * $ allowtype_array Allow upload File Type Array * $ allow_size Maximum size of files allowed to be uploaded, default is 1MB * $ isRename Need to rename uploaded files * / function upload_file ($ formname, $ path, $ allowtype_array, $ allow_size = '1048576', $ isRename = true) {if ($ _ FILES [$ formname] ['tmp_name'] == '' || $ _FILES [$ formname] ['name'] == '' || $ _FILES [$ formname] ['size' ] == 0) {return false;} if (! File_exists ('../ uploadfiles /'.$ path)) {if (! Mkdir (' ../ uploadfiles /'. $ path, 0777)) {WriteErrMsg ( 'Create an upload file to save the file directory failed, please contact the administrator to check the directory permissions');}} if ($ _ FILES [$ formname] [' size ']> $ allow_size) {WriteErrMsg (' upload file is too large, Files are limited to '.number_format ($ allow_size / 1048576,' 2 ','. ',' ').' MB ');} $ file_name_array = explode ('. ', $ _ FILES [$ form if (! in_array ($ file_type, $ allowtype_array)) {WriteErrMsg ('The upload file type is wrong, only allowed $ File_name_type: $ _ FILES [$ formname] ['name']. This file has the extension '. Implode ('. ', $ Allowtype_array) $ save_name)) {WriteErrMsg ('An error occurred while uploading the file, Please upload again ');} switch ($ _ FILES [$ formname] [' error ']) {case 0: return $ save_name; break; case 1: WriteErrMsg (' The uploaded file exceeds the upload_max_filesize option in php.ini Case 2: WriteErrMsg ('the size of the uploaded file exceeds the value specified by the MAX_FILE_SIZE option in the HTML form'); break; case 3: WriteErrMsg ('file is only partially uploaded'); break; case 4: WriteErrMsg ('No file has been uploaded'); break; default: Wri teErrMsg ('This error message appears, the day should be mixed up'); break;}}