PHP multiple file and picture upload example detailed _php skills

Source: Internet
Author: User
Tags file upload php script

This article describes a number of PHP files and image upload methods. Share to everyone for your reference. The implementation methods are as follows:

Multiple file uploads on the basis of a single file upload using traversal array to traverse the form of the singular group and then upload the file to the server, below to see a simple multiple file upload instance

Multiple file uploads and individual file uploads are handled the same way, simply by providing several input forms of type "file" to the client and specifying different "name" property values. For example, in the following code, you can have the user select three local files to pass to the server together, and the client's form looks like this:

Copy Code code as follows:
<body>
<form action= "mul_upload.php" method= "post" enctype= "Multipart/form-data" >
<input type= "hidden" name= "max_file_size" value= "1000000" >
Select File 1:<input type= "file" Name= ' myfile[] ' ><br>
Select File 2:<input type= "file" Name= ' myfile[] ' ><br>
Select File 3:<input type= "file" Name= ' myfile[] ' ><br>
<input type= "Submit" value= "Upload file" >
</form>
</body>

In the above code, the forms of three file types are organized together in an array. When the above form teaches PHP script file mul_upload.php, the server side also uses the global array $_files to store all of the above file information, but $_files from the two-dimensional array has been converted to three-dimensional array, so that can store multiple uploaded file information. In the script file mul_upload.php, use the Print_r () function to output the contents of the $_files array, as shown in the following code:
Copy Code code as follows:
<?php
Print the contents of the three-dimensional array $_files to see the structure of the stored upload file
Print_r ($_files);
?>

When three local file submissions are selected, the output is shown below

Copy Code code as follows:
Array (
[Myfile]=>array (
[Name]=>array (//$_files["myfile"] ["name"] stores the contents of all uploaded files
[0]=>rav.ini//$_files["myfile"] ["name"][0] The name of the first upload file
[1]=>msgsocm.log//$_files["myfile"] ["name"][1] The name of the second upload file
[2]=>notepad. EXE)//$_files["MyFile" ["Name"][2] Third upload file name
[Type]=>array (//$_files["myfile"] ["type"] stores all types of uploaded files
[0]=>application/octet-stream//$_files["myfile"] ["type"][0] the type of the first upload file
[1]=>application/octet-stream//$_files["myfile"] ["type"][1] The type of the second upload file
[2]=>application/octet-stream)//$_files["myfile" ["type"][2] The third type of upload file
[Tmp_name]=>array (
[0]=>c:/windows/temp/phpaf.tmp
[1]=>c:/windows/temp/phpb0.tmp
[2]=>c:/windows/temp/phpb1.tmp)
[Error]=>array (
[0]=>0
[1]=>0
[2]=>0)
[Size]=>array (
[0]=>64
[1]=>1350
[2]=>66560)]
)

By outputting the value of the $_files array, you can see that the processing of multiple file uploads is the same as when a single file is uploaded, except that the structure of the $_files array is slightly different. This way you can support a larger number of file uploads.
Examples are as follows:
Copy Code code as follows:
<! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">
<meta http-equiv= "Content-type" content= "text/html; charset=gb2312 "/>
<title> Document Upload </title>
<body>
<script language= "JavaScript" ><!--
Dynamically adding a file selection control-->
function AddRow ()
{
var enewrow = Tbldata.insertrow ();
for (Var i=0;i<1;i++)
{
var Enewcell = Enewrow.insertcell ();
enewcell.innerhtml = "<tr><td><input type= ' file ' name= ' filelist[] ' size= '/></td></tr > ";
}
}
--></script>
<form name= "MyForm" method= "post" action= "uploadfile.php" enctype= "Multipart/form-data" >
<table id= "Tbldata" width= "border=" "0" >
<!--will upload the file must use the Post method and enctype= "Multipart/form-data"-->
<!--the Web page to uploadfile.php-->
<input name= "Postadd" type= hidden "value=" <?php echo "http://" $_server["Http_host"].$_server["PHP_SELF"];? > "/>
<tr><td> File Upload List
<input type= "button" Name= "AddFile" onclick= "AddRow ()" value= "Add List"/></td></tr>
<!--filelist[] must be an array-->
<tr><td><input type= "File" name= "filelist[" "size="/></td></tr>
</table>
<input type= "Submit" Name= "Submitfile" value= "Submitting Documents"/>
</form>
</body>

Copy Code code as follows:
<! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">
<meta http-equiv= "Content-type" content= "text/html; charset=gb2312 "/>
<title> Document Upload </title>
<body>
<script language= "JavaScript" ><!--
Dynamically adding a file selection control-->
function AddRow ()
{
var enewrow = Tbldata.insertrow ();
for (Var i=0;i<1;i++)
{
var Enewcell = Enewrow.insertcell ();
enewcell.innerhtml = "<tr><td><input type= ' file ' name= ' filelist[] ' size= '/></td></tr > ";
}
}
--></script>
<form name= "MyForm" method= "post" action= "uploadfile.php" enctype= "Multipart/form-data" >
<table id= "Tbldata" width= "border=" "0" >
<!--will upload the file must use the Post method and enctype= "Multipart/form-data"-->
<!--the Web page to uploadfile.php-->
<input name= "Postadd" type= hidden "value=" <?php echo "http://" $_server["Http_host"].$_server["PHP_SELF"];? > "/>
<tr><td> File Upload List
<input type= "button" Name= "AddFile" onclick= "AddRow ()" value= "Add List"/></td></tr>
<!--filelist[] must be an array-->
<tr><td><input type= "File" name= "filelist[" "size="/></td></tr>
</table>
<input type= "Submit" Name= "Submitfile" value= "Submitting Documents"/>
</form>
</body>

Submit File Code
Copy Code code as follows:
<! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">
<meta http-equiv= "Content-type" content= "text/html; charset=gb2312 "/>
<title> File Upload Results </title>
<body>
<?php
if ($_post["Submitfile"]!= "")
{
$Path = "./". Date (' Ym '). " /";
if (!is_dir ($Path))//create Path
{mkdir ($Path);}
echo "<div>";
for ($i =0; $i <count ($filelist); $i + +)
{//$_files["filelist"] ["size"] [$i] can not be arranged in order, because Fileist is a two-dimensional array
if ($_files["filelist"] ["size"] [$i]!=0)
{
$File = $Path. Date (' Ymdhm '). _ ". $_files[" FileList "[" Name "] [$i];
if (Move_uploaded_file ($_files["filelist"] ["tmp_name"] [$i], $File))
{echo "File upload successful file type:". $_files["FileList"] ["type"] [$i]. " "." File name: "
. $_files["FileList" ["Name"] [$i]. " <br> "; }
Else
{echo "filename:". $_files["FileList"] ["name"] [$i]. " Upload Failure </br> "; }
}
}
echo "</div><br><a href=" $postadd "href=" $postadd "> Return </a></div>";
}
?>
</body>

The above example based on JS to dynamically increase the upload file box, so as to achieve multiple file upload function.

I hope this article will help you with your PHP program design.

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.