简体   繁体   中英

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

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:

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. What am I doing wrong?

Your form declaration in HTML needs to have the enctype property set.

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

Then use something like livehttpheaders for firefox to see if the data is actually going across. After you add that enctype, there should be something in the $_FILES array on the php side.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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