I have a php form with unknown number of rows where user can upload files. For this purpose I use two dimensional array.
<form action="upload_file_module.php" method="post" enctype="multipart/form-data">
<? $i = 0; ?>
<table>
<tr>
<td>
<input type="hidden" name="row[<? echo $i; ?>][row_name]" value = "one"/> One
</td>
<td>
<input type="file" name="row[<? echo $i; ?>][fileToUpload]" >
</td>
<?
$i++;
?>
</tr>
<tr>
<td>
<input type="hidden" name="row[<? echo $i; ?>][row_name]" value = "two"/> Two
</td>
<td>
<input type="file" name="row[<? echo $i; ?>][fileToUpload]" >
</td>
<?
$i++;
?>
</tr>
</table>
<input type="submit" value="Upload" name="submit">
All files should be saved in different folders.
<?php
$unique_id = "folder";
$unique_id = $unique_id . '/';
foreach ( $_POST['row'] as $val ) {
$target_dir = $unique_id;
$target_dir = $target_dir . "/" . $val. "/";
if (!file_exists($target_dir)) {
mkdir($target_dir, 0777, true);
}
echo '<table>';
echo '<tr>';
echo '<td>', $val['row_name'], '</td>';
echo '<td>', $val['fileToUpload'], '</td>';
echo '</tr>';
echo '</table>';
$target_file = $target_dir . basename($_FILES[$val['fileToUpload']]["name"]);
$uploadOk = 1;
$imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);
// Check if image file is a actual image or fake image
// Check if file already exists
if (file_exists($target_file)) {
echo "Sorry, file already exists.";
$uploadOk = 0;
}
// Check file size
if ($_FILES["fileToUpload"]["size"] > 900000000) {
echo "Sorry, your file is too large.";
$uploadOk = 0;
}
// Check if $uploadOk is set to 0 by an error
if ($uploadOk == 0) {
echo "Sorry, your file was not uploaded.";
// if everything is ok, try to upload file
} else {
if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file)) {
echo "The file ". basename( $_FILES["fileToUpload"]["name"]). " has been uploaded.<br>";
} else {
echo "Sorry, there was an error uploading your file.<br>";
}
}
}
But in fact it doesn't see any files, and output looks like:
one
Sorry, file already exists.Sorry, your file was not uploaded.
two
Sorry, file already exists.Sorry, your file was not uploaded.
var_dumpyour$_FILESafter the upload to see its structure?