Table of Contents

filesize

WMPRO, WMMINI FW >= 1.0 WMMEGA FW >= 2.0

Return the size of a file, or the number of unread bytes in a stream or socket

Description

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:

int filesize ( string $filename )

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

int filesize ( int $handle )

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)

Parameters

$filename: Full path and filename (Filename Form)

$handle: Valid handle of a previously opened resource (Handle Form)

Return Values

Integer number of bytes in the file, or pending in the stream or socket

Examples

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");
?>

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);
  }
?>

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 ...
  }
?>

See Also

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

fseropen() - Open the serial port at the specified baud rate with optional parameters

f485open() - Open the RS-485 port at the specified baud rate and parity

fsockopen() - Open an internet socket connection with optional timeout

fread() - Read bytes from a file, stream or socket

fgets() - Return a single line from a file, stream or socket, with optional size limit

fwrite() - Write data to a file, stream or socket

feof() - Test if no more data is available in a file, stream or socket [possibly useful if you don't need to know the exact number of bytes pending in a stream]

fseek() - Position the file pointer in an open file

ftell() - Return the current position of a file read/write pointer

fclose() - Close a file, stream or socket

file_exists() - Check if a file exists

ini_get() - Get a value from an INI file

1)
The Handle Form is more commonly used on the Wattmon. Compared to mainline PHP, this uPHP form is a much simpler method for retrieving the number of bytes pending in a stream. For examples of how much more complex mainline PHP can be, take a look at the PHP function stream-get-meta-data which returns an array with one of the elements containing 'unread_bytes'.
2)
I haven't verified this yet, perhaps for files the Handle Form returns the number of bytes remaining to be read beyond the pointer. - Jeff, 2/23/17