Check whether the file or directory has an instance code in PHP

Source: Internet
Author: User
Check whether the file or directory exists, we use PHP commonly used function file_exists, this function can realize I want to function, below everybody slowly reference

The following is an example code that checks whether a file exists:

The code is 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 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:

The code is as follows:

if (file_exists ("C:\blabla\php")) {echo "yes";} else {echo "No";}

Instance

The 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, the four-bit high-to-low represents the * executable rename () function permission, the ability to append content to the file, the writable file permission, the readable file permission. */function File_mode_info ($file _path) {/* If not present, unreadable, not writable, not 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 the directory */if (Is_dir ($file _path)) {/* Check the directory is readable */$dir = @opendir ($file _path); if ($dir = = = False) {return $mark;//IF Directory Open failed, directly return directory non-modifiable, non-writable, unreadable} if (@readdir ($dir)!== false) {$mark ^= 1;//directory readable 001, directory unreadable} @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, 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 to see if 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 permissions to execute the rename () function */if (@rename ($test _file, $test _file)!== false) {$mark ^= 8;} @unlink ($test _file); }/* If it is file */ElseIf (Is_file ($file _path)) {/* Read Open */$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 to writable readable 111, non-modifiable writable readable 011 ...} @fclose ($FP); /* Check the directory for permissions 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 ^=;}} return $mark; }

PHP determines if a directory exists

The code is as follows:

/**************************************************** * XML data stream, write to XML file * @param $xmlData * @return bool|string * * function Writexmlfile ($xmlData) {$time = time ();//get timestamp for naming the file $path = dirname (file);//Get the current absolute path $path = Substr_replace ($path, "", Stripos ($path, "Actions\data")); Replace the intrinsic path where this file resides with an empty $path. = "Xmlfiles\"; Store Directory Name/* To determine if the target directory exists, not present the new */if (!is_dir ($path)) {mkdir ($path);//New Directory}/* Record the full path and file name */$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.