PHP checks whether a file or directory exists code summary

Source: Internet
Author: User
Tags readable file permissions

The following is a simple instance code that checks whether a file exists:

The code is as follows Copy Code

<?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:blablaphphello.txt does not exist.

You can also use the File_exists function to test whether a directory exists, as shown in the sample code:

The code is as follows Copy Code

if (file_exists ("C:\blabla\php"))
{echo "yes";}
Else
{echo ' no ';}


Instance

The code is as follows Copy Code

/**
* 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

The code is as follows Copy Code

/****************************************************
* Writes the XML data stream to the 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 current absolute path
$path = Substr_replace ($path, "", Stripos ($path, "Actions\data")); Replace the natural path where this file resides with empty
$path. = "Xmlfiles\"; Store Directory Name

/* To determine whether the target directory exists, does not exist, new * *
if (!is_dir ($path))
{
mkdir ($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;
}

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.