简体   繁体   English

shell_exec()总是返回一个空字符串

[英]shell_exec() always returning an empty string

I am trying to create a server within my Android phone. 我正在尝试在Android手机中创建服务器。 I am unable to execute any shell script from my PHP code. 我无法从我的PHP代码执行任何Shell脚本。

Here's the code: 这是代码:

//index.php
<?php
$output=shell_exec("sdcard/htdocs/myscript.sh 2>&1");
if(!$output){
  echo "Failed";
}else{
  echo $output;
}
?>

//myscript.sh
cd sdcard/htdocs/images
ls -t1 | head -n 1

The script works fine within terminal emulator. 该脚本在终端仿真器中运行良好。 I also tried changing permissions of the script file but that didn't work. 我还尝试了更改脚本文件的权限,但这没有用。 I don't know if it requires superuser permissions to execute shell scripts within PHP code. 我不知道它是否需要超级用户权限才能在PHP代码中执行Shell脚本。

The whole code is used to return the filename of the last file created in the images directory. 整个代码用于返回在images目录中创建的最后一个文件的文件名。

Need suggestions to make this code work.Is there any other way to perform the required job? 需要使该代码正常工作的建议。还有其他方法可以执行所需的工作吗?

Your code looks fine. 您的代码看起来不错。 Superuser permission is not necessary for script execution. 脚本执行不需要超级用户权限。 You should turn on PHP error output or check the PHP error log file. 您应该打开PHP错误输出或检查PHP错误日志文件。 I bet you find the reason there. 我敢打赌,你在那里找到原因。 If not, recheck the directory, and file permissions: 如果没有,请重新检查目录和文件权限:

./index.php
./sdcard/htdocs/myscript.sh
./sdcard/htdocs/images

sdcard and sdcard/htdocs require executable persmissions. sdcardsdcard/htdocs需要可执行权限。 sdcard/htdocs/images requires executable and read permission ( ls in myscript.sh ), and so does sdcard/htdocs/myscript.sh . sdcard/htdocs/images需要可执行和读取权限(在myscript.sh ls ),因此sdcard/htdocs/myscript.sh也是如此。 But I guess it's something else because permission errors should be displayed ( 2>&1 ). 但是我想这是另一回事,因为应该显示权限错误( 2>&1 )。


Edit 编辑

You can find the last modified file with PHP, no need to run another process. 您可以使用PHP找到最后修改的文件,而无需运行其他进程。 Take one of these two: 采取以下两个之一:

$images = glob('sdcard/htdocs/images/*');
$images = array_combine(array_map('filemtime', $images), $images);
asort($images);
echo $lastModifiedImage = end($images);

Or with some fewer array operations: 或使用更少的数组操作:

$images = glob('sdcard/htdocs/images/*');
array_reduce($images, function($previous, $element) use (&$found) {
    $mtime = filemtime($element);
    $found = $previous < $mtime ? $found : $element;
    return $previous < $mtime ? $mtime : $previous;
}, 0);
echo $found;

Make sure what you have run 确保您已运行

chmod a+x sdcard/htdocs/myscript.sh

on your file. 在您的文件上。

Also $output is not a boolean. 同样,$ output不是布尔值。

sdcard and sdcard/htdocs require executable persmissions. sdcard和sdcard / htdocs需要可执行权限。 sdcard/htdocs/images requires executable and read permission (ls in myscript.sh), and so does sdcard/htdocs/myscript.sh. sdcard / htdocs / images需要可执行和读取权限(myscript.sh中的ls),sdcard / htdocs / myscript.sh也需要。 But I guess it's something else because permission errors should be displayed (2>&1). 但是我想这是另一回事,因为应该显示权限错误(2>&1)。

Probably FAT ! 大概是FAT!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM