dtvgo28624 2013-09-03 11:11
浏览 459

为什么shell_exec不执行文件但执行简单命令?

Is there any reason why I can not complied files in PHP's shell_exec/exec/system function?

Example of something that does work in command line and PHP's shell_exec function:

<?php
$data = shell_exec("ls");
echo $data;
?>

Example of something that does not work in PHP's shell_exec function but will work in command line (I can confirm that):

<?php
$data = shell_exec("./c-compiled-file argv1 argv2 argv3");
echo $data;
?>

Is there anything I can do on my server so this will work? I've looked everywhere and no solutions I found fixed the problem. The compiled file is in the same directory as the PHP script as well, it just won't execute it. Also if you're asking, yes I have tried this with SSH2 and it still will not execute.

Also PHP is not in safe mode and NO functions are disabled.

  • 写回答

3条回答 默认 最新

  • duanfan1965 2013-09-03 11:16
    关注

    You want to use system in the second case, and not shell_exec.

    system executes an external program and displays the output.

    shell_exec executes a command via shell and returns the complete output as a string.

    and for good measure:

    exec simply executes an external program.

    Furthermore you want to make sure your external program is executable and (though you have stated it, I'll restate this) has execute permissions for the user which is running the web server. You also want to make sure the directory your external program is running in has the ability to write to its directory or /tmp or whatever output directory you have set.

    Finally you should always use absolute paths for executing things like this in cron or php or whatever... so don't use ./c-compiled-file argv1 argv2 argv3, but instead use /home/username/c-compiled-file argv1 argv2 argv3 or whatever the full path is.

    评论

报告相同问题?

悬赏问题

  • ¥50 如何增强飞上天的树莓派的热点信号强度,以使得笔记本可以在地面实现远程桌面连接
  • ¥15 MCNP里如何定义多个源?
  • ¥20 双层网络上信息-疾病传播
  • ¥50 paddlepaddle pinn
  • ¥20 idea运行测试代码报错问题
  • ¥15 网络监控:网络故障告警通知
  • ¥15 django项目运行报编码错误
  • ¥15 请问这个是什么意思?
  • ¥15 STM32驱动继电器
  • ¥15 Windows server update services