Heim  >  Artikel  >  Backend-Entwicklung  >  PHP上传多文件、多图片的示例代码

PHP上传多文件、多图片的示例代码

WBOY
WBOYOriginal
2016-07-25 08:57:461223Durchsuche
  1. $uptypes=array(
  2. //上传文件的ContentType格式
  3. 'image/jpg',
  4. 'image/jpeg',
  5. 'image/png',
  6. 'image/pjpeg',
  7. 'image/gif',
  8. 'image/bmp',
  9. 'image/x-png',
  10. 'application/msword',//doc
  11. 'application/vnd.openxmlformats-officedocument.wordprocessingml.document',//docx
  12. 'application/vnd.openxmlformats-officedocument.presentationml.presentation',//pptx
  13. 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',//xlsx
  14. 'text/plain'
  15. );
  16. /**
  17. * php多文件、多图片上传
  18. * by bbs.it-home.org
  19. */
  20. $max_file_size=2000000; //上传文件大小限制, 单位BYTE
  21. $dir="upload/"; //上传文件路径
  22. if ($_SERVER['REQUEST_METHOD'] == 'POST')
  23. {
  24. $file = $_FILES['upfile']['name'];
  25. foreach($file as $key=>$item){
  26. if($item != ''){
  27. if (!is_uploaded_file($_FILES['upfile']['tmp_name'][$key]))//是否存在文件
  28. {
  29. echo "图片不存在!";
  30. exit;
  31. }
  32. if($max_file_size {
  33. echo "文件太大!";
  34. exit;
  35. }
  36. if(!file_exists($dir))
  37. {
  38. mkdir($dir);
  39. }
  40. $filename=$_FILES['upfile']['tmp_name'][$key];
  41. $image_size = getimagesize($filename);
  42. $pinfo = pathinfo($file[$key]);
  43. $ftype = $pinfo['extension'];
  44. $destination = $dir.time().$file[$key];
  45. if (file_exists($destination) && $overwrite != true)
  46. {
  47. echo "同名文件已经存在了";
  48. exit;
  49. }
  50. if(!move_uploaded_file ($filename, $destination))
  51. {
  52. echo "移动文件出错";
  53. exit;
  54. }
  55. $pinfo=pathinfo($destination);
  56. $fname=$pinfo['basename'];
  57. echo " 已经成功上传
    文件名: ".$dir.$fname."
    ";
  58. echo " 宽度:".$image_size[0];
  59. echo " 长度:".$image_size[1];
  60. echo "
    大小:".$_FILES['upfile']['size']." bytes";
  61. }
  62. echo "
    图片预览:
    ";
  63. echo "\"图片预览:\r文件名:".$destination."\r上传时间:\"";
  64. echo "
    ";
  65. }
  66. }
  67. ?>
复制代码

说明: 上传时,需要上传两个,否则会报错。 代码不是很完善,只是给出一个思路,仅供学习参考。



Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn