我有一个PHP表单,其中用户可以上传文件的行数未知.为此,我使用二维数组.
$i = 0; ?>
One |
$i++;
?>
Two
$i++;
?>
所有文件应保存在不同的文件夹中.
$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 '
echo '
';echo '
', $val['row_name'], '';echo '
', $val['fileToUpload'], '';echo '
';echo '
';$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.
";
} else {
echo "Sorry, there was an error uploading your file.
";
}
}
}
但实际上它看不到任何文件,并且输出看起来像:
一
抱歉,文件已存在.很抱歉,您的文件尚未上传.
二
抱歉,文件已存在.很抱歉,您的文件尚未上传.
解决方法:
除了评论中的内容外,还需要进行一些更改.主要问题是:
>当PHP中$_POST和$_FILES都是完全分离的实体时,您试图将文件和数据都发布到一个数组中.因此,当这两个超全局变量中实际上有两个行数组时,您最终将尝试访问单个数组行.
>您的$target_file从未声明过,并且$target_dir的斜杠太多.
>项目1使您以错误的方式访问$val.
这些是我提出的最终更正,保留了您自己环境的逻辑.每个部分的说明都在代码内进行了注释.
HTML表格
for($i = 0; $i < $counter; $i++): // do proper counting ?>
File:
PHP Post脚本
$unique_id = "upload"; // just changed to reproduce
$unique_id = $unique_id . '/';
foreach ($_POST['row'] as $val) {
$target_dir = $unique_id;
$target_file = $_FILES['row']['name'][$val]; //actually defining this var
$target_dir = $target_dir . $val. "/"; // no need to index assoc nor '/'
if (!file_exists($target_dir)) {
mkdir($target_dir, 0777, true);
}
$imageFileType = pathinfo($target_dir,PATHINFO_EXTENSION);
// Check if image file is a actual image or fake image
// Check if file already exists
if (file_exists($target_file)) {
die("Sorry, file already exists."); // die if error
}
// Check file size
if ($_FILES['row']['size'][$val] > 900000000) { // using the proper index reference for file
die("Sorry, your file is too large."); //die if error
}
// Check if there are error msg in $_FILES
if ($_FILES['row']['error'][$val] != 0) {
die("Sorry, your file was not uploaded."); // die if error
// if everything is ok, try to upload file
} else {
// point your move_files with the final name of the actual file to be moved
if (move_uploaded_file($_FILES['row']['tmp_name'][$val], $target_dir.'/'.$_FILES['row']['name'][$val])) {
echo "The file ". basename($_FILES['row']['name'][$val]). " has been uploaded.
";
} else {
die("Sorry, there was an error uploading your file.");
}
}
}
最终输出(上传两个虚拟文件后)
Array
(
[row] => Array
(
[0] => 0
[1] => 1
)
[submit] => Upload
)
Array
(
[row] => Array
(
[name] => Array
(
[0] => dummyfile1.docx
[1] => dummyfile2.mpp
)
[type] => Array
(
[0] => application/vnd.openxmlformats-officedocument.wordprocessingml.document
[1] => application/vnd.ms-project
)
[tmp_name] => Array
(
[0] => C:\Windows\Temp\php73DA.tmp
[1] => C:\Windows\Temp\php73DB.tmp
)
[error] => Array
(
[0] => 0
[1] => 0
)
[size] => Array
(
[0] => 0
[1] => 180224
)
)
)
The file dummyfile1.docx has been uploaded.
The file dummyfile2.mpp has been uploaded.
标签:php,arrays,multidimensional-array,file-upload,foreach
来源: https://codeday.me/bug/20191012/1901871.html