views:

237

answers:

1

hello i want to upload files using PHP but the problem is that i don't know how many files i will upload, my question is how can i upload files if i use file[] ?

<form action="upload.php" method="post" enctype="multipart/form-data">
<label for="file">Filename:</label><input type="file" name="file[]" id="file" /> 
<br />
<label for="file">Filename:</label><input type="file" name="file[]" id="file" /> 
<br />
<input type="submit" name="submit" value="Submit" />
</form>

i will add just File box and i will use javascript to create more file input to upload but how to handle them in PHP ?

Thanks

+1  A: 

See: $_FILES, Handling file uploads

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

        /** 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],$upload_dir.'/'.$_FILES['file']['name'][$i]))
                {
                    /*** give praise and thanks to the php gods ***/
                    $messages[] = $_FILES['file']['name'][$i].' uploaded';
                }
                else
                {
                    /*** an error message ***/
                    $messages[] = 'Uploading '.$_FILES['file']['name'][$i].' Failed';
                }
            }
        }
    }
?>
John Conde
Actually, the recommended practice is using the move_uploaded_file() builtin.
amphetamachine
yes, i think move_uploaded_file() is better, Thanks A lot John that's works like magic !!
From.ME.to.YOU