Php rename () Access denied. (code: 5)

So, I am trying to use the rename function in php.

On the first try, if the destination folder is empty or does not contain directories with the same name as the original folder, the rename function works fine. However, if there is the same directory name, it fails. I want to just overwrite it, and I thought that rename () would be sufficient.

Here is my code:

/** * Move temp folders to their permanent places * * $module_folder = example (violator, pnp, etc) * $folders = name of folders within module_folder **/ public function move_temp_to_permanent($module_folder, $folders){ $bool = false; $module_folder_path = realpath(APPPATH . '../public/resources/temps/' . $module_folder); $module_folder_destination_path = $_SERVER['DOCUMENT_ROOT'] . '/ssmis/public/resources/photos/' . $module_folder . '/'; foreach($folders as $folder){ $bool = rename($module_folder_path . '/' . $folder, $module_folder_destination_path . $folder); } return $bool; } 

There is an error in the above code:

Message: rename (C: \ XAMPP \ HTDOCS \ ssmis \ Public \ Resources \ Temps \ intruder / SJ-VIOL-2015-0002, C: / XAMPP / HTDOCS / ssmis / state / resources / photo / intruder / SJ-VIOL- 2015- 0002): Access denied. (code: 5)

I use CodeIgniter as a framework.

Thank you very much!

+6
source share
2 answers

If it is on Windows, this can be read in the reports:

rename () definitely does not comply with the agreement on renaming * nix to WinXP with PHP 5. If $ newname exists, it will return FALSE and $ oldname, and $ newname will remain in its original state. Instead, you can do something like this:

 function rename_win($oldfile,$newfile) { if (!rename($oldfile,$newfile)) { if (copy ($oldfile,$newfile)) { unlink($oldfile); return TRUE; } return FALSE; } return TRUE; } 

Link

+9
source

You add an extra / path to make it like

 /** * Move temp folders to their permanent places * * $module_folder = example (violator, pnp, etc) * $folders = name of folders within module_folder **/ public function move_temp_to_permanent($module_folder, $folders){ $bool = false; $module_folder_path = realpath(APPPATH . '../public/resources/temps/' . $module_folder); $module_folder_destination_path = $_SERVER['DOCUMENT_ROOT'] . '/ssmis/public/resources/photos/' . $module_folder . '/'; foreach($folders as $folder){ $bool = rename($module_folder_path . $folder, $module_folder_destination_path . $folder); } return $bool; } 

You can also track the path you prepared to check whether it is right or not.

0
source

Source: https://habr.com/ru/post/986653/


All Articles