使用 PHP 在 windows 服务器上复制文件

Copy files on windows server using PHP

这里遇到了一些具体问题。我正在 PHP 中开发自动化脚本,以在客户需要演示站点设置时帮助创建演示 Web 应用程序。

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

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

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

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

我知道我不能用 windows 进行 chmod。 我试过通过 exec() 使用 xcopy returns

string(13) "Access denied"

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

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

由于没有授予整个 httpdocs 文件夹写权限,我对如何最好地进一步解决这个问题感到有些茫然。任何 advice/help 将不胜感激。

这个脚本适合我:

<?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

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

    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 将所有文件夹和子文件夹,甚至是空文件夹从源文件夹复制到目标文件夹。

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