Read text from .txt file started from other line php laravel - 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]
);

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

Spider a website and retrieve all links that contain a keyword

How do I make a Bash script that will copy all links (non-download website). The function is only to get all the links and then save it in a txt file.
I've tried this code:
wget --spider --force-html -r -l1 http://somesite.com | grep 'Saving to:'
Example: there are download links within a website (for example, dlink.com), so I just want to copy all words that contain dlink.com and save it into a txt file.
I've searched around using Google, and I found none of it useful.
Using a proper parser in Perl:
#!/usr/bin/env perl -w
use strict;
use LWP::UserAgent;
use HTML::LinkExtor;
use URI::URL;
my $ua = LWP::UserAgent->new;
my ($url, $f, $p, $res);
if(#ARGV) {
$url = $ARGV[0]; }
else {
print "Enter an URL : ";
$url = <>;
chomp($url);
}
my #array = ();
sub callback {
my($tag, %attr) = #_;
return if $tag ne 'a'; # we only look closer at <a href ...>
push(#array, values %attr) if $attr{href} =~ /dlink\.com/i;
}
# Make the parser. Unfortunately, we don’t know the base yet
# (it might be diffent from $url)
$p = HTML::LinkExtor->new(\&callback);
# Request document and parse it as it arrives
$res = $ua->request(HTTP::Request->new(GET => $url),
sub {$p->parse($_[0])});
# Expand all URLs to absolute ones
my $base = $res->base;
#array = map { $_ = url($_, $base)->abs; } #array;
# Print them out
print join("\n", #array), "\n";

How can I check if stdin exists in PHP ( php-cgi )?

Setup and Background
I am working on script that needs to run as /usr/bin/php-cgi instead /usr/local/bin/php and I'm having trouble checking for stdin
If I use /usr/local/bin/php as the interpreter I can do something like
if defined('STDIN'){ ... }
This doesn't seem to work with php-cgi - Looks to always be undefined. I checked the man page for php-cgi but didn't find it very helpful. Also, if I understand it correctly, the STDIN constant is a file handle for php://stdin. I read somewhere that constant is not supposed to be available in php-cgi
Requirements
The shebang needs to be #!/usr/bin/php-cgi -q
The script will sometimes be passed arguments
The script will sometimes receive input via STDIN
Current Script
#!/usr/bin/php-cgi -q
<?php
$stdin = '';
$fh = fopen('php://stdin', 'r');
if($fh)
{
while ($line = fgets( $fh )) {
$stdin .= $line;
}
fclose($fh);
}
echo $stdin;
Problematic Behavior
This works OK:
$ echo hello | ./myscript.php
hello
This just hangs:
./myscript.php
These things don't work for me:
Checking defined('STDIN') // always returns false
Looking to see if CONTENT_LENGTH is defined
Checking variables and constants
I have added this to the script and run it both ways:
print_r(get_defined_constants());
print_r($GLOBALS);
print_r($_COOKIE);
print_r($_ENV);
print_r($_FILES);
print_r($_GET);
print_r($_POST);
print_r($_REQUEST);
print_r($_SERVER);
echo shell_exec('printenv');
I then diff'ed the output and it is the same.
I don't know any other way to check for / get stdin via php-cgi without locking up the script if it does not exist.
/usr/bin/php-cgi -v yields: PHP 5.4.17 (cgi-fcgi)
You can use the select function such as:
$stdin = '';
$fh = fopen('php://stdin', 'r');
$read = array($fh);
$write = NULL;
$except = NULL;
if ( stream_select( $read, $write, $except, 0 ) === 1 ) {
while ($line = fgets( $fh )) {
$stdin .= $line;
}
}
fclose($fh);
Regarding your specific problem of hanging when there is no input: php stream reads are blocking operations by default. You can change that behavior with stream_set_blocking(). Like so:
$fh = fopen('php://stdin', 'r');
stream_set_blocking($fh, false);
$stdin = fgets($fh);
echo "stdin: '$stdin'"; // immediately returns "stdin: ''"
Note that this solution does not work with that magic file handle STDIN.
stream_get_meta_data helped me :)
And as mentioned in the previous answer by Seth Battin stream_set_blocking($fh, false); works very well 👍
The next code reads data from the command line if provided and skips when it's not.
For example:
echo "x" | php render.php
and php render.php
In the first case, I provide some data from another stream (I really need to see the changed files from git, something like git status | php render.php.
Here is an example of my solution which works:
$input = [];
$fp = fopen('php://stdin', 'r+');
$info = stream_get_meta_data($fp);
if (!$info['seekable'] && $fp) {
while (false !== ($line = fgets($fp))) {
$input[] = trim($line);
}
fclose($fp);
}
The problem is that you create a endless loop with the while($line = fgets($fh)) part in your code.
$stdin = '';
$fh = fopen('php://stdin','r');
if($fh) {
// read *one* line from stdin upto "\r\n"
$stdin = fgets($fh);
fclose($fh);
}
echo $stdin;
The above would work if you're passing arguments like echo foo=bar | ./myscript.php and will read a single line when you call it like ./myscript.php
If you like to read more lines and keep your original code you can send a quit signal CTRL + D
To get parameters passed like ./myscript.php foo=bar you could check the contents of the $argv variable, in which the first argument always is the name of the executing script:
./myscript.php foo=bar
// File: myscript.php
$stdin = '';
for($i = 1; $i < count($argv); i++) {
$stdin .= $argv[$i];
}
I'm not sure that this solves anything but perhaps it give you some ideas.

Warning: implode() [<a href='function.implode'>function.implode</a>]: Invalid arguments passed in C:\wamp\www\test.php on line 71

i try to convert csv file to tsv using below code.
in my csv the first word has no value.
when i run the code it generate tsv file correctly , but it gives above error. please help
Thanks
$myfile = "file path";
function convert($filename)
{
if(#$fh_in = fopen("{$filename}.csv","r"))
{
$fh_out = fopen("{$filename}.tsv","a");
while(!feof($fh_in))
{
$line = array();
$line = fgetcsv($fh_in,1024);
fwrite($fh_out,implode("\t",$line)."\n");
}
fclose($fh_in);
fclose($fh_out);
}
else {
echo "File doesn’t exist\n";
return false;
}
echo "Conversion completed!\n";
return true;
}
convert($myfile);
According to PHP Manual, implode can be
string implode ( string $glue , array $pieces )
string implode ( array $pieces )
So in your case if $line is empty then it will execute with second prototype and consider \n as $pieces which is wrong. So check for value in $line before calling implode

Converting a code from bash to php

I currently have an existing code in bash that greps a keyword from a config file:
[USER1]
usrcid = 5654654654
usrsid = XDFDFSAS22
usrmid = COMPANYNAME1
usrsrt = secret1
urlenc = http://www.url1.com
[USER2]
usrcid = 5654654667
usrsid = XDFDFSAS45
usrmid = COMPANYNAME2
usrsrt = secret2
urlenc = http://www.url2.com
I store it as a variable and use it for processing the rest of the script. What I want to achieve is to convert the behavior from bash to php and do a curl:
F1=/etc/config/file.txt
CID=`grep "\[USER1\]" -A 5 $F1 | grep usrcid | awk {'print$3'}`
SID=`grep "\[USER1\]" -A 5 $F1 | grep usrsid | awk {'print$3'}`
MID=`grep "\[USER1\]" -A 5 $F1 | grep usrmid | awk {'print$3'}`
SRT=`grep "\[USER1\]" -A 5 $F1 | grep usrsrt | awk {'print$3'}`
URI=`grep "\[USER1\]" -A 5 $F1 | grep urlenc | awk {'print$3'}`
echo $CID $SID $MID $SRT $URI
I'm really not a php guru so please excuse the code below but from a general perspective, the below code is my understanding of what I want to achieve:
<?php
include "/etc/config/file.txt"
// *** the equivalent code grep? ***
function get_data($url)
{
$ch = curl_init();
$timeout = 5;
curl_setopt($ch,CURLOPT_URL,$url);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch,CURLOPT_CONNECTTIMEOUT,$timeout);
$data = curl_exec($ch);
curl_close($ch);
return $data;
}
// *** i'm not sure if this one is correct? ***
$returned_content = get_data('$URI/cid=$CID&sid=$SID&mid=$MID&srt=$SRT')
echo $returned_content;
?>
This is my first time to ask in stackoverflow so I would like to thank you in advance!
Include doesn't do what you think it's doing. It won't get the variables you set in the text-file. If it were PHP code in the file you included, it would evaluate that, but in this case, it's only text. See the Manual
What you need is to use the parse_ini_file() function. It takes the config file as first argument, and a boolean flag as the second. The second argument is used to let the function know that you should use sections in your config file, which you do.
Example:
file.txt:
[USER1]
usrcid = 5654654654
usrsid = XDFDFSAS22
usrmid = COMPANYNAME1
usrsrt = secret1
urlenc = http://www.url1.com
[USER2]
usrcid = 5654654667
usrsid = XDFDFSAS45
usrmid = COMPANYNAME2
usrsrt = secret2
urlenc = http://www.url2.com
test.php:
<?php
$config = parse_ini_file("file.txt", true);
print_r($config);
?>
(See the manual for parse_ini_file())
This will load the config file to the $config variable, and it will contain the following:
Array
(
[USER1] => Array
(
[usrcid] => 5654654654
[usrsid] => XDFDFSAS22
[usrmid] => COMPANYNAME1
[usrsrt] => secret1
[urlenc] => http://www.url1.com
)
[USER2] => Array
(
[usrcid] => 5654654667
[usrsid] => XDFDFSAS45
[usrmid] => COMPANYNAME2
[usrsrt] => secret2
[urlenc] => http://www.url2.com
)
)
Now, to construct an URL you could use:
$url = "{$config['USER1']['urlenc']}/cid={$config['USER1']['usrcid']}&sid={$config['USER1']['usrsid']}&mid={$config['USER1']['usrmid']}&srt={$config['USER1']['usrsrt']}";
Or construct a dynamic way of iterating through the array given in the $config variable, to account for several sections. This URL you can run through the cURL function you got.

Resources