A simple php file Upload method

Source: Internet
Author: User
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!

  1. ******************/
  2. /*
  3. * $ Formname: Html Upload form name
  4. * $ Allowtype_array array of file types that can be uploaded
  5. * $ Allow_size: maximum size of the file to be uploaded. The default value is 1 MB.
  6. * $ Whether isRename needs to be renamed to the uploaded file
  7. */
  8. Function upload_file ($ formname, $ path, $ allowtype_array, $ allow_size = '000000', $ isRename = true ){
  9. If ($ _ FILES [$ formname] ['tmp _ name'] = ''| $ _ FILES [$ formname] ['name'] ='' | $ _ FILES [$ formname] ['size'] = 0) {
  10. Return false;
  11.  }   
  12. If (! File_exists ('../uploadfiles/'. $ path )){
  13. If (! Mkdir ('../uploadfiles/'. $ path, 0777 )){
  14. WriteErrMsg ('failed to create the directory for saving the uploaded file, please contact the administrator to check the directory authorization ');
  15.   }   
  16.  }   
  17. If ($ _ FILES [$ formname] ['size']> $ allow_size ){
  18. WriteErrMsg ('upload file is too large. Please restrict the file to '. number_format ($ allow_size/1048576, '2','. ', '').' MB ');
  19.  }   
  20. $ File_name_array = explode ('.', $ _ FILES [$ formname] ['name']);
  21. $ File_type = strtolower ($ file_name_array [count ($ file_name_array)-1]);
  22. If (! In_array ($ file_type, $ allowtype_array )){
  23. WriteErrMsg ('upload file type error. Only files with the suffix. '. implode ('. ', $ allowtype_array).' can be uploaded ');
  24.  }   
  25. $ Save_name = $ isRename? Randomname (). '.'. $ file_type: $ _ FILES [$ formname] ['name'];
  26. If (! Move_uploaded_file ($ _ FILES [$ formname] ['tmp _ name'], '../uploadfiles/'. $ path. '/'. $ save_name )){
  27. WriteErrMsg ('upload an error while uploading the file ');
  28.  }   
  29. Switch ($ _ FILES [$ formname] ['error']) {
  30. Case 0:
  31. Return $ save_name;
  32. Break;
  33. Case 1:
  34. WriteErrMsg ('The uploaded file exceeds the limit of the upload_max_filesize option in php. ini ');
  35. Break;
  36. Case 2:
  37. WriteErrMsg ('the size of the uploaded file exceeds the value specified by the MAX_FILE_SIZE option in the HTML form ');
  38. Break;
  39. Case 3:
  40. WriteErrMsg ('only part of the file is upload ');
  41. Break;
  42. Case 4:
  43. WriteErrMsg ('No file is upload ');
  44. Break;
  45. Default:
  46. WriteErrMsg ('The error message can be displayed, and the days should be mixed in head ');
  47. Break;
  48.  }   
  49. } 

 

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.