【问题标题】:Copy files on windows server using PHP使用 PHP 在 Windows 服务器上复制文件
【发布时间】:2016-09-24 09:51:28
【问题描述】:

这里有一个具体的问题。我正在用 PHP 开发一个自动化脚本,以帮助在客户想要演示站点设置的情况下创建演示 Web 应用程序。

我正在使用运行 Plesk 的专用服务器。

我的目的是创建一个新的子域,创建一个新的数据库,从其他地方复制数据库,从另一个文件夹复制站点文件,最后通过电子邮件向客户发送登录凭据等。

我正在使用 Plesk API RPC 创建子域、数据库和数据库用户,这一切都运行良好。我有数据库从其他地方复制模式,并且我的电子邮件部分正在工作。唯一让我无法理解的部分是将文件从一个文件夹复制到另一个文件夹。

源文件夹与目标文件夹位于同一“httpdocs”文件夹中。我最初遇到的问题是我已经纠正的 open_basedir 问题,但现在我遇到了权限被拒绝的问题。

我知道我不能对 windows 进行 chmod。 我试过通过 exec() 使用 xcopy,它返回

string(13) "Access denied"

我也尝试过 cacls 和 icacls,两者都给了我类似的错误

string(57) "Successfully processed 0 files; Failed processing 1 files"

没有授予整个 httpdocs 文件夹写入权限,我不知道如何最好地进一步解决这个问题。任何建议/帮助将不胜感激。

【问题讨论】:

  • 您能分享一下您尝试使用 xcopy/icacls 达到的目标吗?

标签: php windows permissions plesk


【解决方案1】:

这个脚本适合我:

<?php

echo(system('xcopy /Y /Z "C:\Inetpub\vhosts\example.tld\httpdocs\index.html" "C:\Inetpub\vhosts\example.tld\httpdocs\index2.html"'));

你可以使用 xcopy:

C:\Inetpub\vhosts\example.tld\httpdocs>xcopy index.html index5.html
Does index5.html specify a file name
or directory name on the target
(F = file, D = directory)? F
C:index.html
1 File(s) copied

但并非在所有情况下:

C:\Inetpub\vhosts\example.tld\httpdocs>xcopy /O index.html index4.html
Does index4.html specify a file name
or directory name on the target
(F = file, D = directory)? F
Access denied
0 File(s) copied

你也可以使用icacls:

C:\Inetpub\vhosts\example.tld\httpdocs>icacls index3.html /grant ftp3:(F)
processed file: index3.html
Successfully processed 1 files; Failed processing 0 files

您甚至可以禁用继承:

C:\Inetpub\vhosts\example.tld\httpdocs>icacls index4.html /inheritance:r
processed file: index4.html
Successfully processed 1 files; Failed processing 0 files

【讨论】:

  • 您好,感谢您的回复,如上所述,我已经通过 PHP 尝试了这些方法,但它们似乎不起作用。我会再试一次,以防我做错了什么,让你知道我的进展情况。
  • 我已经在订阅系统用户(不是管理员)和 PHP(检查答案中的脚本)下执行了这个命令,所以从 PHP 使用 xcopy 没有任何限制。
  • 虽然由于访问被拒绝,我仍然无法让 xcopy 工作,但我确实设法接受了你的回答,并在使用 icacls 调整权限之前稍微调整了一下权限,然后使用 xcopy 复制文件夹.对于任何尝试使用类似功能的人,请在下面找到我所做的示例;
【解决方案2】:

我设法使用以下代码解决了我的困境;

    system('icacls "C:\Inetpub\vhosts\domain.ltd\httpdocs\destination_subdomain" /grant:r "USER_USERNAME":(OI)(CI)F');
    system('xcopy /y /z /e "C:\Inetpub\vhosts\domain.ltd\httpdocs\source_subdomain" "C:\Inetpub\vhosts\domain.ltd\httpdocs\destination_subdomain\"');
    system('icacls "C:\Inetpub\vhosts\domain.ltd\httpdocs\destination_subdomain" /grant:r "USER_USERNAME":(OI)(CI)RX');

第一行设置目标文件夹上所选用户名的完全权限。

第二行使用 xcopy 将所有文件夹和子文件夹,甚至是源文件夹中的空文件夹复制到目标文件夹。

第三行将目标文件夹的权限重置为只读和执行。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-10-13
    • 2015-08-12
    • 1970-01-01
    • 2022-12-09
    • 2014-07-11
    • 1970-01-01
    相关资源
    最近更新 更多