【发布时间】:2020-02-21 03:47:06
【问题描述】:
我有下面的代码试图将多个文件上传到多个目录(年和月)。
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" enctype="multipart/form-data">
<input type="file" name="files[]" id="files" multiple />
<br/>
<input type="submit" name="submit" id="button" value="Backup!" />
</form>
<?php
if(isset($_FILES['files'])){
$year= Date('Y');
$month = Date('m');
$tempFile = $_FILES['files']['tmp_name'];
for ($a=2017; $a <= $year; $a++) {
echo "first<br>";
if($a == 2017){
for ($m=11; $m <= 12; $m++) {
$targetDir = "../".$a."/".$m."/";
echo "second<br>";
foreach ($tempFile as $key => $tmp_name) {
echo "third<br>";
$fileName = $_FILES['files']['name'][$key];
$fileTemp = $_FILES['files']['tmp_name'][$key];
$targetFile = $targetDir.$fileName;
echo $targetFile."<br>";
if(!file_exists($targetFile)){
if(move_uploaded_file($fileTemp, $targetFile)){
echo "uploaded!<br>";
}else {
echo "error<br>";
}
}else {
echo "file already exists in ".$targetDir."<br>";
}
}
}
}
}
}
?>
2017 只有两个目录(11 和 12)。上传在第一个目录(11)中正确完成,但我得到了
echo error 尝试上传到第二个目录 (12) 时。
代码正在做的循环顺序是:
first
second
third
../2017/11/file-to-change.php
uploaded!
second
third
../2017/12/file-to-change.php
error
first
first
我可以将多个文件上传到它获得的第一个目录,但不能将任何内容上传到第二个。我尝试使用另一个for(),但它给我的结果与foreach() 相同。
【问题讨论】:
-
启用错误报告以查看(真正的)错误是什么。
-
它没有显示任何错误
标签: php html file file-upload