move all files in a folder to another?

前端 未结 11 1976
一个人的身影
一个人的身影 2020-12-01 04:01

when moving one file from one location to another i use

rename(\'path/filename\', \'newpath/filename\');

how do you move all files in a fol

11条回答
  •  野趣味
    野趣味 (楼主)
    2020-12-01 04:08

    So I tried to use the rename() function as described and I kept getting the error back that there was no such file or directory. I placed the code within an if else statement in order to ensure that I really did have the directories created. It looked like this:

    $tempDir = '/home/site/images/tmp/';
    $permanentDir = '/home/site/images/' . $claimid; // this was stored above
    mkdir($permanentDir,0775);
    if(is_dir($permanentDir)){
        echo $permanentDir . ' is a directory';
        if(is_dir($tempDir)){
            echo $tempDir . ' is a directory';
        }else{
            echo $tempDir . ' is not a directory';
        }
    }else{
        echo $permanentDir . ' is not a directory';
    }
    
    rename($tempDir . "*", $permanentDir);
    

    So when I ran the code again, it spit out that both paths were directories. I was stumped. I talked with a coworker and he suggested, "Why not just rename the temp directory to the new directory, since you want to move all the files anyway?"

    Turns out, this is what I ended up doing. I gave up trying to use the wildcard with the rename() function and instead just use the rename() to rename the temp directory to the permanent one.

    so it looks like this.

    $tempDir = '/home/site/images/tmp/';
    $permanentDir = '/home/site/images/' . $claimid; // this was stored above
    mkdir($permanentDir,0775);
    
    rename($tempDir, $permanentDir);
    

    This worked beautifully for my purposes since I don't need the old tmp directory to remain there after the files have been uploaded and "moved".

    Hope this helps. If anyone knows why the wildcard doesn't work in the rename() function and why I was getting the error stating above, please, let me know.

提交回复
热议问题