gpt4 book ai didi

Windows 上的 PHP exec - php cwd 是一个 Windows 网络共享

转载 作者:可可西里 更新时间:2023-11-01 10:38:24 26 4
gpt4 key购买 nike

这是我的第一篇文章,如果措辞或结构不当,我想提前致歉。

我正在使用带有 IIS 7.5 和 PHP 5.3 的 Windows7 家庭高级版我在 php 中执行以下代码,但它不起作用。 exec 命令返回 1 和一个空数组。

$path = "\\\\somecomputer\\somepath\\afolder";
chdir($path);
$cmd = "pushd $path";
exec("echo off & $cmd & \"c:/bfolder/somexecutable.exe\" -flag1 -flag2 \"$inputfile\" > outputfile.log", $retary, $retval);
print_r($reary);
print $retval;

但是,如果我在 exec 调用之前没有 chdir 到网络路径,那么一切正常。似乎当 php cwd 设置为网络路径时,从那时起启动的任何 exec 都会失败。

总而言之,我需要 c:\afolder\win32\pdftotext.exe 使用 exec 从 PHP 运行并从网络共享读取它的输入文件并在 Windows 网络位置上写入它的输出。

最佳答案

我不确定我是否完全理解您的问题,但我突然想到一些可能会有所帮助的事情。

运行 iis 的帐户(应用程序池身份和/或身份验证)是否可以访问网络共享?尝试使用真实用户而不是系统帐户。

可否直接在exec调用中使用unc路径作为参数(是否需要调用chdir())?

你的 print_r($reary) 应该是 print_r($reary)

将“2>&1”添加到在 shell 上执行的命令的末尾,通常会返回一些对调试有用的输出。例如,这是我们用于在 windows 命令行上执行的方法:

    /**
* Execute the command - remember to provide path to the command if it is not in the system path
*
* @param string $command The command to execute through the Windows command line
* @param string &$output This is any output returned from the command line
* @return int The return code 0 normally indicates success.
*/
static public function Execute($command, &$output)
{
$out = array();
$ret = -1;

exec($command." 2>&1",$out,$ret);

foreach($out as $line)
{
$output.= $line;
}
return $ret;
}

使用如下:

$output = '';
if(WindowsUtiliy::Execute('shell command', $output) != 0)
{
die($output);
}

对不起,如果我没捕获要点!

关于Windows 上的 PHP exec - php cwd 是一个 Windows 网络共享,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12464124/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com