?
Solved

Why does this loop go on forever?

Posted on 2011-04-19
9
Medium Priority
?
341 Views
Last Modified: 2012-05-11
Hi, can anyone tell me why this causes an infinite loop?

// incremental reading
$file = 'filetest.txt';
$content = "";
if($handle = fopen($file, 'r')) { // reads
	while(!feof($handle)) { // loop thru until you get to the end of the file (if not at the end of the file)
		$content .= fgets($handle); // gets one line of the file
		fclose($handle);
	}
}
echo $content;

Open in new window

0
Comment
Question by:FairyBusiness
  • 5
  • 4
9 Comments
 
LVL 19

Expert Comment

by:Greg Alexander
ID: 35429284
You can get the same results with

$file = 'filetest.txt';

$contents = file_get_contents($file);
0
 

Author Comment

by:FairyBusiness
ID: 35429291
yeah I know but its part of a tutorial and its suppose to teach me something about feof and fgets.
so annoying when I write exactly whats in the tutorial and it works for them and not me!!
0
 
LVL 19

Expert Comment

by:Greg Alexander
ID: 35429308
Is the file readable and are you sure the path is correct?
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 

Author Comment

by:FairyBusiness
ID: 35429324
Not sure why the path would be incorrect:  http://www.auroriella.com/file_read.php

It has other code besides this loop, but it just stopped working after I put that in it.  If I comment it out the rest of the code works. . .
<?php

echo is_readable('file_access.php') ? 'yes' : 'no';
echo "<br />";
echo is_writable('file_access.php') ? 'yes' : 'no';

$file = 'filetest.txt';
if($handle = fopen($file, 'r')) { // reads
	$content = fread($handle, 5); // each character is 1 byte
	fclose($handle);
}
else {
	echo "Could not open file for writing.";
}
// Beware, it will OVERTYPE!!
// NOTE: a and a+ modes will not let you move the point
echo $content;
echo "<br />";
echo nl2br($content); // look up
echo "<hr />";

// use filesize() to read the whole file
$file = 'filetest.txt';
if($handle = fopen($file, 'r')) { // reads
	$content = fread($handle, filesize($file)); // each character is 1 byte
	fclose($handle);
}
echo "<br />";
echo nl2br($content); // look up
echo "<br />";
echo "<hr />";

// file_get_contents() is a shortcute for fopen/fread/fclose
// companion to shortcut file_put_contents()
$content = file_get_contents($file);
echo $content;

// incremental reading
$file = 'filetest.txt';
$content = "";
if($handle = fopen($file, 'r')) { // reads
	while(!feof($handle)) { // loop thru until you get to the end of the file (if not at the end of the file)
		$content .= fgets($handle); // gets one line of the file
		fclose($handle);
	}
}
echo $content;
?>

Open in new window

0
 
LVL 19

Expert Comment

by:Greg Alexander
ID: 35429335
Hmm, you are checking file_access.php for is_readable however the file you using in the loop is filetest.txt...  is there a reason for that?
0
 

Author Comment

by:FairyBusiness
ID: 35429344
I just changed it to check for filetext.txt because thats the file I am trying to read from.

http://www.auroriella.com/file_read.php

<?php

echo is_readable('filetest.txt') ? 'yes' : 'no';
echo "<br />";
echo is_writable('filetest.txt') ? 'yes' : 'no';
echo "<br />";

$file = 'filetest.txt';
if($handle = fopen($file, 'r')) { // reads
	$content = fread($handle, 5); // each character is 1 byte
	fclose($handle);
}
else {
	echo "Could not open file for writing.";
}
// Beware, it will OVERTYPE!!
// NOTE: a and a+ modes will not let you move the point
echo $content;
echo "<br />";
echo nl2br($content); // look up
echo "<hr />";

// use filesize() to read the whole file
$file = 'filetest.txt';
if($handle = fopen($file, 'r')) { // reads
	$content = fread($handle, filesize($file)); // each character is 1 byte
	fclose($handle);
}
echo "<br />";
echo nl2br($content); // look up
echo "<br />";
echo "<hr />";

// file_get_contents() is a shortcute for fopen/fread/fclose
// companion to shortcut file_put_contents()
$content = file_get_contents($file);
echo $content;

// incremental reading
/*$file = 'filetest.txt';
$content = "";
if($handle = fopen($file, 'r')) { // reads
	while(!feof($handle)) { // loop thru until you get to the end of the file (if not at the end of the file)
		$content .= fgets($handle); // gets one line of the file
		fclose($handle);
	}
}
echo $content;*/
?>

Open in new window

0
 
LVL 19

Accepted Solution

by:
Greg Alexander earned 2000 total points
ID: 35429384
Does moving the fclose to outside the loop change anything:
if($handle = fopen($file, 'r')) { // reads
        while(!feof($handle)) { // loop thru until you get to the end of the file (if not at the end of the file)
                $content .= fgets($handle); // gets one line of the file
                
        }
fclose($handle);
}

Open in new window

0
 

Author Closing Comment

by:FairyBusiness
ID: 35429389
What do you know, it does :)  Thanks!
0
 
LVL 19

Expert Comment

by:Greg Alexander
ID: 35429394
Cool! Glad to help :)
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
This holiday season, we’re giving away the gift of knowledge—tech knowledge, that is. Keep reading to see what hacks, tips, and trends we have wrapped and waiting for you under the tree.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
Suggested Courses
Course of the Month16 days, 15 hours left to enroll

862 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question