Reputation: 11
I can't add * to my code to find file
this code work
exec("mediaconvert -t wav -i /home/20220228/11/23401.rec -o /var/www/html/test.mp3");
if i add a the *, it don't work
exec("mediaconvert -t wav -i /home/20220228/11/*01.rec -o /var/www/html/test.mp3");
p.s. in path is only one file, when i try execute this code from shell it work. Pls help me)
Upvotes: 0
Views: 569
Reputation: 4889
Filename expansion and other bash-specific features may/will not work in other shells (e.g. standard POSIX). If your command with *
is not executed in bash/compatible, it won't work as expected. You need to verify the environment/shell that your PHP installation executes commands in.
Run the following test script:
<?php
exec('echo "$SHELL"', $out);
var_dump($out);
When I run the PHP script directly on CLI, I get "/bin/bash"
for the shell that's called. When I run it via browser, curiously I get "/sbin/nologin"
instead. There are different environments for user apache
that executes PHP via browser calls, and the "actual" user logging in via SSH. Bash shell is not available for the Apache user by default.
These results are from a Centos7 server with Apache 2.4/PHP 8.1.4 running. Your mileage may vary. Bottom line: if the command you are executing depends on bash-specific features, it must execute in a bash environment, or another shell that supports the required features.
If bash is not available, your other option is using e.g. glob
to get files matching the pattern in your directory, and then loop over them while executing the command for each specific file.
Edit: As pointed out by @Sammitch (see comments), /sbin/nologin
is a common "shell name" choice for non-login users, and most likely uses /bin/sh
. This should still allow for filename expansion/globbing. Testing browser script call with exec('ls *.php', $out);
the wildcard functions as expected.
You may find this question/answer relevant: Use php exec to launch a linux command with brace expansion.
Upvotes: 1
Reputation: 409
You can try to find files with glob()
function and after that you can use exec(). You can try a similiar solution with the following code:
$input_files = '/home/20220228/11/*01.rec';
foreach (glob($input_files) as $filename) {
exec("mediaconvert -t wav -i " . $filename . " -o /var/www/html/test.mp3");
}
Upvotes: 0
Reputation: 99
I recommend you do the opposite. First, get the files you want to input then you exec. For instance:
$input_files = ...
exec("mediaconvert -t wav -i " . $input_files . " -o /var/www/html/test.mp3");
Upvotes: 0