I am facing a strange situation with php rename() function. I download a file using API and then moved that file to destination folder.
$download_path = C:\Users\JOHN\AppData\Local\Temp/downloaded_folder/downloaded_file.png $moving_path = C:\xampp\htdocs\project-1/images/downloaded_file.png
Every time downloading is working perfectly and file downloaded
The rename() function is working alternatively. ie on first time it works second time fails. 3rd time ITS works 4th time fails etc.
ie first time file moved . and second time i am getting warning
Warning: rename(C:\Users\JOHN\AppData\Local\Temp/downloaded_folder/downloaded_file.png,C:\xampp\htdocs\project-1/images/downloaded_file.png): Access is denied (code: 5) Warning: copy(C:\xampp\htdocs\project-1/images/downloaded_file.png): Failed to open stream: Permission denied
Please see my code below.
if (file_exists($moving_path)) {
@unlink($moving_path);
}
if (file_exists($download_path) && !file_exists($moving_path)) {
if (!rename($download_path, $moving_path)) {
if (copy ($download_path, $moving_path)) {
echo 'no rename but copied';
} else {
echo 'not moved';
}
} else {
echo 'moved';
}
@rmdir(dirname($download_path))
}
Here what happen is first time it's moved and downloaded_file.png coming inside project-1/images folder & echo moved and second time there is no file coming and echo not moved .
Third time downloaded_file.png coming inside project-1/images folder & echo moved and 4th time there is no file coming and echo not moved.
How to solve this issue. Please help. I seen a similar question but that answer also not working for me