Code Summary for checking if a file or directory exists in PHP _php tutorial

Source: Internet
Author: User
The following is an example code that checks whether a file exists:
Copy CodeThe code is as follows:
$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 execution of the PHP file displays the following results:
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 CodeThe code is as follows:
if (file_exists ("C:\blabla\php"))
{echo "yes";}
Else
{echo "No";}

Instance
Copy CodeThe code is as follows:
/**
* File or directory permission check function
*
* @access Public
* @param string $file _path file path
* @param bool $rename _PRV Check permission to execute rename () function when checking for modify permissions
*
* @return int return value range is {0 <= x <= 15}, the meaning of each value can be introduced by a combination of four-bit binary numbers.
* return value in binary notation, four bits are represented by high to low respectively
* Executable Rename () function permission, can append content permission to file, writable file permission, read file permission.
*/
function File_mode_info ($file _path)
{
/* If not present, unreadable, non-writable, non-modified */
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 it is a directory */
if (Is_dir ($file _path))
{
/* Check if the directory is readable */
$dir = @opendir ($file _path);
if ($dir = = = False)
{
return $mark; If the directory fails to open, directly returns the directory is non-modifiable, non-writable, unreadable
}
if (@readdir ($dir)!== false)
{
$mark ^= 1; Directory readable 001, directory unreadable 000
}
@closedir ($dir);
/* Check if the directory is writable */
$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 writable readable 011, directory writable unreadable 010
}
@fclose ($FP);
@unlink ($test _file);
/* Check if 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 permission to execute 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))
{
/* Open as Read */
$fp = @fopen ($file _path, ' RB ');
if ($FP)
{
$mark ^= 1; Readable 001
}
@fclose ($FP);
/* Try to modify the file */
$fp = @fopen ($file _path, ' ab+ ');
if ($fp && @fwrite ($fp, ')!== false)
{
$mark ^= 6; Can be modified writable readable 111, non-modifiable writable readable 011 ...
}
@fclose ($FP);
/* Check the directory for permission to execute 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 determines if a directory exists
Copy CodeThe code is as follows:
/****************************************************
* XML data stream, written to XML file
* @param $xmlData
* @return bool|string
*/
function Writexmlfile ($xmlData)
{
$time = time (); Gets the timestamp used to name the file
$path = DirName (__file__); Gets the current absolute path
$path = Substr_replace ($path, "", Stripos ($path, "Actions\data")); Replace the inherent path of this file with an empty
$path. = "Xmlfiles\"; Store Directory Name
/* Determine if the target directory exists, does not exist, and then new */
if (!is_dir ($path))
{
mkdir ($path); New Catalog
}
/* Record full path and file name */
$filePathAndName = $path. $time. ". XML ";
/* Open the file with the 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;
}

http://www.bkjia.com/phpjc/326197.html Span id= "Indexurl" itemprop= "Indexurl" >www.bkjia.com true http://www.bkjia.com/phpjc/326197.html techarticle Below is a simple example code that checks whether a file exists: Copy the code as follows: PHP $filename = '/path/to/foo.txt '; if (file_exists ($filename)) {echo "The file $file ...

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.