Home  >  Article  >  Backend Development  >  Sharing examples of php processing single file and multiple file uploads

Sharing examples of php processing single file and multiple file uploads

墨辰丷
墨辰丷Original
2018-06-01 14:56:571301browse

This article mainly introduces the PHP processing single file and multiple file upload code in detail. It has certain reference value. Interested friends can refer to

php processing single file and multiple files. Upload the example code for your reference. The specific content is as follows

Backend processing file submit_form_process.php

";
  if (!is_uploaded_file($_FILES["upfile"]['tmp_name'])) 
  //是否存在文件 
  { 
     echo "图片不存在!"; 
     exit; 
  } 

  $file = $_FILES["upfile"]; 
  if($max_file_size < $file["size"]) 
  //检查文件大小 
  { 
    echo "文件太大!"; 
    exit; 
  } 
 
  if(!in_array($file["type"], $uptypes)) 
  //检查文件类型 
  { 
    echo "文件类型不符!".$file["type"]; 
    exit; 
  } 
 
  if(!file_exists($destination_folder)) 
  { 
    mkdir($destination_folder); 
  } 
  $filename=$file["tmp_name"]; 
  $image_size = getimagesize($filename); 
  $pinfo=pathinfo($file["name"]); 
  $ftype=$pinfo['extension']; 
  $destination = $destination_folder.time().".".$ftype; 
  //$destination = $destination_folder.$file["name"]; 
  if (file_exists($destination) && $overwrite != true) 
  { 
    echo "同名文件已经存在了"; 
    exit; 
  } 
  if(!move_uploaded_file ($filename, $destination)) 
  { 
    echo "移动文件出错"; 
    exit; 
  } 

  
  $pinfo=pathinfo($destination); 
  $fname=$pinfo[basename]; 
  echo " 已经成功上传
文件名: ".$destination_folder.$fname."
"; echo " 宽度:".$image_size[0]; echo " 长度:".$image_size[1]; echo "
大小:".$file["size"]." bytes"; if($watermark==1) { $iinfo=getimagesize($destination,$iinfo); $nimage=imagecreatetruecolor($image_size[0],$image_size[1]); $white=imagecolorallocate($nimage,255,255,255); $black=imagecolorallocate($nimage,0,0,0); $red=imagecolorallocate($nimage,255,0,0); imagefill($nimage,0,0,$white); switch ($iinfo[2]) { case 1: $simage =imagecreatefromgif($destination); break; case 2: $simage =imagecreatefromjpeg($destination); break; case 3: $simage =imagecreatefrompng($destination); break; case 6: $simage =imagecreatefromwbmp($destination); break; default: die("不支持的文件类型"); exit; } imagecopy($nimage,$simage,0,0,0,0,$image_size[0],$image_size[1]); imagefilledrectangle($nimage,1,$image_size[1]-15,80,$image_size[1],$white); switch($watertype) { case 1: //加水印字符串 imagestring($nimage,2,3,$image_size[1]-15,$waterstring,$black); break; case 2: //加水印图片 $simage1 =imagecreatefromgif("xplore.gif"); imagecopy($nimage,$simage1,0,0,0,0,85,15); imagedestroy($simage1); break; } switch ($iinfo[2]) { case 1: //imagegif($nimage, $destination); imagejpeg($nimage, $destination); break; case 2: imagejpeg($nimage, $destination); break; case 3: imagepng($nimage, $destination); break; case 6: imagewbmp($nimage, $destination); //imagejpeg($nimage, $destination); break; } //覆盖原上传文件 imagedestroy($nimage); imagedestroy($simage); } if($imgpreview==1) { echo "
图片预览:
"; echo "\"图片预览:\r文件名:".$destination."\r上传时间:\""; } } ?>

Front page page

   

允许上传的文件类型为:

Note 1: The biggest difference between uploading multiple pictures and uploading a single picture is the name attribute in the input. At the beginning, my name was only Equal to upfile, when reading F ILES on the server side, only the last file can be read, because when FILES is read on the server side, only the last file can be read, because on the server side, _FILES["file"] is what we uploaded. File, when uploading multiple files, the later value will overwrite the previous value, so only the last file can be read. Now we rename it upfile[]. When the server reads $_FILES["file"], it gets an array, so I can use the array traversal method above to get the information of the uploaded file.
88c09c73e12c6fc34be8f8c5c2f1df44

Note 2 : You can also add an IFrame to the front page so that the form is submitted to this IFrame and the content returned by the background service is displayed here

  

允许上传的文件类型为: