Share php code for processing single files and uploading multiple files,
Php processes single files and multi-File Upload instance code for your reference. The specific content is as follows:
Submit_form_process.php
<? Php /************************************** **************************************** parameter description: $ max_file_size: the size limit of the uploaded file. Unit: BYTE $ destination_folder: Upload File Path $ watermark: whether to append the watermark (1 indicates adding the watermark, and others indicates not adding the watermark). Usage: 1. set PHP. remove the SN of the line "extension = php_gd2.dll" in the INI file because the GD library is used. 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; // the size of the uploaded file. The unit is BYTE $ destination_folder = get_stylesheet_directory (). '/mytest/'; // Upload File Path $ watermark = 1; // whether to append the watermark (1 is the watermark, and others are not the watermark); $ watertype = 1; // watermark type (1 indicates text, 2 indicates image) $ waterposition = 1; // watermark position (1 indicates the lower left corner, 2 indicates the lower right corner, 3 indicates the upper left corner, and 4 indicates the upper right corner, 5); $ waterstring = "test"; // watermark string $ waterimg = "xplore.gif"; // watermark image $ Imgpreview = 1; // whether to generate a preview image (1 is generated, others are not generated); $ imgpreviewsize = 1/2; // thumbnail ratio if ($ _ SERVER ['request _ method'] = 'post') {$ fileArray = $ _ FILES ['upfile']; // obtain the information of multiple files. Note: The key name here does not contain [] print_r ($ fileArray); echo "<br/>"; if (! Is_uploaded_file ($ _ FILES ["upfile"] ['tmp _ name']) // check whether a file exists {echo "the image does not exist! "; Exit ;}$ file =$ _ FILES [" upfile "]; if ($ max_file_size <$ file [" size "]) // check the file size {echo "the file is too large! "; Exit;} if (! In_array ($ file ["type"], $ uptypes) // check the file type {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 "a file with the same name already exists"; exit;} if (! Move_uploaded_file ($ filename, $ destination) {echo "An error occurred while moving the file"; exit ;}$ pinfo = pathinfo ($ destination); $ fname = $ pinfo [basename]; echo "<font color = red> uploaded successfully </font> <br> file name: <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 = im Agecreatetruecolor ($ image_size [0], $ image_size [1]); $ white = imagecolorallocate ($ nimage, 255,255,255); $ black = imagecolorallocate ($ nimage, 0 ); $ red = imagecolorallocate ($ nimage, 0); imagefill ($ nimage, $ 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, $ image_size [0], $ image_size [1]); imagefilledrectangle ($ nimage, 1, $ image_size [1, $ image_size [1], $ white); switch ($ watertype) {case 1: // Add the watermark string imagestring ($ nimage, 2, 3, $ image_size [1]-15, $ waterstring, $ black); break; case 2: // watermark image $ simage1 = imagecreatefromgif (" Xplore.gif "); imagecopy ($ nimage, $ simage1,); 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 ;} // overwrite the original uploaded file imagedestro Y ($ nimage); imagedestroy ($ simage);} if ($ imgpreview = 1) {echo "<br> image preview: <br> "; echo " ";}}?>
Front-end 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> the file type that can be uploaded is: <? = Implode (',', $ uptypes)?> </Form>
Note 1:The biggest difference between multi-Image Upload and single-Image Upload is that the name attribute in the input. At first, my name is equal to upfile. When the server reads f iles, it can only read the last file, because FILES on the server can only read the last file, because _ FILES ["file"] on the server is the file we uploaded. When uploading multiple FILES, the post-par value will overwrite the previous value, so you can only read the last file. Now we can rename it upfile []. What we get when the server reads $ _ FILES ["file"] is an array, so I can use the array Traversal method above to obtain the information of the uploaded file.
<Input type = "file" multiple = "multiple" id = "file" name = "upfile []">
NOTE 2:You can also add an IFrame on the front-end page so that the form can be submitted to this IFrame, And the content returned by the backend service 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 the file to be uploaded is of the following type: <? = Implode (',', $ uptypes)?> </Form> <iframe name = "iframefile">
The above is all the content of this article. I hope it will be helpful for your learning and support for helping customers.