12

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 3

27

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.

Sign up to request clarification or add additional context in comments.

4 Comments

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?
@aalaap See if there is something in $process->getErrorOutput()
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.
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();
5

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

welcome to 2019! Are these functions deprecated for Laravel 5.8?
-1
$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

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.