Home  >  Article  >  Backend Development  >  PHP file upload sample code

PHP file upload sample code

WBOY
WBOYOriginal
2016-07-25 08:45:08925browse
  1. Image upload
  2. Upload file:

  3. //Types that can be uploaded
  4. $uptypes=array('image/jpg','image/jpeg','image/png','image/pjpeg','image/gif ',
  5. 'image/bmp','image/x-png');
  6. ?>
  7. The file types allowed to be uploaded are:
  8. $max_file_szie=2*pow(2,20); //The uploaded file is less than 2MB
  9. $destination_folder='uploadimg/'; // Upload file saving path
  10. if($_SERVER['REQUEST_METHOD']=='POST'){
  11. if(!is_uploaded_file($_FILES['upfile']['tmp_name'])){
  12. echo 'The picture does not exist! ';
  13. exit;
  14. }
  15. if($max_file_szie<$_FILES['upfile']['size']){
  16. echo 'The file is too big! ';
  17. exit;
  18. }
  19. if(!in_array($_FILES['upfile']['type'],$uptypes)){
  20. echo 'The file type does not match! '.$_FILES['upfile']['type'];
  21. exit;
  22. }
  23. if(!file_exists($destination_folder)){
  24. mkdir($destination_folder);
  25. }
  26. $filename=$_FILES['upfile' ]['tmp_name'];
  27. $image_size=getimagesize($filename);
  28. $pinfo=pathinfo($_FILES['upfile']['name']); //File path information
  29. $ftype=$pinfo[' extension']; //Old file extension
  30. $destination = $destination_folder.time().".".$ftype; //New file name
  31. if(file_exists($destination)&&$voerwrie !=true){
  32. echo 'The file with the same name already exists! ';
  33. exit;
  34. }
  35. //Move the uploaded file from the temporary folder to the specified directory
  36. if(!move_uploaded_file($filename,$destination)){
  37. echo 'An error occurred while moving the file! ';
  38. exit;
  39. }
  40. $pinfo=pathinfo($destination);
  41. $fname=$pinfo[basename];
  42. echo "Uploaded successfully< br>File name:
  43. ".$destination_folder.$fname."
    ";
  44. echo 'Width:'.$image_size[0];
  45. echo 'Height:'.$image_size[1];
  46. echo '
    Size:'.$_FILES['upfile']['size']."bytes";
  47. }
  48. ?>
Copy Code

File upload, php


Statement:
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
Previous article:A PHP caching classNext article:A PHP caching class