PHP upload files to multiple directories

Asked

Viewed 83 times

-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.

2 answers

0


I had to think differently. Fix me if I’m wrong but I believe that in the second looping ended up losing the source of the file since it had been saved/moved in the first directory chosen and at the time of the second the file was no longer available.

So I upload it to the first directory and after that I make copies of this guy to the other directories:

foreach ($tempFile as $key => $tmp_name) {

    $fileName = $_FILES['files']['name'][$key];
    $fileTemp = $_FILES['files']['tmp_name'][$key];
    $targetFile = $targetDir.$fileName;

    if(!file_exists($targetFile)){
        if(move_uploaded_file($fileTemp, $targetFile)){
            echo "Upload feito em: ".json_encode($targetDir, JSON_UNESCAPED_SLASHES)."<br>";
        }else {
            echo "Erro de upload em: ".json_encode($targetDir, JSON_UNESCAPED_SLASHES)."<br>";
        }
    }else {
        echo "Arquivo já existe na pasta ".json_encode($targetDir, JSON_UNESCAPED_SLASHES)."<br>";
    }
}

for ($a=2017; $a <= $ano; $a++) {
    if($a == 2017){
        for ($m = 11; $m <= 12; $m++) {
            $targetDirToCopy = "../".$a."/".$m."/".$fileName;
            if(copy($targetFile, $targetDirToCopy)){
                echo "Copiado para: ".json_encode($targetDirToCopy, JSON_UNESCAPED_SLASHES)."<br>";
            }else {
                echo "Arquivo já existe na pasta ".json_encode($targetDirToCopy, JSON_UNESCAPED_SLASHES)."<br>";
            }

        }
    }
    if($a == 2018){
        for ($m = 1; $m <= 12; $m++) {
            $targetDirToCopy = "../".$a."/".$m."/".$fileName;
            if(copy($targetFile, $targetDirToCopy)){
                echo "Copiado para: em".json_encode($targetDirToCopy, JSON_UNESCAPED_SLASHES)."<br>";
            }else {
                echo "Arquivo já existe na pasta ".json_encode($targetDirToCopy, JSON_UNESCAPED_SLASHES)."<br>";
            }

        }
    }
    if($a == $ano){
        for ($m = 1; $m < ($mes-1); $m++) {
            $targetDirToCopy = "../".$a."/".$m."/".$fileName;
            if(copy($targetFile, $targetDirToCopy)){
                echo "Copiado para: em".json_encode($targetDirToCopy, JSON_UNESCAPED_SLASHES)."<br>";
            }else {
                echo "Arquivo já existe na pasta ".json_encode($targetDirToCopy, JSON_UNESCAPED_SLASHES)."<br>";
            }

        }
    }
}

0

I think the problem lies in this following excerpt:

for ($m=11; $m <= 12; $m++) {
            $targetDir = "../".$a."/".$m."/";
            echo "segundo<br>";


This error has already happened to me several times when I needed to do repeat commands. try to do so:

for ($m=11; $m < 12; $m++) {
            $targetDir = "../".$a."/".$m."/";
            echo "segundo<br>";


I know it’s a little rough, but it always worked for me

  • Worst I ever tried. But then it doesn’t take the month/directory 12.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.