The following is a simple instance code that checks whether a file exists:
Copy Code code as follows:
<?php
$filename = '/path/to/foo.txt ';
if (file_exists ($filename)) {
echo "The file $filename exists";
} else {
echo "The file $filename does not exist";
}
?>
If the file exists, the result of executing the PHP file is:
The file C:blablaphphello.txt exists.
If the file does not exist, the result of executing the PHP file is:
The file C:\blabla\phphello.txt does not exist.
You can also use the File_exists function to test whether a directory exists, as shown in the sample code:
Copy Code code as follows:
if (file_exists ("C:\blabla\php"))
{echo "yes";}
Else
{echo ' no ';}
Instance
Copy Code code as follows:
/**
* File or directory permission check function
*
* @access Public
* @param string $file _path file path
* @param bool $rename _PRV Check the permissions to perform the rename () function when checking for modify permissions
*
* @return int Returns a value of {0 <= x <= 15}, each value represents a combination of four-bit binary numbers.
* return value in binary notation, four bits from high to low represent
* Executable Rename () function permissions, append content permissions to files, writable file permissions, read file permissions.
*/
function File_mode_info ($file _path)
{
/* If it does not exist, it is unreadable, cannot be written, can not be changed * *
if (!file_exists ($file _path))
{
return false;
}
$mark = 0;
if (Strtoupper (substr (php_os, 0, 3)) = = ' WIN ')
{
/* Test File * *
$test _file = $file _path. '/cf_test.txt ';
/* If the directory * *
if (Is_dir ($file _path))
{
/* Check the directory is readable * *
$dir = @opendir ($file _path);
if ($dir = = False)
{
return $mark; If the directory fails to open, the direct return directory cannot be modified, writable, unreadable
}
if (@readdir ($dir)!== false)
{
$mark ^= 1; Catalog readable 001, directory not readable 000
}
@closedir ($dir);
/* Check whether the directory can be written * *
$fp = @fopen ($test _file, ' WB ');
if ($fp = = False)
{
return $mark; If the file creation in the directory fails, the return is not writable.
}
if (@fwrite ($fp, ' Directory Access testing. ')!== false)
{
$mark ^= 2; Directory can write readable 011, directory can write not read 010
}
@fclose ($FP);
@unlink ($test _file);
/* Check the directory can be modified * *
$fp = @fopen ($test _file, ' ab+ ');
if ($fp = = False)
{
return $mark;
}
if (@fwrite ($fp, "Modify Test.rn")!== false)
{
$mark ^= 4;
}
@fclose ($FP);
/* Check the directory for the permission to perform the rename () function.
if (@rename ($test _file, $test _file)!== false)
{
$mark ^= 8;
}
@unlink ($test _file);
}
/* If it is a file * *
ElseIf (Is_file ($file _path))
{
/* Read mode Open/*
$fp = @fopen ($file _path, ' RB ');
if ($FP)
{
$mark ^= 1; Can read 001
}
@fclose ($FP);
* * Try to modify the file * *
$fp = @fopen ($file _path, ' ab+ ');
if ($fp && @fwrite ($fp, ')!== false)
{
$mark ^= 6; Can be modified to write readable 111, can not be modified to write readable 011 ...
}
@fclose ($FP);
/* Check the directory for the permission to perform the rename () function.
if (@rename ($test _file, $test _file)!== false)
{
$mark ^= 8;
}
}
}
Else
{
if (@is_readable ($file _path))
{
$mark ^= 1;
}
if (@is_writable ($file _path))
{
$mark ^= 14;
}
}
return $mark;
}
PHP to determine whether the directory exists
Copy Code code as follows:
/****************************************************
* streams XML data, Write to XML file
* @param $xmlData
* @return bool|string
/
Function Writexmlfile ($xmlData)
{
$time = time (); Gets a timestamp that is used to name the file
$path = dirname (__file__);//Get the current absolute path
$path = Substr_replace ($path, "", Stripos ($path, "action S\data ")); Replace the natural path where this file resides with an empty
$path. = "xmlfiles\";//Storage Directory name
/* To determine if the target directory exists, does not exist then new */
if (!is_dir ($path))
{
M Kdir ($path); New directory
}
/* Record full path and filename/
$filePathAndName = $path. $time. ". XML ";
/* Open file with file name < timestamp > + <.xml>*/
$fp = fopen ($filePathAndName, "w");
if (! $fp)
{
return false;
}
/* write to file stream * *
$flag = fwrite ($fp, $xmlData);
if (! $flag)
{
return false;
}
Fclose ($FP);
return $filePathAndName;
}