PCHomepage
Programmer
I have a simple form for uploading multiple files to the file system:
... and a script for processing the upload:
It all works as needed so far but I also want to grab the file names and insert them into the database. I've tried various things including this but all I seem to be able to get is the last file name so I'm sure I just must something obvious that needs another pair of eyes to spot or should I be doing it as part of the code above?
Any ideas? Thanks in advance.
Code:
<form method="POST" enctype="multipart/form-data" action="fileupload.php">
<p>CSV / XML / XMLX files only:<br>
<input type="file" name="userfile[]" /><br>
<input type="file" name="userfile[]" /><br>
<input type="file" name="userfile[]" /><br>
<input type="submit" value="Upload All" />
</form>
... and a script for processing the upload:
Code:
foreach ($_FILES["userfile"]["error"] as $key => $error) {
if ($error == UPLOAD_ERR_OK) {
$tmp_name = $_FILES["userfile"]["tmp_name"][$key];
$name = $_FILES["userfile"]["name"][$key];
move_uploaded_file($tmp_name, "$FileUploadPath/$name");
}
}
It all works as needed so far but I also want to grab the file names and insert them into the database. I've tried various things including this but all I seem to be able to get is the last file name so I'm sure I just must something obvious that needs another pair of eyes to spot or should I be doing it as part of the code above?
Code:
//check if a file has been submitted, insert name into database
if(isset($_FILES['userfile']['tmp_name'])) {
// loop through the array of files //
for($i=0; $i < count($_FILES['userfile']['tmp_name']);$i++) {
if (is_valid_file_size($_FILES['userfile']['size'][$i])
&& is_uploaded_file($_FILES['userfile']['tmp_name'][$i]) ) {
$Filename .= $_FILES[$i];
// mysqli_query("INSERT INTO file_uploads(FileName) VALUES('$FileName')");
echo $FileName;
} else {
$warning = "Error uploading file(s).";
}
}
}
Any ideas? Thanks in advance.