Laravel: Calling Artisan command from controller not working properly - laravel

I'm trying to backup my db when I hit a spesific route.
first I made artisan command: php artisan backupDB
here is the function:
echo "Generating backup file..\n";
$filename = "backup-" . Carbon::now()->format('Y-m-d-H-i-s') . ".sql";
$command = "mysqldump --user=" . env('DB_USERNAME') ." --password=" . env('DB_PASSWORD') . " --host=" . env('DB_HOST') . " " . env('DB_DATABASE') . " > " . storage_path() . "/app/backup/" . $filename;
$returnVar = NULL;
$output = NULL;
exec($command, $output, $returnVar);
echo "Backup file path: ".storage_path() . "/app/backup/".$filename;
I run it in cli and it is working fine.
then when I tried to run it via controller:
use Illuminate\Support\Facades\Artisan;
public function backupDB{
Artisan::call("backupDB");
}
when I hit the route it prints the echo, but nothing else happened. just that.

So, I found a solution. wrap the command inside a job or just put the code inside that job, and then start the queue. By doing that the code works fine when I try to hit the route.
php artisan make:job backupDB
it will create a file inside app>jobs folder
then put the code inside handle() function.
next just call the job inside your controller or route
use App\Jobs\backupDB;
public function backup()
{
$process = new pull();
dispatch($process);
}
and you need to setup the queue, here is the docs

You don't need to reinvent the wheel. Just use this extension.
PS: I can't explain the problem you are facing, because you didn't provide any debug info: logs, backtrace, or smth like that.
PS2: Be careful with exec() functions usage

Related

How to run Artisan command without a console

Someone know how to start command without console
Let me explain
After the file is downloaded successfully, you need to run the command php artisan db:seed
I tried this option but I got an error as a result
Maybe someone knows another solution to this problem
I will be very grateful
I tried this option but I got an error as a result
$file = file($request->file->getRealPath());
$fileName = resource_path('upload/csv/accounts/' . date('y-m-d-H-i-s') . '.csv');
$path = file_put_contents($fileName, $file);
return redirect()->route('admin.accounts');
I tried this option
$file = file($request->file->getRealPath());
$fileName = resource_path('upload/csv/accounts/' . date('y-m-d-H-i-s') . '.csv');
$path = file_put_contents($fileName, $file);
return redirect()->route('admin.accounts');
Artisan::call('db:seed');
And this
$file = file($request->file->getRealPath());
$fileName = resource_path('upload/csv/accounts/' . date('y-m-d-H-i-s') . '.csv');
$path = file_put_contents($fileName, $file);
return redirect()->route('admin.accounts')->with(Artisan::call('db:seed'));
I think this option might work. but put artisan call above return. any lines of code after return will be ignored.
$file = file($request->file->getRealPath());
$fileName = resource_path('upload/csv/accounts/' . date('y-m-d-H-i-s') . '.csv');
$path = file_put_contents($fileName, $file);
Artisan::call('db:seed');// <---- this line
return redirect()->route('admin.accounts');

Symfony command finding folder

I am trying to call command using symfony Process and command looks like:
$data = escapeshellarg("public\uploads\post_data.txt");
$url = "www.example.com/";
$process = new Process(['ab -n 10 -p '.$data.' -T application/x-www-form-urlencoded -w '. $url . 'test/send']);
$process->run();
When i call this command from terminal it works fine but when i try to call it using symfony command then i am getting:
The filename, directory name, or volume label syntax is incorrect. This error is windows terminal error as i know and i think the problem is with \ but i have tried to change it to \\, // and / but not helped.
In my point of view, your approach doesnt work, because new Process needs to be defined in a array, where each piece of the shellcmd is a entry in the array.
So for your case, you should have to write
$process = new Process(['ab', '-n', 10, '-p', $data, '-T', 'application/x-www-form-urlencoded', '-w', $url.'test/send']);
Or try with the fromShellCommandline static function
$process = Process::fromShellCommandline('ab -n 10 -p '.$data.' -T application/x-www-form-urlencoded -w '. $url . 'test/send');

GET path not accepting 'dot' in it

I have router path with a '.' in it, my path is downloads/pass.pkpass
I have defined the path in web.php as the following
$router->get('downloads/pass.pkpass', 'PassServerController#downloadPass');
but somehow it's not working. if I remove the '.' it's working fine. what might be the problem here?
It's was working before recently I updated lumen after that it's not working.
The . is the string concatenation operator in PHP.
example
<?php
$string1 = "Test";
$string2 = "working!";
$string = $string1 . $string2;
echo $string;
?>
this will print "Test working!"
You can get your route by this may be
$router->get('downloads/pass.'.'pkpass', 'PassServerController#downloadPass');
But this is some kind of hack we can say.
You can use _ instead of . parameter that will work for sure.

Cannot add line break to file

I am going to make function that writes all activity logs on my webpage. I'm using Codeigniter 3x as the framework. Here is the code.
public function writeLog($loc, $user, $activity) {
$this->load->helper('file');
date_default_timezone_set('Asia/Jakarta');
$files = FCPATH . '/assets/logs/logs-' . date('Y-m-d') . ".log";
$data = date("H:i:s") . " | User: " . $user . " from IP " . $loc . " do activity " . $activity . "\n";
write_file($files, $data, "a");
}
When I'm about to call the function by writeLog("192.168.1.3", "blackjack", "login"), it should be written like this:
15:20:22 | blackjack from IP 192.168.1.3 do activity login
But when I call that function again by writeLog("192.168.1.3", "blackjack", "logout"), the result like this:
15:20:22 | blackjack from IP 192.168.1.3 do activity login15:20:39 | blackjack from IP 192.168.1.3 do activity logout
I have inserted "\n" in the end of the $data, but new line isn't created.
Please help me
Different Operating systems use different characters for line endings. On unix-like systems the line ending is \n (called LF) while windows use \r\n (called CRLF). Most windows programs can display \n but not all.
Best way to go about is to use the PHP_EOL constant and let the server decide what line endings to use.
Extra curiosa: Mac OSX uses LF now days but older mac's used only CR.

fopen with dynamic filename is not working

I need to make a web site on which people can upload data files that after some treatment will be plotted using the jpgraph. The file is analyzed with a bash script called analfile.sh. The bash script is like this:
#!/bin/bash
file=$1
fecha=`date +%s`
mv $1 $fecha.dat
echo $fecha.dat
So, it gives back another file which name is like: 1321290921.dat. That is the file that I need to plot.
This my current php code:
$target_path = "/home/myhome";
$target_path = $target_path . basename( $_FILES['rdata']['name']);
$target_file = basename( $_FILES['rdata']['name']);
if(move_uploaded_file($_FILES['rdata']['tmp_name'], $target_path)) {
echo "The file ". $target_file. " has been uploaded";
chdir('/home/myhome');
$filetoplot=shell_exec('./analfile.sh'.' '.$target_file);
} else{
echo "There was an error uploading the file, please <a href=\"index.html\">try again!
</a>";
}
//$filetoplot="1321290921.dat"
echo "<br>The file to be opened is ". $filetoplot. "<br>";
if ($file_handle = fopen("$filetoplot", 'r')) {
while ( $line_of_text = fgets($file_handle) ) {
$parts = explode('.', $line_of_text);
echo $line_of_text ;
print $parts[0] . $parts[1]. $parts[2]. "<br>";
}
fclose($file_handle);
}
I have permissions to read and write on the target directory. I find strange that if I uncomment the line $filetoplot="1321290921.dat" then the script works perfectly. I guess I am doing something stupid since this is my first code in php but after some hours googling I was not able to find a solution.
Any help will be appreciated.
First thing I'm seeing is that you don't append trailing slash (/) to your home path, so that the path would be like /home/myhomefoo rather than /home/myhome/foo.
You should also move $target_file earlier, and reuse that within $target_path. There's no reason to do the same thing twice.
If that doesn't help, we'll see what goes next.

Resources