A code summary in php that checks for the existence of a file or directory
- 2020-05-26 08:01:10
- OfStack
Here is a simple example code to check if a file exists:
If the file exists, the display result of executing the PHP file is:
The file C:blablaphphello.txt exists.
If the file does not exist, the PHP file is displayed as:
The file C:\blabla\phphello.txt does not exist.
You can also test the existence of a directory with the file_exists function. The code is as follows:
The instance
PHP determines if the directory exists
<?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 display result of executing the PHP file is:
The file C:blablaphphello.txt exists.
If the file does not exist, the PHP file is displayed as:
The file C:\blabla\phphello.txt does not exist.
You can also test the existence of a directory with the file_exists function. The code is as follows:
if (file_exists("C:\blabla\php"))
{echo "yes";}
else
{echo "no";}
The instance
/**
* File or directory permission checking function
*
* @access public
* @param string $file_path The file path
* @param bool $rename_prv Whether to check execution when checking for modification permissions rename() Permission of a function
*
* @return int The return value ranges from {0 <= x <= 15} , the meaning of each value can be determined by 4 position 2 Base number combination.
* The return value in 2 In base counting, 4 The bits are represented from high to low
* The executable rename() Function permission, can append content permission to the file, can write to the file permission, can read the file permission.
*/
function file_mode_info($file_path)
{
/* If it does not exist, it cannot be read, written or changed */
if (!file_exists($file_path))
{
return false;
}
$mark = 0;
if (strtoupper(substr(PHP_OS, 0, 3)) == 'WIN')
{
/* The test file */
$test_file = $file_path . '/cf_test.txt';
/* If it's a directory */
if (is_dir($file_path))
{
/* Check that the directory is readable */
$dir = @opendir($file_path);
if ($dir === false)
{
return $mark; // If the directory fails to open, the directory is returned unmodified, unwritten, and unreadable
}
if (@readdir($dir) !== false)
{
$mark ^= 1; // Directory readable 001 , the directory is not readable 000
}
@closedir($dir);
/* Check that the directory is writable */
$fp = @fopen($test_file, 'wb');
if ($fp === false)
{
return $mark; // If file creation in the directory fails, return unwritable.
}
if (@fwrite($fp, 'directory access testing.') !== false)
{
$mark ^= 2; // The contents are writable and readable 011 , the directory is writable and unreadable 010
}
@fclose($fp);
@unlink($test_file);
/* Check that the directory is modifiable */
$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 execution rename() Permission of a function */
if (@rename($test_file, $test_file) !== false)
{
$mark ^= 8;
}
@unlink($test_file);
}
/* If it's a file */
elseif (is_file($file_path))
{
/* Open as a read */
$fp = @fopen($file_path, 'rb');
if ($fp)
{
$mark ^= 1; // Can be read 001
}
@fclose($fp);
/* Try to modify the file */
$fp = @fopen($file_path, 'ab+');
if ($fp && @fwrite($fp, '') !== false)
{
$mark ^= 6; // Modifiable, writable, and readable 111 , unmodifiable, writable and readable 011...
}
@fclose($fp);
/* Check the directory for execution rename() Permission of a 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 the directory exists
/****************************************************
* will xml Data flow, write to xml file
* @param $xmlData
* @return bool|string
*/
function writeXmlFile($xmlData)
{
$time = time(); // Gets a timestamp to name the file
$path = dirname(__FILE__); // Gets the current absolute path
$path = substr_replace($path, "", stripos($path, "actions\data")); // Replace the proper path where this file resides with empty
$path .= "xmlFiles\"; // Store directory name
/* Determine whether the target directory exists, does not exist, new */
if(!is_dir($path))
{
mkdir($path); // The new directory
}
/* Record the full path and file name */
$filePathAndName = $path.$time.".xml";
/* Open the file, file name < The time stamp > + <.xml>*/
$fp = fopen($filePathAndName, "w");
if(!$fp)
{
return false;
}
/* Write file stream */
$flag = fwrite($fp, $xmlData);
if(!$flag)
{
return false;
}
fclose($fp);
return $filePathAndName;
}