Is it possible to call Shell commands (e.g for converting images ) from Laravel controller? If yes then how. I have searched on internet. Nothing seems relevant.
3 Answers
You can use the Process component provided by Symfony: http://symfony.com/doc/current/components/process.html
The component is used by Laravel itself, so your don't need to install it via composer separately. Just add use Symfony\Component\Process\Process; to your file.
If you use it(instead of php's exec() function), you'll be able to unit test the code that calls shell commands.
4 Comments
aalaap
The Process component only seems to work with basic commands such as
ls, uname and even man, but it doesn't seem to work with executables such as ffmpeg. There is no output. What am I doing wrong?Emil M
@aalaap See if there is something in
$process->getErrorOutput()Kumanan
I found the solution on ffmpeg No output issue by removing empty characters in between the shell command. `` 'ffmpeg -i "concat:test1.mp3|test2.mp3|test3.mp3" -acodec copy' merged_file.mp3 `` Remove the space characters in between the concat and the file.
Tyler Collier
Just to clarify, "The component is used by Laravel itself, so it can be loaded directly" means you don't need to install it separately. You can already do (e.g.)
use Symfony\Component\Process\Process; $process = new Process(['ls', '-lsa']); $process->run();It all depends on what operating system you are using. php already has a few functions to execute shell commands.
Laravel has a build in task runner for ssh commands.
1 Comment
victorf
welcome to 2019! Are these functions deprecated for Laravel 5.8?
$FilenamePending = "files".csv";
if (File::exists(public_path('downloads/files/wstock/'.$FilenamePending))){
$PathFilesWip= public_path('downloads/files/wstock/'.$FilenamePending);
//$getCommand= "aws s3 cp $PathFiles s3://bucket-name/share/in/test/Transaction/";
$getCommandPending= "aws s3 cp $PathFilesWip s3://store_stocks/";
}else{
$getCommandPending= "";
}
$schedule->exec($getCommandPending) ->timezone('Asia/Kolkata')->dailyAt('00:38')->appendOutputTo(storage_path().'/logs/laravel_output.log');
1 Comment
Community
As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.