【发布时间】:2015-12-30 15:17:34
【问题描述】:
我有一个用户可以上传文件的行数未知的 php 表单。为此,我使用了二维数组。
<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">
所有文件都应保存在不同的文件夹中。
<?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>";
}
}
}
但实际上它没有看到任何文件,并且输出看起来像:
一个
抱歉,文件已存在。抱歉,您的文件未上传。
两个
抱歉,文件已存在。抱歉,您的文件未上传。
【问题讨论】:
-
那我们调试一下吧。您是否能够: 1) 制作目录? 2)给它它的权限? 3) 实际上传 tmp 文件夹的文件(还没有移动它)?
-
1.是的。我能够创建文件夹。 2. 经许可一切正常。 3. 我没有将它们上传到 tmp 文件夹。问题是我无法从 $_FILES[$val['fileToUpload']]["name"] 获取它们,因为 $val['fileToUpload'] 是空的。
-
上传后你
var_dump你的$_FILES看它的结构了吗? -
@AlanMachado 是的。这里的样子:imgur.com/1Dhj6Rd
-
是的。我得到了问题的原因。非常感谢! @AlanMachado
标签: php arrays file-upload multidimensional-array foreach