Several methods for php to determine the file type to Upload

Source: Internet
Author: User
Several methods for php to determine the file type to Upload

  1. /**

  2. Desc: determine the type of the uploaded file
  3. Link: bbs.it-home.org
  4. Date:
  5. */
  6. $ Array = array ('jpg ', 'GIF', 'PNG', 'jpeg ');
  7. $ PicImg = '/upfile/upload_pic/thumbnail_1258615556.jpg ';

  8. $ Img = strtolower ($ picImg );

  9. // Method 1 for obtaining the file extension

  10. $ Ext = substr ($ img, strrpos ($ img, '.') + 1); // code for reading the file extension

  11. // Method 2

  12. $ Ext = end (explode ('.', $ img ));

  13. // Method 3 for obtaining the file extension. this is probably the safest method, that is, php $ _ FILES ['type'].

  14. $ Ext = $ _ FILES ['file'] ['type'];

  15. // Method 4 for obtaining the file extension

  16. $ Ext = getimagesize ($ img); // This function returns an array

  17. If (! In_array ($ ext, $ array ))

  18. {
  19. Exit ('thumbnail address is incorrect. please upload it again! ');
  20. }
  21. Else
  22. {
  23. Echo ('The file type you uploaded is disallowed ');
  24. Exit;
  25. }

  26. /*

  27. Strtolower converts the character into lowercase letters
  28. Substr character truncation, unfriendly to Chinese processing.
  29. Strrpos determines the position of a character in a specified string
  30. Explode split function. the returned result is an array.
  31. End reads the last value of the data.
  32. $ _ FILES global variable file Upload
  33. Getimagesize: obtain the image type
  34. In_array: determines whether the variable is in the array.
  35. Exit to terminate the current script
  36. */
  37. ?>

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.