简体   繁体   English

php/jquery 多文件上传

[英]php/jquery multiple file uploads

I know there's quite a few topics on this already, and I've read quite a few of them, but to no avail.我知道已经有很多关于此的主题,并且我已经阅读了很多,但无济于事。 I have a form for uploading multiple files with descriptions.我有一个用于上传多个带有描述的文件的表单。 If the user needs more than one upload/description field, they can click a link to add another via jquery.如果用户需要多个上传/描述字段,他们可以单击链接通过 jquery 添加另一个。 The code for the form is:表格的代码是:

<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>

A clone of一个克隆

<div id="file" class="row">
    <input type="file" name="file[]" id="file" /><br />
    Description:<br />
    <textarea name="desc[]" cols="50" rows="5"></textarea>
</div>

is appended to the files div when add_upload is clicked.单击 add_upload 时将附加到文件 div。

I believe this is correct usage of <input type="file" name="file[]" id="file" /> according to php.net, however the listener script at adm.php?mode=upload never receives the $_FILES array upon submission:我相信这是<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;

break;休息;

I always get the "No files set for upload??"我总是收到“没有设置上传的文件??” error because the files aren't being transferred.错误,因为文件没有被传输。 All of the other values are sent over POST and received with no problem.所有其他值都通过 POST 发送并毫无问题地接收。 What am I doing wrong?我究竟做错了什么?

Your form declaration in HTML needs to have the enctype property set.您在 HTML 中的表单声明需要设置 enctype 属性。

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

Then use something like livehttpheaders for firefox to see if the data is actually going across.然后对 firefox 使用诸如 livehttpheaders 之类的东西来查看数据是否真正通过。 After you add that enctype, there should be something in the $_FILES array on the php side.添加该 enctype 后,php 端的 $_FILES 数组中应该有一些内容。

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

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