This is a file upload function written by myself. It is often used to upload images when I create enterprise websites, I hope you will give your valuable comments! /********************************* // $ FormnameHtml upload form name * $ allowtype_array "> <LI
This is a file upload function written by myself. It is often used to upload images when I create enterprise websites, I hope you will give your valuable comments!
- ******************/
- /*
- * $ Formname: Html Upload form name
- * $ Allowtype_array array of file types that can be uploaded
- * $ Allow_size: maximum size of the file to be uploaded. The default value is 1 MB.
- * $ Whether isRename needs to be renamed to the uploaded file
- */
- Function upload_file ($ formname, $ path, $ allowtype_array, $ allow_size = '000000', $ 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 ('failed to create the directory for saving the uploaded file, please contact the administrator to check the directory authorization ');
- }
- }
- If ($ _ FILES [$ formname] ['size']> $ allow_size ){
- WriteErrMsg ('upload file is too large. Please restrict the file to '. number_format ($ allow_size/1048576, '2','. ', '').' MB ');
- }
- $ File_name_array = explode ('.', $ _ FILES [$ formname] ['name']);
- $ File_type = strtolower ($ file_name_array [count ($ file_name_array)-1]);
- If (! In_array ($ file_type, $ allowtype_array )){
- WriteErrMsg ('upload file type error. Only files with the suffix. '. implode ('. ', $ allowtype_array).' can be uploaded ');
- }
- $ Save_name = $ isRename? Randomname (). '.'. $ file_type: $ _ FILES [$ formname] ['name'];
- If (! Move_uploaded_file ($ _ FILES [$ formname] ['tmp _ name'], '../uploadfiles/'. $ path. '/'. $ save_name )){
- WriteErrMsg ('upload an error while uploading the file ');
- }
- Switch ($ _ FILES [$ formname] ['error']) {
- Case 0:
- Return $ save_name;
- Break;
- Case 1:
- WriteErrMsg ('The uploaded file exceeds the limit of the upload_max_filesize option in php. ini ');
- Break;
- 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 ('only part of the file is upload ');
- Break;
- Case 4:
- WriteErrMsg ('No file is upload ');
- Break;
- Default:
- WriteErrMsg ('The error message can be displayed, and the days should be mixed in head ');
- Break;
- }
- }