Fgets stops after 130 lines PHP - php

$done=0;
$filename = "raw_urls.txt";
if(! ($fhandle = fopen($filename, "r")))
{ echo "File failed to open";
Exit; }
//
// main loop reads sitemap url list
//
while($url_full_raw = fgets($fhandle,4096))
{
print (mysql_error());
$url_full= preg_replace('/[\x00-\x1F\x80-\xFF]/', '', $url_full_raw);
if(strlen($url_full) > 3)
{
$url_stat++;
// Echo ' tp1 Url from sitemap:',$url_stat,' - ' ,$url_full,'<br>';
$end_st = strlen($url_full)-29;
$s_url= substr($url_full,29,$end_st);
//Echo 'short:',$s_url,'<br>';
$url_full_raw= '';
}
else{
$done++;
Echo '----------- short string ---------------';
}
//
// Check for url
//
$res1=sql("SELECT * FROM `spy3` WHERE `Landingpage` LIKE '%$s_url%' LIMIT 0, 30 ",$o);
if($row=mysql_fetch_array($res1))
{
$lp=$row[6];
$found++;
// Echo '------->Url from sitemap:',$url_full,'<br>';
}
else{
Echo 'Not Found:-> ',$s_url,'<br>';
$nfound++;
}
sql("insert into sitemap (url, stat_url,nf, s_recno)
values (
'$url_full',
'$lp',
'$nfound',
'$url_stat'
)", $o);
print (mysql_error());
$found=0;
$nfound=0;
}
So the code works great. Except for one problem, after about 130 lines, it stops. It exits the program with no error. Yes full error reporting is on. PHP.ini memory is huge.
If I edit the txt file and take out some lines, no difference. I have been working on this for many hours.

Try doing it like they do in their example... with !== false. i.e,
while(($url_full_raw = fgets($fhandle,4096))!==false) {
I'm guessing your content is evaluating to false for whatever reason. That just happens to be at 130 lines (throw the 130 lines into a text file and see if the file size is close to 4 KB).
Also, you might want to fix your formatting for next time. Makes it very hard for us to read and help you.

Related

PHP: Missing records when writing to file

My telecom vendor is sending me a report each time a message goes out. I have written a very simple PHP script that receive values via HTTP GET. Using fwrite I write the query parameter to a CSV file.The filename is report.csv with the current date as a prefix.
Here is the code :
<?php
error_reporting(E_ALL ^ E_NOTICE);
date_default_timezone_set('America/New_York');
//setting a the CSV File
$fileDate = date("m-d-Y") ;
$filename = $fileDate."_Report.csv";
$directory = "./csv_archive/";
//Creating handle
$handle = fopen($filename, "a");
//These are the main data field
$item1 = $_GET['item1'];
$item2 = $_GET['item2'];
$item3 = $_GET['item3'];
$mydate = date("Y-m-d H:i:s") ;
$pass = $_GET['pass'];
//testing the pass
if (isset($_GET['pass']) AND $_GET['pass'] == "password")
{
echo 'Login successful';
// just making sure the function could write to it
if (!$handle = fopen($directory.$filename, 'a')){
echo "Cannot open file ($filename)";
exit;
}
//writing the data I receive through query string
if (fwrite($handle, "$item1,$item2,$item3,$mydate \n") === FALSE) {
echo "Cannot write to file ($filename)";
exit;
}
fclose($handle);
}
else{
echo 'Login Failure please add the right pass to URL';
}
?>
The script does what I want, but the only problem is inconsistency, meaning that a good portion of the records are missing (about half the report). When I log to my account I can get the complete report.
I have no clue of what I need to do to fix this, please advice.
I have a couple of suggestions for this script.
To address Andrew Rhyne's suggestion, change your code that reads from each $GET variable to:
$item1 = (isset($_GET['item1']) && $_GET['item1']) ? $_GET['item1'] : 'empty';
This will tell you if all your fields are being populated.
I suspect you problem is something else. It sounds like you are getting a seperate request for each record that you want to save. Perhaps some of these requests are happening to close together and are messing up each other's ability to open and write to the file. To check if this is happening, you might try using the following code check if you opened the file correctly. (Note that your first use of 'fopen' in your script does nothing, because you are overwriting $handle with your second use of 'fopen', it is also opening the wrong file...)
if (!$handle = fopen($directory.$filename, 'a')){
$handle = fopen($directory.date("Y-m-d H:i:s:u").'_Record_Error.txt', 'a');
exit;
}
This will make sure that you don't ever lose data because of concurrent write attempts. If you find that this is indeed you issue, you can delay subsequent write attempts until the file is not busy.
$tries = 0;
while ($tries < 50 && !$handle = fopen($directory.$filename, 'a')){
sleep(.5);//wait half a second
$tries++;
}
if($handle){
flock($handle);//lock the file to prevent other requests from opening the file until you are done.
} else {
$handle = fopen($directory.date("Y-m-d H:i:s:u").'_Record_Error.txt', 'a');//the 'u' is for milliseconds
exit;
}
This will spend 25 seconds, trying to open the file once every half second and will still output your record to a unique file every time you are still unable to open the file to write to. You can then safely fwrite() and fclose() $handle as you were.

Getting syntax error while using fopen and fread

$file = "status.txt";
$open = fopen($file, "r");
$size = filesize($file);
$count = fread($open, $size);
if($count == 1) {
header('Location: http://www.google.com/');
} else {
echo "Status is unavailable";
}
Hello, I am trying to read a text file.
I get the error Parse error: syntax error, unexpected T_STRING while doing this.
I am trying to read status.txt and if it has 1 it will redirect else it will say Status is unavailable.
Any ideas?
I must point out that for simple files like this, that file_get_contents is much easier than fopen, fread, fclose (which you omitted), etc.
The code that you posted does not appear to have any issues by itself. Perhaps you messed something up before that whole block? Try and comment things out to isolat the bug. This is a debugging skill you will need to acquire.
The error is not in those lines for sure! please include the whole PHP script, there're must be something wrong before or after those lines.
Please try this code out!
<?php
$file = 'status.txt';
$contents = file_get_contents($file);
if ((strlen($contents)-1) == 1) {
header('Location: http://www.google.com');
} else {
echo 'Status is unavailable';
}
?>
The (-1) in the compression because of the new line character count at the last line.
If you're talking about the content of "status.txt" please put the '1' between quotes. if it is just a TRUE or FALSE statement just use if($count){ ... }else{ ...}

Need help resetting a php counter

I'm really new to php. I decided to make a counter based off a script I've seen. I've made changes to it. I'm trying to figure out how to reset the counter.
$userCount = file_get_contents("count.txt");
$userCount = trim($userCount);
$userCount = $userCount + 1;
$countReset = $userCount - $userCount;
$file = fopen("count.txt","w+");
fwrite($file,$userCount);
fclose($file);
print "The number of visitors is: $userCount";
if ($userCount < 20){
echo 'not yet';
}
else {
echo 'done!';
}
if ($userCount > 40){
fwrite($file,$countReset);
fclose($file);
}
I tried subtracting the counter from itself to get it back to 0.
$countReset = $userCount - $userCount;
However, it doesn't seem to work. the counter itself works, but I am unable to get it to reset back to 0.
This is just an impression like script I'm doing as a way to learn php. Also, sorry about the format, struggling with this post editor.
Any help with the script?
You've closed the file before trying to write to it again. Before your second fwrite, add:
$file = fopen("count.txt","w+");
Try to simply set value 0 to the var:
$countReset = 0;
Couldn't you just edit the count.txt file?
Doing it in PHP, you could do
fwrite($file,'0');
EDIT: Like CanSpice said, you shouldn't close the file before you're done with it. Remove the first fclose, and it should work.
I wouldn't mix fopen() and file_get_content() functions in this context, either use fopen(), fread() and fwrite() or use file_get_contents() and file_put_contents().
If you need just reset counter and you don't need previous value, than use:
file_put_contents('count.txt', '0');
If you need update value you may use either:
$count = file_get_contents( 'count.txt');
$count++;
// Reset?
if( $reset){
$count = 0;
}
file_put_contents( 'count.txt', "$count");
Or rather:
$fp = fopen( 'count.txt', 'r+') or die( 'Cannot use counter');
$count = trim( fread( $fp, 1024));
$count++;
// Reset?
if( $reset){
$count = 0;
}
ftruncate( $fp, 0);
fseek( 0, SEEK_SET)
fwrite( $fp, "$count");
fclose( $fp);
Here are the manual pages for ftruncate() and fseek() + you should probably study flock() so two scripts wouldn't overwrite the content at the same time.
/****************************************************************************
* read the current count from the counter file, increment
* it by 1, and re-save the new value off to the file
***************************************************************************/
function getAndIncrementCount(){
// attempt to open the file
// a+ means keep the contents so we can read it at least once,
// but allow us to overwrite the value once we increment it
if (($fHandle = fopen('count.txt','a+')) !== FALSE){
// read in the count (also cast to an int so if there is
// an invalid (or absent) value it will default to a 0
$count = (int) fread($fHandle, 100);
// increase the counter
$count++;
// go back to the beginning of the file
fseek($fHandle, 0);
// re-write the new count back to the file
fwrite($fHandle, $count);
// cose the file now that we're done with it
fclose($fHandle);
// return back the count
return $count;
}
// we couldn't get to the file, so return an error flag
return FALSE;
}
/****************************************************************************
* write the specified value to the counter file
***************************************************************************/
function setCount($count = 0){
// attempt to open the file with over-write permissions
// w+ will open the file and clear it
if (($fHandle = fopen('count.txt','w+')){
// write the counter to the file
fwrite($fHandle, $count);
// close the file now that we're done
fclose($fHandle);
// return the newly saved count
return $count;
}
// we couldn't get to the file, so return an error flag
return FALSE;
}
And applied in practice:
$userCount = getAndIncrementCount();
echo "The number of visitors is: {$userCount}";
if ($userCount < 20){
echo "Not Yet";
}else{
echo "Done!";
}
if ($userCount > 40){
setCount(0);
}
It's because you are not rewriting the file contents but adding to them when you use the fwrite second time, so $countReset gets appended to the content already in the file. Try this:
$userCount = file_get_contents("count.txt");
$userCount = $userCount + 1;
$countReset = $userCount - $userCount;
file_put_contents("count.txt", $userCount);
print "The number of visitors is: $userCount\n";
if ($userCount < 20){
echo 'not yet';
}
else {
echo 'done!';
}
if ($userCount > 40){
file_put_contents("count.txt", $countReset);
}

Fscanf stops after 130 lines PHP

I have been working on this for days, I thought I had it but was wrong.
$done=0;
$filename = "raw_urls.txt";
if(! ($fhandle = fopen($filename, "r")))
{ echo "File failed to open";
Exit; }
while((fscanf($fhandle, "%s\n",$url_full))!== false)
{
print (mysql_error());
if(strlen($url_full) > 3)
{
$url_stat++;
$end_st = strlen($url_full)-29;
$s_url= substr($url_full,29,$end_st);
}
else{
$done++;
}
$res1=sql("SELECT * FROM `spy3` WHERE `Landingpage` LIKE '%$s_url%' LIMIT 0, 30 ",$o);
if($row=mysql_fetch_array($res1))
{
$lp=$row[6];
$found++;
}
else{
$nfound++;
}
sql("insert into sitemap (url, stat_url,nf, s_recno)
values (
'$url_full',
'$lp',
'$nfound',
'$url_stat'
)", $o);
print (mysql_error());
$found=0;
$nfound=0;
}
?>
I have tried fgets, changed txt files, it always stops between 128 and 132 lines of text. There are 2500 lines in the text file. Php.ini memory is very big. If I cut the txt file where it stops and save it, its 9k big.
have seen this link..?
Please Click here
OR
you can also use fseek($handle, 0); AND refer this link
Thanks.

limiting size of written file

again. First of all, bear with me, it's late at night, and I'm tired, so this is probably a simple answer...
I'm gathering info on products, one by one, then writing each product as a line in a CSV file. However, I don't want one huge CSV file, so I'm trying to split the files up by size. As each new product is gathered, the script should check whether or not the file it's about to write to is less than the limit. If it is, then it should go ahead and write to that file. If not, then a new file should be created and the product added to the new file.
Here's what I curently have trying to limit the size of the file:
$f = 1;
if(file_exists($shop_path.'items/items'.$f.'.txt'))
{
if(filesize($shop_path.'items/items'.$f.'.txt') >= 512000)
{
$f++;
$fh = fopen($shop_path.'items/items'.$f.'.txt', "a+");
}
else
{
$fh = fopen($shop_path.'items/items'.$f.'.txt', "a+");
}
}
else
{
$fh = fopen($shop_path.'items/items'.$f.'.txt', "a+");
}
if(fwrite($fh, $prod_str) === TRUE)
{
echo 'Done!<br />';
}
else
{
echo 'Could not write to file<br />';
}
fclose($fh);
However, I just keep getting the "Could not write to file" error. Why is this?
fwrite doesnt return a boolean value, only on failure (false).
"fwrite() returns the number of bytes written, or FALSE on error."
Check out: http://hu2.php.net/manual/en/function.fwrite.php
Maybe it works:
if(fwrite($fh, $prod_str))
{
echo 'Done!<br />';
}
else
{
echo 'Could not write to file<br />';
}
Edit:
Or even better:
if(fwrite($fh, $prod_str) !== false)
{
echo 'Done!<br />';
}
else
{
echo 'Could not write to file<br />';
}
The ftell() function will always tell you your current byte offset in a file. Test the value of this before each write, and if it's over your predefined value then you can close that file and open a new one.

Categories