1

I have a multipart form in which I allow the user to select multiple files which is placed in an array. Here is a scaled down portion of my code:

<form id="fileupload" action="upload.php" method="POST" enctype="multipart/form-data">
    <input type="file" name="files[]" multiple>
</form>

In my upload.php file, I want to write to a text file and place info from each file (doesn't matter how it's formatted, array is fine).

$date = $_POST["date"];
$files = $_POST["files"];


$myFile = $date . ".txt";
$fh = fopen($myFile, 'w') or die("cant_open_file");
$stringData = "Date: " . $date . "\n";
$stringData .= "Uploaded Files:\n";
$stringData .=  $file; // prints nothing?
fwrite($fh, $stringData);
fclose($fh);

However, nothing is $file is printing nothing.

Question

How can I print the file info for each file that is chosen from the user into my text file?

Desired output

Date: Todays Date
Files: 2 file(s)
    File: someimage1.png
    Size: 10KB

    File: someimage1.png
    Size: 20KB

Update

For some reason, $_FILES['files'] is blank when submitted but I when I use the preceding example, "File: " and "Size: " print out, but $file['name'] and $file['size'] seem to be blank. Also, say I choose 3 files, "File/Size" is printed 5 times (suggesting that 5 files were chosen). This is the same for any amount of files chosen. I've made sure the correct values are in php.ini as well. Not sure where to go from here.

foreach($files as $file){
    $stringData .=  "   File: ".$file['name']."\n"; // return blank
    $stringData .=  "   Size: ".$file['size']."\n"; // return blank
}

Also, I am using jQuery File Upload Demo.

4 Answers 4

4

Use $files = $_FILES["files"]; to get the uploaded file information.

POST method uploads

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

Comments

1

You can use loop in the $_FILES array:

foreach($files as $file){
    $stringData .=  "File: ".$file['name']."\n";
    $stringData .=  "Size: ".$file['size']."\n";
}

make sure you use move_uploaded_file() because you only store them in /tmp folder and you will lose them.

2 Comments

Okay, so a little progress made. Now i get "File: " and "Size: " but $file['name'] and $file['size'] seem to be blank. Also, say I choose 3 files, "File/Size" is printed 5 times (suggesting that 5 files were chosen. Not sure what thats all about.
Actually, it' doesn't matter how many files I select, it always shows as 5 files. Any idea why that would be?
1

Use

$files = $_FILES['files'];

echo "<pre>";
print_r($files);

instead of

$files = $_POST["files"];

Comments

1

Please try using $_FILES as follows instead of $_POST['files']:

print_r($_FILES['files'])

Comments

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.