Upload File Code in PHP with Master notes

Source: Internet
Author: User

PHP is still quite commonly used, and everyone knows about its powerful functions. Here we will implement PHP file upload code analysis. PHP is difficult to implement the upload progress bar because when we upload files to the server, we need to wait until all the files are sent to the server before executing the corresponding PHP file. Before that, the file data is stored in a temporary file, and php cannot obtain the path and size of the file.

Flash supports uploading and downloading files starting with Actionscript 2.0. Although the File Upload progress cannot be obtained on the server side, we can get the file sending progress on the server side. Based on this principle, you can use Flash to make the upload progress bar effect. I have seen some materials on the Internet, but I feel defective. So I studied it myself and strengthened the security and robustness of the program based on the predecessors, and added some customizable parameters. Currently, I know two methods. One is to use the APC extension module written by RasmusLerdorf, founder of PHP, another method is to use the PECL extension module uploadprogress for implementation. Here are two implementation examples for reference. More flexible applications can be modified as needed.

Php uploads a file to determine whether the path to be stored exists is_dir () and if whether the file type meets the requirements of explode ('. ', name) array () in_array (), if the file size meets the requirements $ _ FILES [size], you can upload move_uploaded_file ($ _ FILES [] ['tmp _ name'], the path to store and the file name) save $ _ FILES [] ['name'] or rename: time ().

PHP file upload code:

 
 
  1. Function uploafile ($ path, $Filesize=10241000, $Type=1)
  2. {
  3. If (! Empty ($ path )&&! File_exists ($ path ))
  4. {
  5. Echo 'the path of the uploaded file does not exist'; exit;
  6. }
  7. // File type that can be uploaded
  8. $Filetype=Array 
  9. (
  10.  1=>Array ('gif', 'png ', 'jpg', 'jpeg ', 'bmp '),
  11.  2=>Array ('swf ', 'flv '),
  12.  3=>Array ('rm ', 'rmvb', 'av', 'wmv ', 'mpg', 'asf', 'mp3', 'wm', 'wmv ', 'Mid '),
  13.  4=>Array ('txt ', 'Doc', 'xls', 'ppt ', 'pdf', 'xml', 'rar ', 'zip', 'gzip ', 'cab ', 'iso', 'SQL '),
  14.  6=>Array ('exe ', 'com', 'Scr ', 'bat ')
  15. );
  16. // Merge the uploaded file type Arrays
  17. $ Filetype [5] = array_merge ($ filetype [2], $ filetype [3]);
  18. // Obtain all keys in the $ _ FILES Array
  19. $Arr_key=Array_keys($ _ FILES );
  20. // Determine the name of the file to be uploaded
  21. $File= $ _ FILES [$ arr_key [0];
  22. $Exten=Check_file_type($ File ['name']);
  23. // Determine the File Upload type
  24. If ($ type! = 7 &&! In_array ($ exten, $ filetype [$ type])
  25. {
  26. The echo $ file ['name']. 'file type does not meet the requirements! '; Exit;
  27. }
  28. // Determine the file size
  29. If ($ file ['SIZE']>$ Filesize)
  30. {
  31. Echo $ file ['SIZE']. 'cannot exceed'. $ filesize. 'byte'; exit;
  32. }
  33. // Start upload
  34. // Move_uploaded_file ($ file ['tmp _ name'], $ path. $ file ['name']);
  35. // Re-name the file to avoid Overwriting
  36. $Newname=Time(). '.'. $ Exten;
  37. Move_uploaded_file ($ file ['tmp _ name'], $ path. $ newname );
  38. Return $ newname;
  39. }
  40.  
  41. Echo uploafile ('', $Filesize=10241000, $Type=3);
  42. ?> 

The above is the detailed PHP file upload code, it is very simple.


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.