Search code examples
phpdirectoryfile-rename

PHP rename not working


Simple rename() function not working on server.

$val = "dir/1.txt";
$rval = "dir2/2.txt";
$test=rename($val, $rval);
if($test){
    echo "<br>Rename working";
}
else{
    echo "<br>Rename not working";
}

I am actually trying to move file to different directory using rename. What could be the reason?


Solution

  • rename returns false on failure. This could be for any number of reasons in your code.

    • The destination directory does not exist
    • The user running the process does not have the required privileges to write to the destination
    • Source file does not exist
    • If you are running PHP < 5.3.1 and trying to move the file across drives in Windows this is not supported

    etc...