Guide to Using is_file () functions in PHP
In php, The is_file () function is used to determine whether a file exists. It is also very simple to use. If you need it, refer to it.
The is_file () function checks whether the specified file name is a normal file.
Is_file-Tells whether the filename is a regular file
Usage:
Bool is_file (string $ filename) $ file is a required Parameter
Returns TRUE if the object exists and is normal.
Let's take a look at Example 1:
?
| 1 2 3 4 |
<? Php Var_dump(is_file('a_file.txt '). "\ n "; Var_dump (is_file ('/usr/bin/'). "\ n "; ?> |
The above example will output:
Bool (true)
Bool (false)
Example 2:
?
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
<? Php Function isfile ($ file ){ Return preg_match ('/^ [^. ^: ^? ^-] [^: ^?] *.(? I) '. getexts ().' $/', $ file ); // First character cannot be .:? -Subsequent characters can't be :? // Then a. character and must end with one of your extentions // Getexts () can be replaced with your extentions pattern } Function getexts (){ // List acceptable file extensions here Return '(app | avi | doc | docx | exe | ico | mid | midi | mov | mp3 | Mpg | mpeg | pdf | psd | qt | ra | ram | rm | rtf | txt | wav | word | xls )'; } Echo isfile ('/Users/YourUserName/Sites/index.html '); ?> |
Example 3:
?
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
<? Php Function deletefolder ($ path) { If ($ handle = opendir ($ path )) { While (false! ==( $ File = readdir ($ handle ))) { If ($ file <> "." AND $ file <> "..") { If (is_file ($ path. '/'. $ file )) { @ Unlink ($ path. '/'. $ file ); } If (is_dir ($ path. '/'. $ file )) { Deletefolder ($ path. '/'. $ file ); @ Rmdir ($ path. '/'. $ file ); } } } } } ?> |
This function deletes all files and folders.
The above is all the content of this article. I hope you will like it.