This is an old revision of the document!
WMPRO, WMMINI FW >= 1.0 WMMEGA FW >= 2.0
Return the size of a file, or the number of unread bytes in a stream
There are two different forms for this function.
Filename Form - The syntax is the same as in mainline PHP, and uses a filename for the parameter:
Handle Form - The syntax is unique to uPHP,1) and uses an open resource handle for the parameter (which works with streams, sockets, ports and files):
The Handle Form works in different ways, depending on the type of resource. For streams, sockets and ports it will return the number of unread bytes in the receive buffer. For files it will return the total number of bytes in the file.2)
filename: Full path and filename (Filename Form)
handle: Valid handle of a previously opened resource (Handle Form)
Bytes in the file or pending in the stream
Example #1 - Filename Form, output the size of the System Log:
<? $syslog="/logs/log.txt"; if (file_exists($syslog)) print("The System Log contains ".filesize($syslog)." bytes"); ?>
Example #2 - Handle Form, for a file, output the file size:
<? $f=fopen("/index.cgi","r"); if ($f) { print(filesize($f)); //output the file size fclose($f); } ?>
Example #3 - Handle Form, for a socket (this example is extracted from the Wattmon OS “/scripts/ip_dongle.cgi”):
<? $dns_ip=ini_get("/config/dns.ini","dns","my.wattmon.com","my.wattmon.com"); $f=fsockopen($dns_ip,80,30); if ($f) { //... code not shown: send a request to my.wattmon.com using fwrite() ... while (!filesize($f)) sleep(10); //wait until we begin to get a response $str=fread($f,filesize($f)); //retrieve all the bytes pending //... code not shown: process the response in $str ... } ?>
feof() - Test if no more data is available in a file or stream [possibly useful if you don't need to know the exact number of bytes pending in a stream]
findfirst() - Start searching the current folder for files matching a pattern and attributes ['filesize' is one of the array elements returned, see also findnext()]
fopen() - Open a file for reading or writing
f485open() - Open the RS-485 port as a stream
fseropen() - Open the serial port at the specified baud rate with optional parameters
fsockopen() - Open an internet socket connection with optional timeout
fclose() - Close a file, stream or socket
file_exists() - Check if a file exists
ini_get() - Return the value associated with the key if it exists, or the default value otherwise
fread() - Return bytes from a file or stream
if - Flow control structure for conditional execution
while - Flow control structure for a conditional loop