If your PHP script is running on Pantheon hosting , you might be noticed that PHP rename function is not working. In PHP rename function is used to rename a file or directory and to move any file or directory.
PHP rename function fails on Pantheon Hosting
Pantheon server is configured in such way where os command mv
is not available. Files and directories on Pantheon’s file serving infrastructure cannot be moved or renamed. Read about this problem on Pantheon’s doc
Solution
To rename or move a file/directory Pantheon suggest to copy file to new location/name and delete old one.
I have written a PHP class Custom_FS_Functions
including a custom rename method. Download the class from GitHub
How to use this class and rename function
Create custom PHP function my_custom_rename
that will call the rename
method from Custom_FS_Functions
class. Use this method only when your script is running on Pantheon hosting.
Example code
<?php
require_once "class-custom-fs-functions.php";
function my_custom_rename( $oldname, $newname ){
$success = rename( $oldname, $newname );
if( !$success )
{
Custom_FS_Functions::rename( $oldname, $newname );
}
}
//use rename function
my_custom_rename( $file_or_dir_path , $new_file_or_dir_path );
?>