PHP processing single file, multiple file upload code sharing _php Skills

Source: Internet
Author: User

PHP processing single file, multiple file upload instance code, for your reference, the specific content as follows

Background processing file submit_form_process.php

<?php/****************************************************************************** parameter Description: $max _file_size: Upload text 
 
Size limit, unit byte $destination _folder: Upload file path $watermark: Whether to add watermark (1 for Watermark, other for no watermark); 
Instructions for use: 1. Remove the "Extension=php_gd2.dll" line in front of the php.ini file, because we need to use the GD library; 
2. Change Extension_dir = to the directory where your Php_gd2.dll is located; ////Upload file type list $uptypes =array (' image
 
/jpg ', ' image/jpeg ', ' image/png ', ' image/pjpeg ', ' image/gif ', ' image/bmp ', ' image/x-png ';   $max _file_size=2*1024*1024; Upload file size limit, unit byte $destination _folder=get_stylesheet_directory (). ' /mytest/';   Upload file path $watermark = 1; 
Whether to add watermark (1 for Watermark, other for no watermark);   $watertype = 1;   Watermark Type (1 for text, 2 for picture) $waterposition = 1; 
Watermark position (1 is lower left corner, 2 is lower right corner, 3 is upper left corner, 4 is upper right corner, 5 is centered); $waterstring = "Test";  Watermark string $waterimg = "Xplore.gif";   Watermark Picture $imgpreview = 1; 
Whether to generate a preview diagram (1 is a build, others are not generated);  $imgpreviewsize =1/2; Thumbnail scale if ($_server[' request_method '] = = ' POST ') {$fileARray = $_files[' upfile '];//get information about multiple files, note: The key name here does not contain [] print_r ($fileArray);
  echo "<br/>"; 
     if (!is_uploaded_file ($_files["upfile"] [' tmp_name '])/whether there is a file {echo "picture does not exist!"; 
  Exit 
  } $file = $_files["Upfile"]; 
    if ($max _file_size < $file ["size"])//Check file size {echo "file is too large!"; 
  Exit } if (!in_array ($file [type], $uptypes))//Check file type {echo "file type does not match!". 
    $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 "has a file with the same name already exists"; 
  Exit 
    } if (!move_uploaded_file ($filename, $destination)) {echo "Error moving File"; 
  Exit } $pinfo =patHinfo ($destination); 
  $fname = $pinfo [basename]; echo "<font color=red> has been successfully uploaded </font><br> FileName: <font color=blue>". $destination _folder. $fname. " 
  </font><br> "; 
  echo "width:". $image _size[0]; 
  echo "Length:". $image _size[1]; 
 
  
  echo "<br> size:". $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 ("Unsupported file type"); 
    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://Add watermark String imagestring ($nimage, 2,3, $image _size[1]-15, $waterstring, $black); 
      Break 
      Case 2://Add watermark Picture $simage 1 =imagecreatefromgif ("Xplore.gif"); 
      Imagecopy ($nimage, $simage 1,0,0,0,0,85,15); 
      Imagedestroy ($simage 1); 
    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 
    //Overwrite original upload file Imagedestroy ($nimage); 
  Imagedestroy ($simage); 
 
}  if ($imgpreview ==1) {echo <br> picture preview:<br>; echo "

Front Page

   <form enctype= "Multipart/form-data" method= "post" name= "Upform" action= "submit_form_process.php"
    > 
    <input name= "Testparas" value= "test" type= "text" >
     <input name= "upfile" type= "file" > 
     <input Type= "Submit" value= "upload" ><br> 
     allow file types to be uploaded: <?=implode (', ', $uptypes)?> 
    </form>  

Note 1: The most important difference between uploading and uploading a single picture is the name attribute in input, at first my name is equal to Upfile and only the last file can be read on the server side when reading F Iles, because on the server side Files can only read the last file, because in the server side of the _files["file" is the file we uploaded, when uploading multiple files, the following value will overwrite the previous value, so can only read the last file. Now we're going to rename it upfile[], and when the server reads $_files["file", it gets an array, so I can get the information on the uploaded file by using the array method that's traversed above.
<input type= "file" multiple= "multiple" id= "file" Name= "upfile[]" >

NOTE 2: You can also add an iframe to the foreground page so that form is submitted to this IFRAME, and the backend service return content is displayed here

  <form enctype= "Multipart/form-data" method= "post" name= "Upform" tatget= "Iframefile" action= "Submit_form_"
    process.php "> 
    <input name=" Testparas "value=" test "type=" text ">
     <input name=" upfile "type=" file "> 
     <input type=" Submit "value=" upload "><br> 
     allow file types to be uploaded: <?=implode (', ', $uptypes)?> 
    </form>

<iframe name= "Iframefile" > 

The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.