We help IT Professionals succeed at work.

Check out our new AWS podcast with Certified Expert, Phil Phillips! Listen to "How to Execute a Seamless AWS Migration" on EE or on your favorite podcast platform. Listen Now

x

Why does this loop go on forever?

FairyBusiness
on
Medium Priority
362 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

Comment
Watch Question

Greg AlexanderLead Developer

Commented:
You can get the same results with

$file = 'filetest.txt';

$contents = file_get_contents($file);

Author

Commented:
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!!
Greg AlexanderLead Developer

Commented:
Is the file readable and are you sure the path is correct?

Author

Commented:
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

Greg AlexanderLead Developer

Commented:
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?

Author

Commented:
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

Lead Developer
Commented:
Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION

Author

Commented:
What do you know, it does :)  Thanks!
Greg AlexanderLead Developer

Commented:
Cool! Glad to help :)
Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a sample view!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.