CentOS Linux console 命令与 PHP exec(command)
CentOS Linux console command versus PHP exec(command)
我有一个我写的C程序叫convert3to5
,最初是在2010年初为CentOS / Fedora 32位系统编写的。我将它移动到新的CentOS 6.x 64位系统主机上。
从 CentOS Putty 控制台我可以 运行 convert3to5
命令就好了;这是我的控制台 运行ning 的示例:
[root@cloud convert3to5]# ls
CircleStar convert3to5 Convert3To5.txt test.tif
[root@cloud convert3to5]# ./convert3to5 /var/www/webadmin/data/www/mydomain.com/uploads/SV-DIS160217B.tif
TIFFReadDirectory: Warning, /var/www/webadmin/data/www/mydomain.com/uploads/SV-DIS160217B.tif: wrong data type 7 for "RichTIFFIPTC"; tag ignored. Image has an undefined fillorder - using default: MSB2LSB
上面是 convert3to5 的正常完成,我得到一个 SV-DIS160217B.bmp
放在 /var/www/webadmin/data/www/mydomain.com/uploads/
所以 运行 从控制台运行它很好.
问题 - 我正在尝试 运行 使用 exec(command, output, return)
命令从 PHP 执行完全相同的命令,如下所示:
chdir($sv_path.$c3to5_path); //change our working directory to "/convert3to5" directory
$command = "./convert3to5 $targetFile 2>&1";
$result = exec($command, $output, $return);
// the output of the above command - is a .bmp file it will be placed in the same path as the input .tif file
我得到以下 $result:
ERROR: Unable to convert
/var/www/webadmin/data/www/mydomain.com/uploads/SV-DIS160217B.tif to 5
color BMP file: Open file Error: Tiff_3_to_BMP_5_.lut!
我的convert3to5确实需要打开Tiff_3_to_BMP_5_.lut
为什么当我从控制台提示 运行 convert3to5
而不是 PHP exec(...) 时它找到 Tiff_3_to_BMP_5_.lut
在这两种情况下我的密码都显示我在
[root@cloud convert3to5]# pwd
/var/www/webadmin/data/www/mydomain.com/myView/convert3to5
我还在
之后从我的 PHP 脚本中验证了 pwd 是正确的
chdir($sv_path.$c3to5_path);
Tiff_3_to_BMP_5_.lut 在 CircleStar 目录中 - CircleStar 的路径是 /var/www/webadmin/data/www/mydomain.com/myView/convert3to5/CircleStar
总结:./convert3to5 有效,而 PHP exec('convert3to5 ..) 似乎无效。
任何人都可以提出不同之处以及如何修复 and/or 调试吗?
谢谢
您运行正在从 convert3to5 目录连接控制台,我怀疑您的旧 C 程序使用了 .lut 文件的相对路径,可能相对于 .tif?
如果您在控制台示例中这样做了会怎样
cd ../..
./path/to/convert3to5/convert3to5 /var/www/webadmin/data/www/mydomain.com/uploads/SV-DIS160217B.tif
可能与 $targetFile 有关。打印出来看看是不是完整路径。
最后,运行
/full/path/to/convert3to5 fullTargetPath
如果可行,那么作为解决方法,如果您只是这样做 exec('/full/path/to/convert3to5 $fullTargetPath, ..)
它应该像控制台一样运行。
根据我上面对馄饨的评论:
我在控制台上运行以 root 用户身份(拥有完全特权)。我假设我的 PHP 脚本将 运行 作为服务器上的 "apache" 用户?
这是我认为的问题所在:我查看了 Tiff_3_to_BMP_5_.lut
文件所在的 CircleStar 目录权限。 CircleStar 有 rw-r--r--
(0644) 当 运行 从控制台以 root 身份运行时,这允许我的 convert3to5 程序找到并打开 Tiff_3_to_BMP_5_.lut
文件就好了。但是,一旦我将 CircleStar 的特权更改为 rwxr-xr-x
(0755) PHP exec(...) 运行 就可以了!
所以最终这是一个权限问题。
我有一个我写的C程序叫convert3to5
,最初是在2010年初为CentOS / Fedora 32位系统编写的。我将它移动到新的CentOS 6.x 64位系统主机上。
从 CentOS Putty 控制台我可以 运行 convert3to5
命令就好了;这是我的控制台 运行ning 的示例:
[root@cloud convert3to5]# ls
CircleStar convert3to5 Convert3To5.txt test.tif
[root@cloud convert3to5]# ./convert3to5 /var/www/webadmin/data/www/mydomain.com/uploads/SV-DIS160217B.tif
TIFFReadDirectory: Warning, /var/www/webadmin/data/www/mydomain.com/uploads/SV-DIS160217B.tif: wrong data type 7 for "RichTIFFIPTC"; tag ignored. Image has an undefined fillorder - using default: MSB2LSB
上面是 convert3to5 的正常完成,我得到一个 SV-DIS160217B.bmp
放在 /var/www/webadmin/data/www/mydomain.com/uploads/
所以 运行 从控制台运行它很好.
问题 - 我正在尝试 运行 使用 exec(command, output, return)
命令从 PHP 执行完全相同的命令,如下所示:
chdir($sv_path.$c3to5_path); //change our working directory to "/convert3to5" directory
$command = "./convert3to5 $targetFile 2>&1";
$result = exec($command, $output, $return);
// the output of the above command - is a .bmp file it will be placed in the same path as the input .tif file
我得到以下 $result:
ERROR: Unable to convert /var/www/webadmin/data/www/mydomain.com/uploads/SV-DIS160217B.tif to 5 color BMP file: Open file Error: Tiff_3_to_BMP_5_.lut!
我的convert3to5确实需要打开Tiff_3_to_BMP_5_.lut
为什么当我从控制台提示 运行 convert3to5
而不是 PHP exec(...) 时它找到 Tiff_3_to_BMP_5_.lut
在这两种情况下我的密码都显示我在
[root@cloud convert3to5]# pwd
/var/www/webadmin/data/www/mydomain.com/myView/convert3to5
我还在
之后从我的 PHP 脚本中验证了 pwd 是正确的chdir($sv_path.$c3to5_path);
Tiff_3_to_BMP_5_.lut 在 CircleStar 目录中 - CircleStar 的路径是 /var/www/webadmin/data/www/mydomain.com/myView/convert3to5/CircleStar
总结:./convert3to5 有效,而 PHP exec('convert3to5 ..) 似乎无效。
任何人都可以提出不同之处以及如何修复 and/or 调试吗? 谢谢
您运行正在从 convert3to5 目录连接控制台,我怀疑您的旧 C 程序使用了 .lut 文件的相对路径,可能相对于 .tif?
如果您在控制台示例中这样做了会怎样
cd ../..
./path/to/convert3to5/convert3to5 /var/www/webadmin/data/www/mydomain.com/uploads/SV-DIS160217B.tif
可能与 $targetFile 有关。打印出来看看是不是完整路径。
最后,运行
/full/path/to/convert3to5 fullTargetPath
如果可行,那么作为解决方法,如果您只是这样做 exec('/full/path/to/convert3to5 $fullTargetPath, ..)
它应该像控制台一样运行。
根据我上面对馄饨的评论:
我在控制台上运行以 root 用户身份(拥有完全特权)。我假设我的 PHP 脚本将 运行 作为服务器上的 "apache" 用户?
这是我认为的问题所在:我查看了 Tiff_3_to_BMP_5_.lut
文件所在的 CircleStar 目录权限。 CircleStar 有 rw-r--r--
(0644) 当 运行 从控制台以 root 身份运行时,这允许我的 convert3to5 程序找到并打开 Tiff_3_to_BMP_5_.lut
文件就好了。但是,一旦我将 CircleStar 的特权更改为 rwxr-xr-x
(0755) PHP exec(...) 运行 就可以了!
所以最终这是一个权限问题。