Hello,
I am trying to implement fgets to echo lines from a text file, however the function does not work. If I replace it with fgetc, there is no issue, but fgets is ****. I am running PHP 5.2.x on my site. Is this a recognized issue?
PHP5.2 - No Fgets() ?
Hula,
I assume you just use one argument for fgets (?). Is your text-file correct?
No reason I know of that it should not work, with the caveat that we have not seen your code so do not know how you are using it or whether you would be better served using readfile(), file(), file_get_contents(), etc.
Thanks for the replies folks. Sorry for the ambiguity, I will go ahead and elaborate.
Beta.php is the main file and calls within the HTML
<?php include('array.php') ?>
array.php is in the same directory, containing this code:
<?php
$file=fopen('array.txt','r');
echo fgets($file);
fclose($file);
?>
and array.txt is just a list of strings. If I replace fgets($file) with fgetc($file), the code works and the page will display. Otherwise, the error occurs. Here is the page I am working on in case you are interested: http://cdizzle.comze.com/test/beta.php
Must be something with the text file, I just get the word Array now..I am looking for a skeleton
Could the fact that fgets() will only read until it hits a newline in the file have any bearing on the results you are seeing? (If you want to read the entire contents of a mutli-line file, just use file_get_contents(), instead, with the added benefit that fopen()/fclose() are no longer needed).
Well I pretty much tried everything, fgets(), file(), and file_get_contents() all yield nothing, I have them set to an array so all I get is the word Array. But fgetc() gives me C, the first letter of the first line..
I'm not sure where an array comes into it, other than file() returns an array????
PS: If I wanted to test it, I would do:
<?php
ini_set('display_errors', 1);
error_reporting(E_ALL);
$file = fopen('array.txt', 'r');
echo "<pre>";
while(($line = fgets($file)) !== false) {
echo htmlspecialchars($line);
}
echo "</pre>";
fclose($file);