User Tools

Site Tools


uphp:functions:filesize

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revision Previous revision
Next revision
Previous revision
uphp:functions:filesize [2017/03/18 11:36]
jeff
uphp:functions:filesize [2021/09/13 05:57] (current)
Line 1: Line 1:
 ======filesize====== ======filesize======
 +
 <badge>WMPRO, WMMINI FW >= 1.0</badge> <badge>WMMEGA FW >= 2.0</badge> <badge>WMPRO, WMMINI FW >= 1.0</badge> <badge>WMMEGA FW >= 2.0</badge>
  
-Return the size of a file, or the number of unread bytes in a stream+Return the size of a file, or the number of unread bytes in a stream or socket
  
 ====Description==== ====Description====
 +
 There are two different forms for this function. 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: **Filename Form** - The syntax is the same as in mainline PHP, and uses a filename for the parameter:
  
-<well size="sm"><html><span style="font-size:125%;color:green;">int +<well size="sm"><html> 
-<span style="color:black;"></html>[[filesize]] +<span style="font-size:125%;color:green">int 
-<html>+<span style="color:black">filesize ( 
-<span style="color:green;">string +<span style="color:green">string 
-<span style="color:blue;">$filename +<span style="color:blue">$filename 
-<span style="color:black;">)+<span style="color:black">)
 </html></well> </html></well>
  
-**Handle Form** - The syntax is unique to uPHP,((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 [[http://php.net/manual/en/function.stream-get-meta-data.php|stream-get-meta-data]] which returns an array with one of the elements containing 'unread_bytes'.)) and uses an open resource handle for the parameter (which works with streams, sockets, ports and files):+**Handle Form** - The syntax is unique to uPHP,((The Handle Form is more commonly used on the [[hardware:wattmons|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 [[http://php.net/manual/en/function.stream-get-meta-data.php|stream-get-meta-data]] which returns an <html><b><span style="color:green">array<span style="color:black"></b></html> with one of the elements containing 'unread_bytes'.)) and uses an open resource handle for the parameter (which works with streams, sockets, ports and files):
  
-<well size="sm"><html><span style="font-size:125%;color:green;">int +<well size="sm"><html> 
-<span style="color:black;"></html>[[filesize]] +<span style="font-size:125%;color:green">int 
-<html>+<span style="color:black">filesize ( 
-<span style="color:green;">int +<span style="color:green">int 
-<span style="color:blue;">$handle +<span style="color:blue">$handle 
-<span style="color:black;">)+<span style="color:black">)
 </html></well> </html></well>
  
Line 30: Line 32:
  
 ====Parameters==== ====Parameters====
-<html><span style="color:blue;"><b>filename</b><span style="color:black;"></html>:  Full path and filename (Filename Form) 
  
-<html><span style="color:blue;"><b>handle</b><span style="color:black;"></html>:  Valid handle of a previously opened resource (Handle Form)+<html><b><span style="color:blue">$filename<span style="color:black"></b></html>:  Full path and filename (Filename Form)
  
-====Return Value==== +<html><b><span style="color:blue">$handle<span style="color:black"></b></html>:  Valid handle of a previously opened resource (Handle Form) 
-Bytes in the file or pending in the stream+ 
 +====Return Values==== 
 + 
 +<html><b><span style="color:green">Integer<span style="color:black"></b></html> number of bytes in the fileor pending in the stream or socket
  
 ====Examples==== ====Examples====
  
-**Example #1** - Filename Form, output the size of the System Log:+===Filename Form, output the size of the System Log===
  
 <code php> <code php>
 <? <?
-$syslog="/logs/log.txt"; +  $syslog="/logs/log.txt"; 
-if (file_exists($syslog)) print("The System Log contains ".filesize($syslog)." bytes");+  if (file_exists($syslog)) print("The System Log contains ".filesize($syslog)." bytes");
 ?> ?>
 </code> </code>
  
-**Example #2** - Handle Form, for a file, output the file size:+===Handle Form, for a file, output the file size===
  
 <code php> <code php>
-<?  +<? 
-$f=fopen("/index.cgi","r"); +  $f=fopen("/index.cgi","r"); 
-if ($f) { +  if ($f) { 
-  print(filesize($f)); //output the file size +    print(filesize($f)); // output the file size 
-  fclose($f); +    fclose($f); 
-}+  }
 ?></code> ?></code>
  
-**Example #3** - Handle Form, for a socket (this example is extracted from the Wattmon OS "/scripts/ip_dongle.cgi"):+===Handle Form, for a socket=== 
 + 
 +This example is extracted from the Wattmon OS "/scripts/ip_dongle.cgi":
  
 <code php> <code php>
 <? <?
-$dns_ip=ini_get("/config/dns.ini","dns","my.wattmon.com","my.wattmon.com"); +  $dns_ip=ini_get("/config/dns.ini","dns","my.wattmon.com","my.wattmon.com"); 
-$f=fsockopen($dns_ip,80,30); +  $f=fsockopen($dns_ip,80,30); 
-if ($f) { +  if ($f) { 
-  //... code not shown: send a request to my.wattmon.com using fwrite() ... +    // ... 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 +    while (!filesize($f)) sleep(10); // wait until we begin to get a response 
-  $str=fread($f,filesize($f)); //retrieve all the bytes pending +    $str=fread($f,filesize($f)); // retrieve all the bytes pending 
-  //... code not shown: process the response in $str ... +    // ... code not shown: process the response in $str ... 
-}+  }
 ?> ?>
 </code> </code>
Line 76: Line 82:
  
 ====See Also==== ====See Also====
-[[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()]]] [[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 [[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 [[fseropen()]] - Open the serial port at the specified baud rate with optional parameters
  
-[[fsockopen()]] - Open an internet socket connection+[[f485open()]] - Open the RS-485 port at the specified baud rate and parity
  
-[[fclose()]] - Close a file, stream or socket+[[fsockopen()]] - Open an internet socket connection with optional timeout
  
-[[file_exists()]] - Check if a file exists+[[fread()]] - Read bytes from a file, stream or socket
  
-[[ini_get()]] - Return the value associated with the key if it exists, or the default value otherwise+[[fgets()]] - Return a single line from a filestream or socket, with optional size limit
  
-[[fread()]] - Return bytes from a file or stream+[[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
  
-[[uphp:control_structures:if]] - Flow control structure for conditional execution+[[ini_get()]] - Get a value from an INI file
  
-[[uphp:control_structures:while]] - Flow control structure for a conditional loop 
  
uphp/functions/filesize.1489836973.txt.gz · Last modified: 2021/09/13 05:56 (external edit)