How to run Artisan command without a console - laravel

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');

Related

Read text from .txt file started from other line php laravel

how can i read file from input file txt from certain line, example from line prefix AT on php example
can u help to continue my script
`
$data = $request->file('file');
$filetmp = $data->getRealPath();
$readfile = file_get_contents($filetmp);
$files = fopen($filetmp,"r");
$filedata = fread($files,filesize($filetmp));
fclose($files);
dd($filedata);
$file=$request->file('file');
$content=File::get($file->getRealPath());
$lines = explode("\n", $content);
$lines=array_slice($lines,
array_keys(
array_filter($lines,
function($item){
return strpos($item,'AT') ;
}))[0]
);

Laravel: Calling Artisan command from controller not working properly

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

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');

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.

MediaWiki cannot get path

My MediaWiki cannot get the path of my live web server. Here is the code:
$script = $_SERVER['SCRIPT_NAME'];
$path = pathinfo( $script, PATHINFO_DIRNAME ) . '/';
$path = str_replace( '//', '/', $path );
$ext = pathinfo( $script, PATHINFO_EXTENSION );
echo "Please enter";
The localhostwebserver displays the echo as: Please enter,
but the live server displays the echo as Please enter
How is that possible? Need help!
If you need the url of the current page in php, I can recommend this script. I have used it successfully for years.

Resources