Heim  >  Artikel  >  Backend-Entwicklung  >  PHP 图片上传代码示例

PHP 图片上传代码示例

WBOY
WBOYOriginal
2016-07-25 08:45:13952Durchsuche

PHP 文件上传代码示例

  1. 图片上传
  2. 上传文件:

  3. //能够上传的类型
  4. $uptypes=array('image/jpg','image/jpeg','image/png','image/pjpeg','image/gif',
  5. 'image/bmp','image/x-png');
  6. ?>
  7. 允许上传的文件类型为:=implode(',',$uptypes)?>
  8. $max_file_szie=2*pow(2,20); //上传的文件小于2MB
  9. $destination_folder='uploadimg/'; //上传文件保存路径
  10. if($_SERVER['REQUEST_METHOD']=='POST'){
  11. if(!is_uploaded_file($_FILES['upfile']['tmp_name'])){
  12. echo '图片不存在!';
  13. exit;
  14. }
  15. if($max_file_szie echo '文件太大了!';
  16. exit;
  17. }
  18. if(!in_array($_FILES['upfile']['type'],$uptypes)){
  19. echo '文件类型不符合!'.$_FILES['upfile']['type'];
  20. exit;
  21. }
  22. if(!file_exists($destination_folder)){
  23. mkdir($destination_folder);
  24. }
  25. $filename=$_FILES['upfile']['tmp_name'];
  26. $image_size=getimagesize($filename);
  27. $pinfo=pathinfo($_FILES['upfile']['name']); //文件路径信息
  28. $ftype=$pinfo['extension']; //旧文件后缀名
  29. $destination = $destination_folder.time().".".$ftype; //新文件名称
  30. if(file_exists($destination)&&$voerwrie !=true){
  31. echo '同名文件已经存在了!';
  32. exit;
  33. }
  34. //把上传的文件从临时文件夹移动到指定目录
  35. if(!move_uploaded_file($filename,$destination)){
  36. echo '移动文件出错了!';
  37. exit;
  38. }
  39. $pinfo=pathinfo($destination);
  40. $fname=$pinfo[basename];
  41. echo "已经成功上传
    文件名:
  42. ".$destination_folder.$fname."
    ";
  43. echo '宽度:'.$image_size[0];
  44. echo '高度:'.$image_size[1];
  45. echo '
    大小:'.$_FILES['upfile']['size']."bytes";
  46. }
  47. ?>
复制代码

图片上传, PHP


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
Vorheriger Artikel:php模拟post发送数据 Nächster Artikel:php常用的字符串处理函数