code was working, but suddenly stopped working - php

This code opens a text file, then checks to see if each word in the text file
Exists in a another large 2MB dictionary file.
If it does exist, it stores the line from the dictionary file into a variable.
The code was working, but then began to generate Server 500 errors, and now
It only lists about 7 matches and then loads nothing forever.
It used to list the 1000's of matches and then stop.
$file_handle = fopen("POSdump.txt", "r");
while (!feof($file_handle)) {
$line = fgets($file_handle);
$words= explode(" ", $line );
foreach ($words as $word) {
$word = preg_replace('#[^\w+>\s\':-]#', ' ', $word);
$subwords= explode(" ", $word );
$rawword = $subwords[0];
$poscode = $subwords[1];
$rawword = strtoupper($rawword);
$handle = fopen("dictionary.txt","r"); //
if ($handle) {
while (!feof($handle))
{
$buffer = fgets($handle, 4096);
if (preg_match('#\b'.$rawword.'\b#',$buffer)) {
echo $rawword;
echo "</br>";
}
}
}
}
}
?>

Try closing the file when you are done.

This seems to be a memory_limit error. use ini_set('memory_limit', -1) before starting the process.

Related

How to find multiple lines from file with PHP?

I'm writing a PHP script to search for a few lines in a pcap file. This pcap file will be piped through tail -> PHP.
I need to find a few lines like (Host: www.google.com) or (Domain: amazon.com) etc..
I'm new with PHP and struggling to get this code working, the actual output of all the fetched data need to be inserted into a SQL DB. I've used regex to filter out the binary stuff from the pcap.
I've tried multiple loops like the wile, foreach, for, but I'm not getting the clue how to do this in my script.
The code that I have so far is:
<?php
$handle = fopen('php://stdin', 'r');
$line = fgets ($handle, 1000);
$search1 = 'Location';
$search2 = 'Host:';
$search3 = 'User';
$search4 = 'Cookie';
$search5 = 'Domain:';
$matches = array();
$regex = '/[^a-zA-Z0-9\s\D\#$%^&*()+=\-\[\]\';,.\/{}|":<>?~\\\\]/';
if ($handle){
while ($handle) {
$buffer = fgets($handle);
if(strpos($buffer, $search1) !== FALSE) {
$res = preg_replace($regex, "", $buffer);
$matches[] = $res;
print_r($res). "\n";
}
}
fclose($handle);
}
?>
I've read many posts on the internet, but couldn't find any solution or I've not enough knowledge about PHP to get this done. Can anyone help me with this?
If it's working for first then loop it think about algorithm always
$handle = fopen('php://stdin', 'r');
$line = fgets ($handle, 1000);
$search = ['Location','Host:','User','Cookie','Domain:'];
$matches = array();
$regex = '/[^a-zA-Z0-9\s\D\#$%^&*()+=\-\[\]\';,.\/{}|":<>?~\\\\]/';
if ($handle){
while ($handle) {
$buffer = fgets($handle);
foreach($search as $seek){
if(strpos($buffer, $seek) !== FALSE) {
$res = preg_replace($regex, "", $buffer);
$matches[] = $res;
print_r($res). "\n";
}
}
}
fclose($handle);
}
?>

PHP - while loop (!feof()) isn't outputting/showing everything

I am trying to read (and echo) everything of a .txt-File.
This is my code:
$handle = #fopen("item_sets.txt", "r");
while (!feof($handle))
{
$buffer = fgets($handle, 4096);
$trimmed = trim($buffer);
echo $trimmed;
}
This is my "item_sets.txt": http://pastebin.com/sxapZGuW
But it doesn't echo everything (and changing how much it shows depending on if and how many characters i echo after it). var_dump() shows me that the last string is never finished printing out. That looks like this:
" string(45) ""[cu_well_tra. But if I put an
echo "whateverthisisjustarandomstringwithseveralcharacters";,
my last output lines look like this:
" string(45) ""[cu_well_traveled_ak47]weapon_ak47" "1"
" string(5) "}
"
Basically my code isn't printing/echoing all of what it should or at least not showing it.
Thanks in advance :)
Thats because your test for EOF is before you output your last read
Try this with the test for EOF as part of the reading process
<?php
$line_count = 0;
$handle = fopen("item_sets.txt", "r");
if ($handle) {
while (($buffer = fgets($handle, 4096)) !== false) {
$trimmed = trim($buffer);
echo $trimmed;
$line_count++;
}
} else {
echo 'Unexpected error opening file';
}
fclose($handle);
echo PHP_EOL.PHP_EOL.PHP_EOL.'Lines read from file = ' . $line_count;
?>
Also I removed the # infront of the fopen its bad practice to ignore errors, and much better practice to look for them and deal with them.
I copied your data into a file called tst.txt and ran this exact code
<?php
$handle = fopen('tst.txt', 'r');
if ($handle) {
while (($buffer = fgets($handle, 4096)) !== false) {
$trimmed = trim($buffer);
echo $trimmed;
}
} else {
echo 'Unexpected error opening file';
}
fclose($handle);
And it generated this output ( just a small portion shown here )
"item_sets"{"set_community_3"{"name" "#CSGO_set_community_3""set_description" "#CSGO_set_community_3_desc""is_collection"
And the last output is
[aa_fade_revolver]weapon_revolver" "1"
Which is the last entry in the data file

PHP opening a large file

What's the best way to read a .TXT file (The file size is 225mb). I want to open the file and loop thru it and find information via REGEX.
Example data in the file :
00424333060001410100100BILLLLOYD BRRUSSELL & 12675 MAKALISO AVE WEST WORKS TOWN KS 23456-1035 3341310350630200500004200000001887800001789IWD QM1214200400003367250001799900001287IWD QM 000000000000000000000000000000 000000000000000000000000000000
The problem I am having is the file taking forever to open. And to search thru takes a while. My loop could have 75 items I need to search.
$name2 = "BILLLLOYD BRRUSSELL ";
$RE21 = "/[0-9]{23}.$name2/";
$file = fopen("MYFILE.TXT", "r");
while(!feof($file)){
$line = fget($file);
for ($row = 0; $row = 75; $row++{
$name2 = data i am getting from another file...;
$RE21 = "/[0-9]{23}.$name2/"; //Not sure if this works!!
$a = preg_match($RE21, $line, $matches);
foreach($matches as $x => $x_value) {
I will $x_value and store it.} //$x_value should be 00424333060001410100100BILLLLOYD BRRUSSELL
} //foreach
} //for
}//while
fclose($file);
Maybe you should try a different approach and use command line grep? Generate the regexps from your "another file" and the execute a grep command using your generated pattern and the file you want to search?
Use the -o flag to only get your matches from the result
You can read it line by line:
$handle = fopen("bla.txt", "r");
while (($buffer = fgets($handle, 4096)) !== false) {
// ...
}
fclose($handle);

How to open file with special characters in the name

In PHP, how can I open a file that has special characters in the name?
The name is similar to iPad|-5542fa5501f31.log
Per another forum, I've tried:
$logid = str_replace(" ", "\x20", $_GET['logid']);
$logid = str_replace("|", "\x7C", $logid);
To massage the name, but that doesn't work for me either.
I've also already tried:
$dst_file = escapeshellarg($dst_file);
And of course started out with just straight:
$logid = $_GET['logid'];
The initial file was created by a PHP script on a Linux system. I'm confused why a PHP script can write a file name like that, but can't open it for reading.
Here's my current code:
$logid = str_replace(" ", "\x20", $_GET['logid']);
$logid = str_replace("|", "\x7C", $logid);
$logdate = str_replace("-", "/", $_GET['date'])."/";
$dst_file = $uploads_dir.$logdate.$logid.'.log';
// read the data from the log file
echo "<pre>\n";
if (file_exists($dst_file)) {
$file_handle = fopen($dst_file, "r");
while (!feof($file_handle)) {
$line = fgets($file_handle);
if (strlen($line) < 3) continue;
echo $line;
}
fclose($file_handle);
} else {
echo $dst_file." does not exist\n";
}
echo "</pre>\n";
The only thing I found was to rename the file then open. The problem is no PHP functions so I tried system commands. None worked. The ftp RNTO command will rename it. But it would be much better to filter out "special characters" when the file is saved.
Somebody on another forum had suggested using popen, so I tried this, and it works:
$logid = $_GET['logid'];
$logdate = str_replace("-", "/", $_GET['date'])."/";
$dst_file = escapeshellarg($uploads_dir.$logdate.$logid.'.log');
echo "<pre>\n";
$handle = popen("cat ".$dst_file, 'r');
while(!feof($handle)) {
$line = fgets($handle);
if (strlen($line) > 3) {
echo $line;
}
ob_flush();
flush();
}
pclose($handle);
echo "</pre>\n";
The exact same code, except using fopen/fclose (no cat) will not work.
I was struggling to open the file "2017-09-19_Comisión_Ambiental.jpg", that "ó" was giving me this error :
file_get_contents ... failed to open stream: No such file or directory in ...
So I used utf8_decode on the filename and it worked :
$data = file_get_contents( utf8_decode( $filename ) );
I know this is an old question, there's even an accepted answer, I just posted it because it might help somebody.

create multiple directories using loop in php

I am taking data from text file( data is: daa1 daa2 daa3 on separate lines) then trying to make folders with exact name but only daa3 folders is created. Also when i use integer it creates all folders, same is the case with static string i.e "faraz".
$file = __DIR__."/dataFile.txt";
$f = fopen($file, "r");
$line =0;
while ( $line < 5 )
{
$a = fgets($f, 100);
$nl = mb_strtolower($line);
$nl = "checkmeck/".$nl;
$nl = $nl."faraz"; // it works for static value i.e for faraz
//$nl = $nl.$a; // i want this to be the name of folder
if (!file_exists($nl)) {
mkdir($nl, 0777, true);
}
$line++;
}
kindly help
use feof function its much better to get file content also line by line
Check this full code
$file = __DIR__."/dataFile.txt";
$linecount = 0;
$handle = fopen($file, "r");
$mainFolder = "checkmeck";
while(!feof($handle))
{
$line = fgets($handle);
$foldername = $mainFolder."/".trim($line);
//$line is line name daa1,daa2,daa3 etc
if (!file_exists($foldername)) {
mkdir($foldername, 0777, true);
}
$linecount++;
unset($line);
}
fclose($handle);
output folders
1countfaraz
2countfaraz
3countfaraz
Not sure why you're having trouble with your code, but I find it to be more straightforward to use file_get_contents() instead of fopen() and fgets():
$file = __DIR__."/dataFile.txt";
$contents = file_get_contents($file);
$lines = explode("\n", $contents);
foreach ($lines as $line) {
$nl = "checkmeck/". $line;
if (!file_exists($nl)) {
echo 'Creating file '. $nl . PHP_EOL;
mkdir($nl, 0777, true);
echo 'File '. $nl .' has been created'. PHP_EOL;
} else {
echo 'File '. $nl .' already exists'. PHP_EOL;
}
}
The echo statements above are for debugging so that you can see what your code is doing. Once it is working correctly, you can remove them.
So you get the entire file contents, split it (explode()) by the newline character (\n), and then loop through the lines in the file. If what you said is true, and the file looks like:
daa1
daa2
daa3
...then it should create the following folders:
checkmeck/daa1
checkmeck/daa2
checkmeck/daa3

Categories