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)
CodePudding user response:
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, it will not work as expected. Then, 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. No bash environment there. (There are different environments for user apache
that executes PHP via browser calls, and the "actual" user logging in via SSH. No "proper" shell has been configured for the Apache user.)
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.
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.
You may find this question/answer relevant: Use php exec to launch a linux command with brace expansion.
CodePudding user response:
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");
CodePudding user response:
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");
}