Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to handle multiple file upload using PHP

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?

like image 442
trrrrrrm Avatar asked Feb 10 '10 01:02

trrrrrrm


People also ask

How to upload multiple files with HTML and PHP?

In this article, we will look at how to upload multiple files with HTML and PHP. Multiple image upload allows the user to select multiple files at once and upload all files to the server. index.html Create a simple HTML page to select multiple files and submit it to upload files on the server.

How do I upload multiple files at once?

Multiple files can be selected and then uploaded using the <input type='file' name='file[]' multiple> The sample php script that does the uploading:

Why is it important to customize single file upload PHP code?

Also, it is important to customize your single file upload PHP code and enable your file element to select multiple files. When you can upload your files at a go, you can boost your efficiency and productivity by saving time in performing multiple upload tasks.

How to limit the number of files that can be uploaded?

The max_file_uploads configuration setting acts as a limit on the number of files that can be uploaded in one request. You will need to ensure that your form does not try to upload more files in one request than this limit. In HTML file upload fields, it is possible to upload an entire directory with the webkitdirectory attribute.


3 Answers

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';
                }
            }
        }
    }
?>
like image 105
John Conde Avatar answered Oct 09 '22 21:10

John Conde


This is a preferred method of mine. It includes a mysql insert to keep a table on the images uploaded. It also moves the image to the admin image folder and copies the image to the user sites image folder.

<?php
if(isset($_FILES['image']['tmp_name']))
{
    $num_files = count($_FILES['image']['tmp_name']);
    for($x =0; $x< $num_files;$x++){
        $image = $_FILES['image']['name'][$x];
        if(!is_uploaded_file($_FILES['image']['tmp_name'][$x]))
        {
            $messages[] = $image.' No file uploaded."<br>"';
        }
        if (move_uploaded_file($_FILES["image"]["tmp_name"][$x],"images/". $image)){
            $messages[] = $image .' uploaded';
        }
        copy("images/".$image, '../images/'.$image);
        mysql_query("INSERT INTO $table_name VALUES ('NULL','$id','images/$image')");
    }
}
?>
<?php /*insert this into the form*/ 
$count= count($messages); for ($i =0; $i < $count; $i++){echo $messages[$i]."<br>";}
  ?>
like image 23
aarondartt Avatar answered Oct 09 '22 21:10

aarondartt


Try this:

if(isset($_FILES['image_file'])) {
    $file = $_FILES['image_file'];
    for($i = 0; $i < count($file['name']); $i++){
        $image = array(
            'name' => $file['name'][$i],
            'type' => $file['type'][$i],
            'size' => $file['size'][$i],
            'tmp_name' => $file['tmp_name'][$i],
            'error' => $file['error'][$i]
        );
// Here is your code to handle one file
}

In your code, just use '$image' instead of '$_FILES' ...

like image 1
Hazem_M Avatar answered Oct 09 '22 19:10

Hazem_M