-1
Oops, all right?! I have the code below to upload files to several directories with years/ months:
<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 "primeiro<br>";
if($a == 2017){
for ($m=11; $m <= 12; $m++) {
$targetDir = "../".$a."/".$m."/";
echo "segundo<br>";
foreach ($tempFile as $key => $tmp_name) {
echo "terceiro<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 has only two directories (11 and 12). The upload happens exactly for the month 11 but enter the echo erro
when it arrives in the directory of the month 12.
The looping sequence you’re doing is this:
primeiro
segundo
terceiro
../2017/11/file-to-change.php
uploaded!
segundo
terceiro
../2017/12/file-to-change.php
erro
primeiro
primeiro
I tried to use another for()
instead of foreach()
but it was the same thing. I can upload to the first directory but the second one doesn’t work. Could you help me? Thanks in advance.
Worst I ever tried. But then it doesn’t take the month/directory 12.
– A52