php / jquery多个文件上传

时间:2011-07-22 03:59:31

标签: php

我知道已经有很多这方面的主题了,我已经阅读了不少相关内容,但无济于事。我有一个表格用于上传包含描述的多个文件。如果用户需要多个上传/描述字段,他们可以单击链接以通过jquery添加另一个。表单的代码是:

<form action="adm.php?mode=upload" method="post">
    <input type="hidden" name="article_id" value="<?php echo $article_id; ?>" />
    <input type="hidden" name="new_path" value="<?php echo $new_path; ?>" />
    <div id="files">
        <div id="file" class="row">
            <input type="file" name="file[]" id="file" /><br />
            Description:<br />
            <textarea name="desc[]" cols="50" rows="5"></textarea>
        </div>
    </div>
    <div>
        <input type="submit" name="submit" value="Submit" />
    </div>
</form>
<a href="#" id="add_upload">Add File</a>

的克隆
<div id="file" class="row">
    <input type="file" name="file[]" id="file" /><br />
    Description:<br />
    <textarea name="desc[]" cols="50" rows="5"></textarea>
</div>
单击add_upload时,

会附加到文件div。

我认为根据php.net这是<input type="file" name="file[]" id="file" />的正确用法,但adm.php?mode = upload的监听器脚本在提交时从不接收$ _FILES数组:

case 'upload' :
    $path = request_var('new_path', '');
    $article_id = request_var('article_id', 0);
    $error = array();
    $messages = array();

if(isset($_FILES['file']['tmp_name']))
{
    // Number of uploaded files
    $num_files = count($_FILES['file']['tmp_name']);

    //create the directory if it doesn't exist already
    if(!is_dir($path))
    {
        mkdir($path);
    }

    /** loop through the array of files ***/
    for($i=0; $i < $num_files;$i++)
    {
        // check if there is a file in the array
        if(!is_uploaded_file($_FILES['file']['tmp_name'][$i]))
        {
            $messages[] = 'No file uploaded';
        }
        else
        {
            // copy the file to the specified dir
            if(@copy($_FILES['file']['tmp_name'][$i],$path.'/'.$_FILES['file']['name'][$i]))
            {
                $messages[] = $_FILES['file']['name'][$i].' uploaded';
            }
            else
            {
                /*** an error message ***/
                $messages[] = 'Uploading '.$_FILES['file']['name'][$i].' Failed';
            }
        }
    }
}
else
{
    $messages[] = 'No files set for upload??';
}

$err_msg = $messages;
include($root_path.'pages/header.php');
include($root_path.'pages/error.php');
include($root_path.'pages/column.php');
include($root_path.'pages/footer.php');
exit;

中断;

我总是得到“没有文件设置上传??”错误,因为文件没有被传输。所有其他值都通过POST发送并且没有问题。我做错了什么?

1 个答案:

答案 0 :(得分:1)

HTML中的表单声明需要设置enctype属性。

<form enctype="multipart/form-data">

然后使用像livehttpheaders这样的东西来查看数据是否确实存在。添加该enctype后,php端的$ _​​FILES数组中应该有一些东西。

相关问题