【问题标题】:excute shell by php on xampp error在xampp错误上通过php执行shell
【发布时间】:2017-08-07 16:24:48
【问题描述】:

我想使用代码在 localhost 上备份一个表。

$dbhost = '127.0.0.1';
$dbuser = 'root';
$dbpass = '';
$dbname = 'snapmall';
$dbtable = 'tb_translation';
$dbpath = 'D:test/new/';

//mysqldump -u root -p snapmall ps_translation > D:/test/new/ps_translation.sql
$command = "mysqldump -u $dbuser -p $dbpass $dbname $dbtable > ".$dbpath.$dbtable.".sql";

shell_exec($command);

我在 xampp 的 shell 上有测试命令,它工作正常,但是当我通过命令 shell_exec() 运行它时,结果文件为空。

【问题讨论】:

    标签: php mysql shell


    【解决方案1】:

    试试这个,添加 2>&1 以获取命令的输出:

    $output = shell_exec($command." 2>&1");
    

    然后在这里发布。

    您需要能够通过之前调用 xamp 来执行您的命令,因为您的 exec 正在访问 bash,而不是 xampp,因此,换句话说,您的 exec 必须可以访问一组基本命令,配置您的 PATH多变的。

    【讨论】:

    • 结果为空,但我尝试过: exec('ls -l 2>&1', $output); print_r($输出);并收到消息:数组([0] => 'ls' 未被识别为内部或外部命令,[1] => 可运行的程序或批处理文件。)
    • 所以,你的 PHP exec 不能在 shell 上工作,你需要改变你的 PATH 变量并且能够执行任何命令,当你这样做时,它会工作;)
    • 我运行命令:mysqldump -u root -p leo_snapmall ps_translation > D:/www/prestashop/ps_translation.sql 通过 cmd 可以找到但我的代码不起作用:exec('mysqldump -u root - p leo_snapmall ps_translation > D:/www/prestashop/ps_translation.sql 2>&1'); => 错误信息:'mysqldump' 不是内部或外部命令、可运行程序或批处理文件。
    • 您的 exec 不知道您的任何命令,您需要在 PHP 配置中指定它。
    • 您需要能够通过调用 xamp 之前执行您的命令,因为您的 exec 正在访问 bash,而不是 xampp
    猜你喜欢
    • 1970-01-01
    • 2018-08-07
    • 2023-03-11
    • 1970-01-01
    • 2013-08-24
    • 1970-01-01
    • 2014-11-04
    • 2023-03-28
    • 1970-01-01
    相关资源
    最近更新 更多