How to Select and Upload Multiple Files With HTML and PHP, Using Http Post

How can I select and upload multiple files with HTML and PHP, using HTTP POST?

This is possible in HTML5. Example (PHP 5.4):

<!doctype html>
<html>
<head>
<title>Test</title>
</head>
<body>
<form method="post" enctype="multipart/form-data">
<input type="file" name="my_file[]" multiple>
<input type="submit" value="Upload">
</form>
<?php
if (isset($_FILES['my_file'])) {
$myFile = $_FILES['my_file'];
$fileCount = count($myFile["name"]);

for ($i = 0; $i < $fileCount; $i++) {
?>
<p>File #<?= $i+1 ?>:</p>
<p>
Name: <?= $myFile["name"][$i] ?><br>
Temporary file: <?= $myFile["tmp_name"][$i] ?><br>
Type: <?= $myFile["type"][$i] ?><br>
Size: <?= $myFile["size"][$i] ?><br>
Error: <?= $myFile["error"][$i] ?><br>
</p>
<?php
}
}
?>
</body>
</html>

Here's what it looks like in Chrome after selecting 2 items in the file dialog:

chrome multiple file select

And here's what it looks like after clicking the "Upload" button.

submitting multiple files to PHP

This is just a sketch of a fully working answer. See PHP Manual: Handling file uploads for more information on proper, secure handling of file uploads in PHP.

Multiple File Upload with PHP

Replace input file name like files[] not like files see following form example:

<form action="" method="post" enctype="multipart/form-data">
<input type="file" id="file" name="files[]" multiple="multiple" accept="image/*" />
<input type="submit" value="Upload!" />
</form>

Upload Multiple Files HTML5 / PHP

I have a similar code actually in one of my projects. Try it.

foreach ($_FILES['files']['name'] as $f => $name) {
move_uploaded_file($_FILES["files"]["tmp_name"][$f], $file_path);
}

Look at the following page:
http://php.net/manual/en/function.move-uploaded-file.php

EDIT:
Nowhere in the code you provided, does it show that you actually give your file a filename, you simply refer to a path, rather than a path+filename+extension

move_uploaded_file($_FILES["files"]["tmp_name"][$f], $file_path . $name);

modifying my original code sample to be like the second one, should work.

On an html/php file upload page, can you allow users to select files for upload one at a time without overwriting the previously selected file?

Let me see if I get this straight: you are using HTML file-type input with multiple attribute, meaning you can send multiple files on the same input.

That works this way:

The multiple attribute is a boolean attribute.

When present, it specifies that the user is allowed to enter more than
one value in the element.

Note: The multiple attribute works with the following input types:
email, and file.

Tip: For <input type="file">: to select multiple files, hold down the
CTRL or SHIFT key while selecting.

So if you're trying to upload multiple files and assign them to the same file-type input, it's only natural it gets overwritten every time you manage to select different files again.

If you want to select one file at a time, insert multiple file-type inputs in your form. If you have no idea how many files may be attached, deal with input generation dynamically with Javascript or even PHP, if it isn't mandatory that your form page is pure front-end.

Multiple file upload with php and html

The problem was a permission problem, the folder I was uploading to didn't have proper permissions. I used chmod -R 777 to change permission so that php can upload to that folder



Related Topics



Leave a reply



Submit