Code summary for checking whether a file or directory exists in php_PHP tutorial

WBOY
Release: 2016-07-21 15:14:54
Original
920 people have browsed it

The following is a simple example code to check whether the file exists:

Copy the code The 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 displayed result of executing the PHP file is:
The file C:blablaphphello .txt exists.
If the file does not exist, the displayed result of executing the PHP file is:
The file C:blablaphphello.txt does not exist.
You can also use the file_exists function to test whether a directory exists. , the sample code is as follows:
Copy code The code is as follows:

if (file_exists("C:blablaphp"))
{echo "yes";}
else
{echo "no";}

Example
Copy code The code is as follows:

/**
* File or directory permission check function
*
* @access public
* @param string $file_path File path
* @param bool $rename_prv Whether to check the execution of rename when checking modification permissions () Function permissions
*
* @return int The value range of the return value is {0 <= x <= 15}, and the meaning of each value can be derived from a combination of four binary numbers.
* The return value is in binary notation. The four digits from high to low respectively represent
* executable rename() function permissions, permissions to append content to files, permissions to write files, and permissions to read files. .
*/
function file_mode_info($file_path)
{
/* If it does not exist, it will not be readable or writable , cannot 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 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, return directly to the directory that cannot be modified, written, or read
}
if (@readdir($dir) !== false )
{
$mark ^= 1; //The directory is readable 001, the directory is unreadable 000
}
@closedir($dir);
/* Check whether the directory is writable* /
$fp = @fopen($test_file, 'wb');
if ($fp === false)
{
return $mark; //If the file creation in the directory fails , returns unwritable.
}
if (@fwrite($fp, 'directory access testing.') !== false)
{
$mark ^= 2; //The directory is writable and readable 011, directory Writable but not readable 010
}
@fclose($fp);
@unlink($test_file);
/* Check whether 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 whether the rename() function has been executed in the directory Permissions*/
if (@rename($test_file, $test_file) !== false)
{
$mark ^= 8;
}
@unlink($test_file);
}
/* If it is a file*/
elseif (is_file($file_path))
{
/* Open in read mode*/
$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, written and read 111, cannot be modified, written and read 011...
}
@fclose($fp);
/* Check Whether the directory has 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 judgment directory Is there
Copy code The code is as follows:

/*****************************************************
* Write xml data stream to xml file
* @param $xmlData
* @return bool|string
*/
function writeXmlFile($xmlData)
{
$time = time(); //Get the timestamp for naming the file
$path = dirname(__FILE__); //Get the current absolute path
$path = substr_replace($path, "", stripos($path, "actionsdata")); //Replace the inherent path where this file is located Empty
$path .= "xmlFiles"; //Storage directory name
/*Determine whether the target directory exists, if not, create a new one*/
if(!is_dir($path))
{
mkdir($path); //Create a new directory
}
/*Record the complete path and file name*/
$filePathAndName = $path.$time.".xml";
/*Open the file, the file name is + <.xml>*/
$fp = fopen($filePathAndName, "w");
if(!$fp)
{
return false;
}
/*Write file stream*/
$flag = fwrite($fp, $xmlData);
if(!$flag)
{
return false;
}
fclose($fp);
return $filePathAndName;
}

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/326197.htmlTechArticleThe following is a simple example code to check whether the file exists: Copy the code as follows: ?php $filename = ' /path/to/foo.txt'; if (file_exists($filename)) { echo "The file $file...
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!