Home > Backend Development > PHP Tutorial > PHP implements multi-file upload program code_PHP tutorial

PHP implements multi-file upload program code_PHP tutorial

WBOY
Release: 2016-07-20 11:11:54
Original
818 people have browsed it

There is not much difference between PHP file upload and multi-file upload. For multi-file upload, we just change the form name into an array form, and use foreach traversal to achieve multi-file upload. For dynamic multi-file upload, just add a dynamic increase in js. For the multi-file upload box, just traverse the array when processing it in PHP.

The simplest example is as follows

The code is as follows Copy code
 代码如下 复制代码


Pictures:





       foreach ($_FILES["pictures"]["error"] as $key => $error) {
        if ($error == UPLOAD_ERR_OK) {
            $tmp_name = $_FILES["pictures"]["tmp_name"][$key];
            $name = $_FILES["pictures"]["name"][$key];
            move_uploaded_file($tmp_name, "data/$name");
        }
}
?>

 


Pictures :


< ;input type="file" name="pictures[]" />


foreach ($_FILES["pictures"]["error"] as $key => $error) {
  if ($ error == UPLOAD_ERR_OK) {
              $tmp_name = $_FILES["pictures"]["tmp_name"][$key];
                        $name = $_FILES["pictures"]["name"][ $key ]; >

Share examples from other friends below
Example 1

 代码如下 复制代码

//filename:multi_upload.php
if($ifupload)
{
$path=AddSlashes(dirname($PATH_TRANSLATED))."upload";
for($i=1;$i<=8;$i++)
{
$files="afile".$i;
if(${$files}!="none")
{
if(copy(${$files},$path.${$files."_name"}))
{
}
}
}
print "You have uploaded files successfully
";
print "Return";
exit;
}
?>





多个文件上传









多文件上传















































































If we want dynamic and uncertain multi-file upload, how can we implement it? There are also examples below

File upload code

 代码如下 复制代码
view plaincopy to clipboardprint?




文档上传







" />



文件上传列表









文档上传







" />



文件上传列表





Submit File code
view plaincopy to clipboardprint?




File upload result


if ($_POST["submitfile"]!="")
{
$Path="./".date('Ym')."/";
if (!is_dir($Path))//Create path
{ mkdir($Path); }
echo "
";
for ($i=0;$i< ;count($filelist);$i++)
{ //The order of $_FILES["filelist"]["size"][$i] cannot be changed 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 uploaded successfully file Type: ".$_FILES["filelist"]["type"][$i]." "."File name:"
.$_FILES["filelist"]["name"][$i]. "
"; }
else
{ echo "File name:".$_FILES["filelist"]["name"][$i]."Upload failed
"; }
}
}
echo "

return}
?>

Another: Error message description
From Starting with PHP 4.2.0, PHP will return a corresponding error code along with the file information array. This code can be found in the error field in the file array generated when the file is uploaded, that is, $_FILES['userfile']['error'].
UPLOAD_ERR_OK
The value is 0, no error occurred, and the file was uploaded successfully.
UPLOAD_ERR_INI_SIZE
The value is 1, and the uploaded file exceeds the value limited by the upload_max_filesize option in php.ini.
UPLOAD_ERR_FORM_SIZE
With a value of 2, the size of the uploaded file exceeds the value specified by the MAX_FILE_SIZE option in the HTML form. UPLOAD_ERR_PARTIAL
The value is 3, the file is only partially uploaded.
UPLOAD_ERR_NO_FILE
The value is 4, no file was uploaded.
UPLOAD_ERR_NO_TMP_DIR
The value is 6 and the temporary folder cannot be found. Introduced in PHP 4.3.10 and PHP 5.0.3.
UPLOAD_ERR_CANT_WRITE
The value is 7, file writing failed. Introduced in PHP 5.1.0.
Note: The above values ​​became PHP constants after PHP 4.3.0.


www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/444605.htmlTechArticleThere is not much difference between PHP file upload and multi-file upload. For multi-file upload, we just change the form name to an array. Form, and obtaining multiple files can be uploaded by using foreach traversal...
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template