Php file Upload Sample code

Source: Internet
Author: User
Tags php file upload
Php file Upload Sample code

  1. Upload images
  2. $ Max_file_szie = 2 * pow (); // the uploaded file is smaller than 2 MB.
  3. $ Destination_folder = 'uploadimg/'; // upload the file storage path
  4. If ($ _ SERVER ['request _ method'] = 'post '){
  5. If (! Is_uploaded_file ($ _ FILES ['upfile'] ['tmp _ name']) {
  6. Echo 'the image does not exist! ';
  7. Exit;
  8. }
  9. If ($ max_file_szie <$ _ FILES ['upfile'] ['size']) {
  10. Echo: The file is too large! ';
  11. Exit;
  12. }
  13. If (! In_array ($ _ FILES ['upfile'] ['type'], $ uptypes )){
  14. Echo 'file type does not match! '. $ _ FILES ['upfile'] ['type'];
  15. Exit;
  16. }
  17. If (! File_exists ($ destination_folder )){
  18. Mkdir ($ destination_folder );
  19. }
  20. $ Filename = $ _ FILES ['upfile'] ['tmp _ name'];
  21. $ Image_size = getimagesize ($ filename );
  22. $ Pinfo = pathinfo ($ _ FILES ['upfile'] ['name']); // file path information
  23. $ Ftype = $ pinfo ['extension']; // suffix of the old file
  24. $ Destination = $ destination_folder.time (). ".". $ ftype; // new file name
  25. If (file_exists ($ destination) & $ voerwrie! = True ){
  26. Echo 'file with the same name already exists! ';
  27. Exit;
  28. }
  29. // Move the uploaded file from the temporary folder to the specified directory
  30. If (! Move_uploaded_file ($ filename, $ destination )){
  31. Echo 'an error occurred while moving the file! ';
  32. Exit;
  33. }
  34. $ Pinfo = pathinfo ($ destination );
  35. $ Fname = $ pinfo [basename];
  36. Echo "uploaded successfully
    File name:
  37. ". $ Destination_folder. $ fname ."
    ";
  38. Echo 'width: '. $ image_size [0];
  39. Echo 'height: '. $ image_size [1];
  40. Echo'
    Size: '. $ _ FILES ['upfile'] ['size']. "bytes ";
  41. }
  42. ?>


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.