Encapsulation of PHP Single file upload function

Source: Internet
Author: User
  1. Encapsulation of single-file upload functions
  2. File Upload principle: To upload the client's files to the server side, and then move the server-side temporary files to the specified directory.
  3. The direction of the file: client-to-server (temporary file)--The specified directory, when the file enters the server it is a temporary file, the operation is to use the name of the temporary file tmp_name.
  4. It is not safe to set the limit (file type and size) of the upload file on the client, because the customer can modify the limit by the source code, so set the limit here on the server side.
  5. Set the encoding to UTF-8 to avoid Chinese garbled characters
  6. Header (' Content-type:text/html;charset=utf-8 ');
  7. Receive information about uploading files via $_files
  8. $fileInfo = $_files[' myFile ');
  9. function UploadFile ($fileInfo, $uploadPath = ' uploads ', $flag =true, $allowExt =array (' jpeg ', ' jpg ', ' png ', ' gif '), $ MaxSize = 2097152) {
  10. Determine the error number, only for 0 or UPLOAD_ERR_OK, no error occurred, upload successful
  11. if ($fileInfo [' Error ']>0) {
  12. Attention! Error message No 5
  13. Switch ($fileInfo [' ERROR ']) {
  14. Case 1:
  15. $mes = ' The upload file exceeds the value of the upload_max_filesize option in the PHP configuration file ';
  16. Break
  17. Case 2:
  18. $mes = ' exceeds the size of the HTML form max_file_size limit ';
  19. Break
  20. Case 3:
  21. $mes = ' file part is uploaded ';
  22. Break
  23. Case 4:
  24. $mes = ' no option to upload file ';
  25. Break
  26. Case 6:
  27. $mes = ' No temporary directory found ';
  28. Break
  29. Case 7:
  30. $mes = ' file write Failed ';
  31. Break
  32. Case 8:
  33. $mes = ' uploaded file is interrupted by PHP extension ';
  34. Break
  35. }
  36. Exit ($mes);
  37. }
  38. $ext =pathinfo ($fileInfo [' name '],pathinfo_extension);
  39. $ALLOWEXT =array (' jpeg ', ' jpg ', ' png ', ' gif ');
  40. Detecting the type of upload file
  41. if (In_array ($ext, $allowExt)) {
  42. Exit (' Illegal file type ');
  43. }
  44. Detects if the size of the uploaded text conforms to the specification
  45. $maxSize = 2097152;//2m
  46. if ($fileInfo [' Size ']> $maxSize) {
  47. Exit (' Upload file too large ');
  48. }
  49. Detect if a picture is a real picture type
  50. $flag =true;
  51. if ($flag) {
  52. if (!getimagesize ($fileInfo [' tmp_name '])) {
  53. Exit (' Not a real picture type ');
  54. }
  55. }
  56. Detection is uploaded via HTTP post
  57. if (!is_uploaded_file ($fileInfo [' tmp_name '])) {
  58. Exit (' File not uploaded by HTTP post ');
  59. }
  60. $uploadPath = ' uploads ';
  61. If you do not have this folder, create a
  62. if (!file_exists ($uploadPath)) {
  63. mkdir ($uploadPath, 0777, true);
  64. chmod ($uploadPath, 0777);
  65. }
  66. New file name unique
  67. $uniName = MD5 (Uniqid (Microtime (True), true)). '. '. $ext;
  68. $destination = $uploadPath. ' /'. $uniName;
  69. The @ sign is to keep the customer from seeing the error message
  70. if (! @move_uploaded_file ($fileInfo [' Tmp_name '], $destination)) {
  71. Exit (' File move failed ');
  72. }
  73. echo ' file upload succeeded ';
  74. Return Array (
  75. ' NewName ' = $destination,
  76. ' Size ' = $fileInfo [' Size '],
  77. ' Type ' = $fileInfo [' type ']
  78. //);
  79. return $destination;
  80. }
  81. ?>
Copy Code
File Upload, PHP
  • 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.