用于上传多个文件的php代码

时间:2017-08-02 22:16:27

标签: php

我使用以下代码上传多个文件。我不知道这段代码是对还是错,因为我的一些用户在此页面中收到语法错误。所以,我想检查一下这段代码。请帮帮我。

<?php
if(!empty($_FILES['MyPhoto']['name'])){
    $Photos=$_FILES['MyPhoto']['name'];
    $valid_formats = array("jpeg", "jpg", "png");
    $Vars=array("A","B","C","D");
    $PicsArray=array();
    $x="0";
    $y="0";
    $Id="some_variable";

        foreach($Photos as $Pics){
            $PicType= pathinfo($Pics, PATHINFO_EXTENSION);
            if(in_array($PicType, $valid_formats)) {
                $PicName=$Id.$Vars[$y];
                $tmpFilePath = $_FILES['MyPhoto']['tmp_name'][$x];
                $newFilePath = "./MyPics/" .$PicName.".".$PicType;
                $newFilePath1 = "./Admin/MyPics/" .$PicName.".".$PicType;
                $newFilePath2 = "./Admin/NewPics/" .$PicName.".".$PicType;
                if(move_uploaded_file($tmpFilePath, $newFilePath)) {
                     copy($newFilePath, $newFilePath1);
                     copy($newFilePath, $newFilePath2);
                    }
                $y++;
                }
            $x++;
        }
    }
?>

我已经在http://phpfiddle.org/进行了测试。它显示以下错误:

您通过PhpFiddle输入禁用的功能:move_uploaded_file($ tmpFilePath,$ newFilePath)),copy($ newFilePath,$ newFilePath1),copy($ newFilePath,$ newFilePath2)

1 个答案:

答案 0 :(得分:0)

此问题已经回答here

虽然是上传一个输入,但也可以修改为上传多个输入。

<强>更新

HTML:

<input type="file" name="img[]">
<input type="file" name="img[]">

PHP:

foreach($_FILES["img"]["tmp_name"] as $key=>$tmp_name){
     //the code on the link I shared.
}