简体   繁体   English

PHP 重命名() function 无法正常工作。 替代访问被拒绝(代码:5)

[英]PHP rename() function not working properly. Alternative access is denied (code: 5)

I am facing a strange situation with php rename() function. I download a file using API and then moved that file to destination folder.我正面临 php rename() function 的奇怪情况。我使用 API 下载文件,然后将该文件移动到目标文件夹。

$download_path = C:\Users\JOHN\AppData\Local\Temp/downloaded_folder/downloaded_file.png $moving_path = C:\xampp\htdocs\project-1/images/downloaded_file.png $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. rename() function 交替工作。 ie on first time it works second time fails.即第一次它工作第二次失败。 3rd time ITS works 4th time fails etc. ITS 第 3 次工作,第 4 次失败等。

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警告:重命名(C:\Users\JOHN\AppData\Local\Temp/downloaded_folder/downloaded_file.png,C:\xampp\htdocs\project-1/images/downloaded_file.png):访问被拒绝(代码:5)警告: copy(C:\xampp\htdocs\project-1/images/downloaded_file.png): 无法打开 stream: 权限被拒绝

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 .这里发生的是第一次它被移动并且 downloaded_file.png 进入 project-1/images 文件夹 & echo moved并且第二次没有文件来并且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 .第三次downloaded_file.png 进入 project-1/images 文件夹并回显移动第四次没有文件出现并且回显未移动

How to solve this issue.如何解决这个问题。 Please help.请帮忙。 I seen a similar question but that answer also not working for me我看到了一个类似的问题,但那个答案也不适合我

It seems that you do not have the correct folder permissions for:您似乎没有正确的文件夹权限:

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

As you have this directory showing up in your error for the rename and the copy functions.因为您在renamecopy功能的错误中显示了此目录。 Set this directory's permissions to 750, for your basic needs.将此目录的权限设置为 750,以满足您的基本需求。

Since you're using windows:由于您使用的是 windows:

Right Click>Properties>Security Tab Change the permissions for your user the script is running under to "Read & execute" and "Write".右键单击>属性>安全选项卡将运行脚本的用户的权限更改为“读取和执行”和“写入”。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM