简体   繁体   中英

PHP upload files to multiple directories

I have the code bellow trying to upload multiple files to multiple directories (years and 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 "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 has only two dirs (11 and 12). The upload is done correctly in the first one dir (11) but I get the echo error when it tries to upload to the second dir (12).

The loop sequency that the code is doing is:

first
second
third
../2017/11/file-to-change.php
uploaded!
second
third
../2017/12/file-to-change.php
error
first
first

I can upload multiple files to the first dir it gets but cannot upload anything to the second. I tried using another for() but it gives me the same result than foreach() .

Please correct me if I'm wrong but I think that in the second looping the file was not available anymore after It was uploaded to the first dir.

So I upload it to the first dir and then I copy it to the others 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 "Uploaded in: ".json_encode($targetDir, JSON_UNESCAPED_SLASHES)."<br>";
        }else {
            echo "Error uploading in: ".json_encode($targetDir, JSON_UNESCAPED_SLASHES)."<br>";
        }
    }else {
        echo "File already exists in ".json_encode($targetDir, JSON_UNESCAPED_SLASHES)."<br>";
    }
}

for ($a=2017; $a <= $year; $a++) {
    if($a == 2017){
        for ($m = 11; $m <= 12; $m++) {
            $targetDirToCopy = "../".$a."/".$m."/".$fileName;
            if(copy($targetFile, $targetDirToCopy)){
                echo "Uploaded in: ".json_encode($targetDirToCopy, JSON_UNESCAPED_SLASHES)."<br>";
            }else {
                echo "File already exists in ".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 "Uploaded in:".json_encode($targetDirToCopy, JSON_UNESCAPED_SLASHES)."<br>";
            }else {
                echo "File already exists in ".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 "Uploaded in:".json_encode($targetDirToCopy, JSON_UNESCAPED_SLASHES)."<br>";
            }else {
                echo "File already exists in ".json_encode($targetDirToCopy, JSON_UNESCAPED_SLASHES)."<br>";
            }

        }
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM