0

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?

3
  • var_dump your $_POST and $_FILE variables and see what is in there. Commented Jul 22, 2011 at 4:01
  • output from var_dump: array(5) { ["article_id"]=> string(2) "24" ["new_path"]=> string(30) "images/gallery/family_outings/" ["file"]=> array(2) { [0]=> string(12) "DSC01121.jpg" [1]=> string(12) "DSC01126.jpg" } ["desc"]=> array(2) { [0]=> string(5) "test1" [1]=> string(5) "test2" } ["submit"]=> string(6) "Submit" } array(0) { } Commented Jul 22, 2011 at 4:09
  • the empty array at the end being the dump of $_FILES Commented Jul 22, 2011 at 4:10

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

oh man, i knew it was going to be something stupid like that. thanks!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.