繁体   English   中英

php/jquery 多文件上传

[英]php/jquery multiple file uploads

我知道已经有很多关于此的主题,并且我已经阅读了很多,但无济于事。 我有一个用于上传多个带有描述的文件的表单。 如果用户需要多个上传/描述字段,他们可以单击链接通过 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。

我相信这是<input type="file" name="file[]" id="file" />根据 php.net 的正确用法,但是 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 发送并毫无问题地接收。 我究竟做错了什么?

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

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

然后对 firefox 使用诸如 livehttpheaders 之类的东西来查看数据是否真正通过。 添加该 enctype 后,php 端的 $_FILES 数组中应该有一些内容。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM