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 . "
";
$stringData .= "Uploaded Files:
";
$stringData .= $file; // prints nothing?
fwrite($fh, $stringData);
fclose($fh);
However, nothing is $file
is printing nothing.
How can I print the file info for each file that is chosen from the user into my text file?
Date: Todays Date
Files: 2 file(s)
File: someimage1.png
Size: 10KB
File: someimage1.png
Size: 20KB
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']."
"; // return blank
$stringData .= " Size: ".$file['size']."
"; // return blank
}
Also, I am using jQuery File Upload Demo.
Use $files = $_FILES["files"];
to get the uploaded file information.
You can use loop in the $_FILES
array:
foreach($files as $file){
$stringData .= "File: ".$file['name']."
";
$stringData .= "Size: ".$file['size']."
";
}
make sure you use move_uploaded_file() because you only store them in /tmp
folder and you will lose them.
Use
$files = $_FILES['files'];
echo "<pre>";
print_r($files);
instead of
$files = $_POST["files"];
Please try using $_FILES
as follows instead of $_POST['files']
:
print_r($_FILES['files'])