Master's note php upload file code _php tutorial

Source: Internet
Author: User
PHP is still more commonly used, for its powerful function presumably everyone also know, we here to implement PHP upload file code analysis. PHP is very difficult to implement the upload progress bar is because when we upload files to the server, we have to wait until all the files are sent to the server, the corresponding PHP files are executed. Before that, the file data is stored in a temporary file, and PHP cannot get the path and size of the file.

Starting with ActionScript 2.0, Flash supports file uploads and downloads. Although it is not possible to get the file upload progress on the server, we can get the progress of sending the file on the server. According to this principle, flash can be used to make upload progress bar effect. I have seen some information on the internet, but I feel defective. So I studied it, on the basis of the predecessors to enhance the security and robustness of the program, and added some customizable parameters. There are two ways I know now, one is to use the APC extension module written by the founder of PHP Rasmuslerdorf, and the other is to use the PECL Extension Module uploadprogress to implement the example I have here to give two separate implementations for reference. More flexible applications are modified to suit their needs.

PHP upload file to determine if the path to be stored Is_dir (), if the type of file is required explode ('. ', name) array () In_array (), if the size of the file is required $_files[size] can be uploaded Move_uploaded_file ($_files[][' tmp_name '), the path and file name to be stored) is stored directly with $_files[][' name ') or renamed: Time ().

PHP Upload file code:

 
 
  1. function Uploafile ($path, $filesize=10241000, $type= 1)
  2. {
  3. if (!empty ($path) &&!file_exists ($path))
  4. {
  5. echo ' path to upload file does not exist '; exit;
  6. }
  7. Allowed file types to upload
  8. $ filetype = Array
  9. (
  10. 1 => Array (' gif ', ' png ', ' jpg ', ' jpeg ', ' BMP '),
  11. 2 => Array (' SWF ', ' flv '),
  12. 3 => Array (' RM ', ' rmvb ', ' avi ', ' wmv ', ' mpg ', ' asf ', ' MP3 ', ' wma ', ' 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. Upload file type array merge
  17. $filetype [5] = Array_merge ($filetype [2], $filetype [3]);
  18. Get all keys for the $_files array
  19. $ Arr_key = Array_keys ($_files);
  20. Determine the name of the file to upload
  21. $ file = $_files[$arr _key[0]];
  22. $ Exten = Check_file_type ($file [' name ']);
  23. Determine file Upload type
  24. if ($type!=7 &&!in_array ($exten, $filetype [$type]))
  25. {
  26. echo $file [' name ']. ' The file type does not meet the requirements! '; Exit
  27. }
  28. Determine file size
  29. if ($file [' Size ']>$filesize)
  30. {
  31. echo $file [' size ']. ' The file size cannot exceed '. $filesize. ' bytes '; exit;
  32. }
  33. Start uploading
  34. Move_uploaded_file ($file [' Tmp_name '], $path. $file [' name ']);
  35. To avoid overwriting, re-goner the file name
  36. $ newname = Time ().'.'. $exten;
  37. Move_uploaded_file ($file [' Tmp_name '], $path. $newname);
  38. return $newname;
  39. }
  40. Echo Uploafile (", $filesize=10241000, $type =3);
  41. ?>

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


http://www.bkjia.com/PHPjc/446545.html www.bkjia.com true http://www.bkjia.com/PHPjc/446545.html techarticle PHP is still more commonly used, for its powerful function presumably everyone also know, we here to implement PHP upload file code analysis. PHP is difficult to implement the upload progress bar because of the ...

  • 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.