W D W D - 1 year ago 75
PHP Question

Upload Multiple Files HTML5 / PHP

I'm trying to build a basic upload form to add multiple files to a folder, which is processed by PHP.

The HTML code I have is:

<form id="form" action="add-files-php.php" method="POST" enctype="multipart/form-data">
<div class="addSection">Files To Add:<br><input type="file" name="files[]" multiple /></div>
<div class="addSection"><input type="submit" name="submit" value="Add Files" /></div>

And the PHP to process is:

$file_path = "../a/files/article-files/$year/$month/";
foreach ($_FILES['files']['files'] as $file) {

I can run the PHP without any errors, but the files don't get added to the path folder.

Where am I going wrong with this?

Answer Source

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.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download