ファイルシステム権限
あなたのケースでスクリプトが不十分なfile system permissionsに実行されていない可能性があります。 Linuxでは、あなたは、ファイルが実行可能ビット、現在のユーザー、またはユーザーのグループ、例えばのために設定している場合だけ、ファイルへのパスを書き込むことによって、ファイルを実行することが許可されています。それ以外の場合は
~/scripts/abc.php
./script.sh
、あなたが渡す必要があります適切なプログラムの引数としてファイル、例えば:
php ~/scripts/abc.php
/usr/bin/php ./script.php
bash ./script.sh
あなたは、例えば、ls
、またはstat
コマンドでファイルシステムのアクセス権を確認することができます。
$ stat --format='perm: %A | user: %U | group: %G' 1.sh
perm: -rwxr-xr-x | user: ruslan | group: users
$ ls -l 1.sh
-rwxr-xr-- 1 ruslan users 1261 Nov 26 11:47 1.sh
上記の例では、ユーザruslan
およびグループusers
に対して実行可能ビットが設定されていますが、以外のビットは(r--
)に設定されていません。他のファイルはのファイルを読み取って(r
)にすることはできますが、実行したり、ファイルに書き込んだりすることはできません。実行可能なビットがshell_exec
関数は標準出力記述子に書き込まれた内容を返しchmod
コマンド、例えば:
# For the user
chmod u+x file.sh
# For the group
chmod g+x file.sh
コマンドの実行をより良く制御
を使用し設定する
。コマンドが何も印刷しないか、失敗した場合はNULL
を返します。たとえば:
$out = shell_exec("ls -l inexistent-file");
var_dump($out); // NULL
だからshell_exec
とエラー条件に対して何ら良好な制御を持っていません。私の代わりにprop_open
を使用することをお勧め:
$cmd = 'ls -l 1.sh inexistent-file';
// Descriptors specification
$descriptors = [
1 => ['pipe', 'w'], // standard output
2 => ['pipe', 'w'], // standard error
];
// Open process for the command
$proc = proc_open($cmd, $descriptors, $pipes);
if (!is_resource($proc))
die("failed to open process for $cmd");
if ($output = stream_get_contents($pipes[1])) {
echo "output: $output\n";
}
fclose($pipes[1]);
if ($errors = stream_get_contents($pipes[2])) {
fprintf(STDERR, "Errors: %s\n", $errors);
}
fclose($pipes[2]);
// Close the process
if (($status = proc_close($proc)) != 0)
fprintf(STDERR, "The command failed with status %d\n", $status);
シェバングは
実行可能なスクリプトのためのshebangを使用することを検討してください。例:
#!/bin/bash -
#!/usr/bin/php
動画はどこにありますか?あなたは 'chdir( '../ videos')'できませんか? – David
私はそれがWebルートディレクトリにある問題だと思っているはずはありません。 – TrueBlue