Several ways to determine the file type of uploaded files in PHP

WBOY
Release: 2016-07-25 09:04:35
Original
2412 people have browsed it
  1. /**

  2. desc: Determine the uploaded file type
  3. link: bbs.it-home.org
  4. date: 2013/2/24
  5. */
  6. $array = array('jpg','gif','png','jpeg');
  7. $picImg = '/upfile/upload_pic/thumbnail_1258615556.jpg';

  8. $img = strtolower($picImg);

  9. //Method 1 to get the file extension

  10. $ext = substr($img,strrpos($img,'.')+1);//Here is the code to read the file extension

  11. //How to get the file extension 2

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

  13. //Method 3 of getting the file extension This should be the safest, use php $ _FILES['type']

  14. $ext = $_FILES['file']['type'];

  15. //Method 4 to get the file extension

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

  17. if( !in_array( $ext,$array ) )

  18. {
  19. exit('The thumbnail address is wrong, please re-upload!');
  20. }
  21. else
  22. {
  23. echo('The file type you uploaded is not allowed');
  24. exit;
  25. }

  26. /*

  27. strtolower Convert uppercase characters to lowercase
  28. substr character interception, right Chinese processing is not friendly.
  29. strrpos Determine the position where characters appear in the specified string
  30. explode split function, return the result as an array
  31. end Read the last value of the data
  32. $_FILES Global variable file upload
  33. getimagesize Get the type of image
  34. in_array Determine whether the variable is in the array Medium
  35. exit terminates the current script running
  36. */
  37. ?>

Copy code


source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template