A simple PHP file upload method

Source: Internet
Author: User
Keywords Network programming PHP tutorial
Tags corporate website error file file type file upload files function name

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;}}
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.