我正在运行一个带有apache版本1.3.33和PHP版本4.4的SCO Unix盒子.我可以通过cli正确执行exec命令,但是在尝试通过浏览器执行脚本时会遇到麻烦.我的设置是:
我的代码:htdocs目录中的test_script.php
<?php
exec('ls',$out,$rval);
echo "Output:<hr />";
print "<pre>"; print_r($out); print "</pre>";
echo "Return Value:<hr />";
echo $rval;
?>
Run Code Online (Sandbox Code Playgroud)
我也尝试过显式设置可执行二进制路径.测试脚本与apache是相同的用户/组.我也尝试在arg中添加2>&1,但看不到任何变化.先感谢您.
我正在用PHP编写数据库迁移脚本,我需要在phpunit中模拟git diff的结果.这个想法是git diff只返回自上次提交以来在includes /中添加或更新的文件的名称.但是,当我正在编写脚本并提交更改时,这当然会不断变化.
这是Migrate类和gitDiff方法:
#!/usr/bin/php
<?php
class Migrate {
public function gitDiff(){
return shell_exec('git diff HEAD^ HEAD --name-only includes/');
}
}
?>
Run Code Online (Sandbox Code Playgroud)
有任何想法吗?
我正在尝试从 php 执行 bash 脚本并实时获取其输出。
我正在应用在这里找到的答案:
然而他们不为我工作。
当我以这种方式调用 .sh 脚本时,它工作正常:
<?php
$output = shell_exec("./test.sh");
echo "<pre>$output</pre>";
?>
Run Code Online (Sandbox Code Playgroud)
然而,当做:
<?php
echo '<pre>';
passthru(./test.sh);
echo '</pre>';
?>
Run Code Online (Sandbox Code Playgroud)
或者:
<?php
while (@ ob_end_flush()); // end all output buffers if any
$proc = popen(./test.sh, 'r');
echo '<pre>';
while (!feof($proc))
{
echo fread($proc, 4096);
@ flush();
}
echo '</pre>';
?>
Run Code Online (Sandbox Code Playgroud)
我的浏览器中没有输出。
我还尝试在这两种情况下调用变量而不是脚本,我的意思是:
<?php
$output = shell_exec("./test.sh");
echo '<pre>';
passthru($output);
echo '</pre>';
?>
Run Code Online (Sandbox Code Playgroud)
这是我的 test.sh 脚本:
#!/bin/bash
whoami
sleep 3
dmesg
Run Code Online (Sandbox Code Playgroud) 我目前在一个运行Apache网络服务器的远程CentOS 5.6系统上运行ssh.我需要使用poppler pdftohtml二进制文件,遗憾的是,该二进制文件当前未安装在该计算机上.所以我下载了poppler包并在我的用户文件夹下构建它.由于我不是系统管理员,我没有做
make install
Run Code Online (Sandbox Code Playgroud)
我的所有编译文件都在
/users/myfolder/poppler-0.18.2/
Run Code Online (Sandbox Code Playgroud)
我需要通过php执行的文件shell_exec()是
/users/myfolder/poppler-0.18.2/utils/pdftohtml
Run Code Online (Sandbox Code Playgroud)
如果我通过ssh bash执行它,我会得到正确的输出.相反,如果我把这一行放在php脚本上:
echo shell_exec("/users/myfolder/poppler-0.18.2/utils/pdftohtml");
Run Code Online (Sandbox Code Playgroud)
我得到以下输出:
sh: /users/myfolder/poppler-0.18.2/utils/pdftohtml: Permission denied
Run Code Online (Sandbox Code Playgroud)
我尝试设置为777文件权限,目前是-rwxrwxrwx.我也注意到使用shell_exec("whoami");"apache"结果.如果文件权限是-rwxrwxrwx,apache是否应该能够执行脚本?
我也知道安装poppler make install会解决问题,但由于这是出于测试目的,我想避免在我的个人文件夹外"污染"系统,直到测试完成.
感谢任何有帮助的人!
特权:hunspell和php5。
来自 bash 的测试代码:
user@host ~/ $ echo 'sagad?j?s' | hunspell -d lv_LV,en_US
Hunspell 1.2.14
+ sagad?ties
Run Code Online (Sandbox Code Playgroud)
- 工作正常。
测试代码(test.php):
$encoding = "lv_LV.utf-8";
setlocale(LC_CTYPE, $encoding); // test
putenv('LANG='.$encoding); // and another test
$raw_response = shell_exec("LANG=$encoding; echo 'sagad?j?s' | hunspell -d lv_LV,en_US");
echo $raw_response;
Run Code Online (Sandbox Code Playgroud)
返回
Hunspell 1.2.14
& sagad 5 0: tagad, sagad?ties, sagaudo, sagand?, sagar?o
*
*
Run Code Online (Sandbox Code Playgroud)
屏幕截图(无法发布带有无效字符的代码):

似乎 shell_exec 无法正确处理 utf-8,或者可能需要一些额外的编码/解码?
编辑:我必须使用 en_US.utf-8 来获取有效数据。
I\xe2\x80\x99m 尝试通过 PHP 将字符串(从 PDO 查询提取到 MYSQL DB)传递shell_exec到 *nix 程序,在本例中为xtide。
一切正常,直到我传递一个包含撇号的字符串。
\n\n这适用于 OSX 上的终端:
\n\nhouse@New-MacBook-Pro:~$ tide -l "Nomans Land, Martha\'s Vineyard, Massachusetts"\nRun Code Online (Sandbox Code Playgroud)\n\n但是完全相同的字符串,从 PDO 查询到 MYSQL DB,并作为变量传递到 shell_exec 中,总是会失败。我如何排列单引号/双引号似乎并不重要。
\n\n运行此命令会添加反斜杠,但仍然失败:
\n\n$tideLocation = mysql_real_escape_string($tideLocation);\nRun Code Online (Sandbox Code Playgroud)\n\n输出:
\n\nNomans Land, Martha\\\xe2\x80\x99s Vineyard, Massachusetts\nRun Code Online (Sandbox Code Playgroud)\n\n失败:
\n\n$output1 = shell_exec("/opt/local/bin/tide -l \'Nomans Land, Martha\'s Vineyard, Massachusetts\'");\n\n$output1 = shell_exec("/opt/local/bin/tide -l \'$tideLocation\'");\nRun Code Online (Sandbox Code Playgroud)\n\n当在以下位置手动设置时,这是有效的shell_exec:
$output1 = shell_exec("/opt/local/bin/tide -l \'Nomans Land, Martha\\\'s Vineyard, …Run Code Online (Sandbox Code Playgroud) 我实际上正在为自己开发一个小项目,是一个 Web 应用程序,它创建证书签名请求以及证书.pem/.crt及其.key.
实际的问题是我正在尝试运行:
shell_exec(openssl ca -config ../openssl.cnf -in $CSR_FILE -out $CRT_FILE)
Run Code Online (Sandbox Code Playgroud)
我发现问题是,运行此命令后要求输入我的 CA 密码,然后回答“是”两次以接受证书的创建。我不知道如何让它发挥作用。我已经被这个问题困扰了近三天,Google 或 Stack Overflow 都没有答案。
我尝试运行该命令并添加另一个命令shell_exec(passphrase),以这种方式传递密码和“y”两次。
shell_exec("openssl....","passphrase","y","y")
Run Code Online (Sandbox Code Playgroud)
非常感谢,我感谢所有的帮助。
大家好!
我有一个名为 start.php 的文件,在这个文件中我将 x 的值设置为 5。我有另一个名为 check.js 的文件
在我的 PHP 文件中,我使用 shell_exec 来运行 check.js
我的问题是,我应该怎么做才能让 check.js 检查 start.php 中 x 的值
在使用 shell_exec 时可以这样做吗?如果不是我该怎么办?
此致
用户通过 POST 提交数据后,我显示一个临时页面并使用 shell_exec 在后台执行该过程,至少这就是我想要做的。
这是我的页面设置:
C:\larragon\www\index.php
<?php
try {
shell_exec("php " . __DIR__ . "/test.php");
} catch(Exception $e) {
echo "Exception: " . $e;
}
?>
Run Code Online (Sandbox Code Playgroud)
C:\larragon\www\test.php
<?php
$myfile = fopen(__DIR__ . "/testfile.txt", "w");
echo "test";
?>
Run Code Online (Sandbox Code Playgroud)
如果我转到 localhost 或 localhost/index.php,第二个脚本不会运行。但是,当我尝试从 cmd 调用这两个脚本时,它们都适用。
php C:\laragon\www\index.php
php C:\laragon\www\test.php
Run Code Online (Sandbox Code Playgroud)
他们都工作并创建一个名为 testfile.txt 的文件
将 Laravel 用于 Web 应用程序,我在其中使用https://github.com/clegginabox/pdf-merger合并 pdf 文件,并在尝试将两个 pdf 文件合并到一起超过 1.4 版时遇到以下错误:
Exception in pdf_parser.php line 133:
This document (C:\path-to-doc\file.pdf) probably uses a compression technique
which is not supported by the free parser shipped with FPDI.
(See https://www.setasign.com/fpdi-pdf-parser for more details)
in pdf_parser.php line 133
Run Code Online (Sandbox Code Playgroud)
我按照以下答案中的建议使用 ghostscript 将任何 1.4 以上的 pdf 转换为 1.4,以便合并工作。
FPDF 错误:此文档 (testcopy.pdf) 可能使用了 FPDI 附带的免费解析器不支持的压缩技术
我已经成功安装了 ghostscript 并将其添加到我的路径中。为了测试,我有一个名为 1.5 版的示例 pdf 文件,test.pdf我从 Windows 终端运行以下命令:
gswin64 -sDEVICE=pdfwrite -dCompatibilityLevel=1.4 -dNOPAUSE -dQUIET -dBATCH -sOutputFile=new-file.pdf test-file.pdf
我得到了我的 …
php ×10
shell-exec ×10
apache ×2
exec ×2
shell ×2
bash ×1
ghostscript ×1
git ×1
git-diff ×1
hunspell ×1
node.js ×1
openssl ×1
pdf ×1
pdf-to-html ×1
permissions ×1
phpunit ×1
poppler ×1
ssl ×1
system ×1
utf-8 ×1