User Tools

Site Tools


uphp:functions:file_exists

This is an old revision of the document!


file_exists

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

Check if a file exists

Description

int file_exists ( string $filename )

Return 1 (true) if the file exists, otherwise return 0 (false)

Parameter

$filename: Full path and file name to check

Return Value

1 if file exists, 0 if file does not exist

Examples

Simple existence report:

<pre><?
  if (file_exists("/test.txt")) {
    print("File exists.");
  } else {
    print("File does not exist!");
  }
?></pre>

Create a new file with a unique non-conflicting filename - this example is extracted from the Wattmon OS “/scripts/logrotate.cgi”:

<?
  $date=strftime("/logs/log%Y%m%d.txt",time()); // desired filename, but it may already exist
  $cnt=0;
  while (file_exists($date)) {
    $date=strftime("/logs/log%Y%m%d-".$cnt.".txt",time()); // unique non-conflicting filename
    $cnt++;
  }
  $st="/logs/log.txt";
  if (file_exists($st)) {
    $res=rename($st,$date);
    if ($res==0) $_GLOBALS['sysmsg']="Log file rotated to ".$date;
    log("New logfile created after logrotate called.");
  } else {
    $_GLOBALS['sysmsg']="Unable to rotate, log file doesnt exist";
  }
  print("{\"status\":1,\"msg\":\"".$_GLOBALS['sysmsg']."\"}");
?>

See Also

findfirst() - Start searching the current folder for files matching a pattern and attributes

strftime() - Format the passed time using the format string

time() - Return the current system timestamp

rename() - Rename or move a file from source to destination

log() - Print to the System Log

uphp_special_variables - Special arrays that are populated automatically (including $_GLOBALS)

uphp/functions/file_exists.1490731774.txt.gz · Last modified: 2021/09/13 05:56 (external edit)