0

New to programming. I am using fopen, fwrite fead to read and add content to a flat txt file. I can check to see if file_exists no problem, but if file exists but its empty... not sure how to check for that. Was hoping to at least get pointed in the right direction. Thanks :)

3
Contributors
3
Replies
4
Views
7 Years
Discussion Span
Last Post by fuston05
0

Hi fuston05,

The PHP function filesize($filename) will return the size of a file :)

However are you looking for the feof($file_handle) function which tells you when you've finished reading from a file?

For example:

<?php

//open the file
$file = fopen("no_such_file", "r");

if ($file===false) {
//error opening the file
}

if (feof($file)) {
//the file is empty and we've only just opened it, we haven't even read anything from it yet
}

$contents = '';
while (!feof($file)) {

//read from the file
$line = fgets($file);
$contents .= $line;

}

//close the file
fclose($file);
?>

Cheers,

James

Edited by james.newell: Used a bad example from PHP's website

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.