多个文件上传错误

时间:2014-04-25 01:38:10

标签: php

我在 Anthoniraj Amalanathan 的YouTube上找到了一个很好的教程。在视频教程中,它适用于下摆,但当我尝试复制它时,我收到一个错误。这是代码:

<form action="<?php $_SERVER['PHP_SELF']; ?>" enctype="multipart/form-data" method="post">
    <input type="file" name="upload[]">
    <input type="file" name="upload[]">
    <input type="submit" name="send" value="Send Now">
</form>

<?php

if(isset($_FILES['upload'])=== true)
{
    $files = $_FILES['upload'];
    for($x=0;$x<count($files['name']);$x++)
    {
        $name=$files['name'][$x];
        $tmp_name = $file['tmp_name'][$x];
        move_uploaded_file($files,'test/'.$name);
        echo 'Upload OK';
    }
}

?>

该消息指出错误在第12行($tmp_name = $file['tmp_name'][$x];),但我似乎没有找出原因。

有人可以帮忙吗?

3 个答案:

答案 0 :(得分:2)

试试这个,我测试了它,它适用于我。

<form action="<?php $_SERVER['PHP_SELF']; ?>" enctype="multipart/form-data" method="post">
<input type="file" name="upload[]">
<input type="file" name="upload[]">
<input type="submit" name="send" value="Send Now">
</form>
<?php
if(isset($_FILES['upload'])=== true) {
    $files = $_FILES['upload'];
    for($x=0;$x<count($files['name']);$x++) {
        $name = $files['name'][$x];
        $tmp_name = $file['tmp_name'][$x];
        move_uploaded_file($files['tmp_name'][$x],'test/'.$name);
        echo 'Upload OK';
    }
}

?>

我得到的错误来自于使用数组作为临时数据。文件位置。通过将其更改为$ files [&#39; tmp_name&#39;] [$ x],它可以正常工作。

旧:move_uploaded_file($files,'test/'.$name);

新:move_uploaded_file($files['tmp_name'][$x],'test/'.$name);

答案 1 :(得分:1)

这只是一个错字。永远不会声明$file,它应该是$files

下面:

$tmp_name = $file['tmp_name'][$x];
//               ^ missing s

同样在这里:

move_uploaded_file($files,'test/'.$name);
//                 ^^^^^^ shouldn't this be $tmp_name?

答案 2 :(得分:0)

试试这样:

<?php 
if(is_uploaded_file($_FILES['upload']['tmp_name'])){ 
    foreach($_FILES['upload']['name'] as $x=>$name) {
        $name = basename($_FILES['upload']['name'][$x}); 
        $folder = 'test/'; 
        $full_path = $folder.$name ;    
        if(move_uploaded_file($_FILES['upload']['tmp_name'][$x], $full_path)) { 
            echo 'Upload OK';
        } else { 
            echo 'Upload Failed'; 
        } 
    }
}else{ 
    echo 'Upload Not Received'; 
} 
?>