1

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

John
  • 97
  • 5
  • 2
    Start by taking out the `@` operator so that you can see what error messages PHP might be producing. – Tangentially Perpendicular Apr 08 '22 at 03:09
  • @TangentiallyPerpendicular thank you . Updated the question. I am getting Access is denied (code: 5) message. – John Apr 08 '22 at 03:34
  • PHP doesn't have access to the user's filesystem for security reasons. At least not from the browser. – mardubbles Apr 08 '22 at 03:49
  • Code: 5 is about [permission](https://answers.microsoft.com/en-us/windows/forum/all/system-error-code-5-access-is-denied/0d027ec1-553d-47cd-9be3-d9eed71013e5). However, I see this [old question](https://stackoverflow.com/questions/30077379/php-rename-access-is-denied-code-5) but I'm not sure does it work for you. – vee Apr 08 '22 at 04:12
  • maybe php does not have permission to read file in ```C:\Users\JOHN\AppData\Local\Temp/downloaded_folder/downloaded_file.png``` since it's user's temp folder – Kristian Apr 08 '22 at 04:23
  • but first time it is working. second time getting warning. third time it is working. 4th time getting warning. – John Apr 08 '22 at 04:26
  • @John Try to get [file permission](https://www.php.net/manual/en/function.fileperms.php) and [owner](https://www.php.net/manual/en/function.fileowner.php) data every time and see if they are always same or different when showing warning. – vee Apr 08 '22 at 04:32

1 Answers1

0

It seems that you do not have the correct folder permissions for:

C:\xampp\htdocs\project-1/images/

As you have this directory showing up in your error for the rename and the copy functions. Set this directory's permissions to 750, for your basic needs.

Since you're using windows:

Right Click>Properties>Security Tab Change the permissions for your user the script is running under to "Read & execute" and "Write".