Differences between is_file file_exists

Source: Internet
Author: User
Tags time 0

Is_file: checks whether a file exists and whether the specified file name is a normal file. file_exists checks whether the file exists or whether the directory exists. is_dir checks whether the directory exists. Check the manual, although the results of both functions are cached, The is_file speed is N times faster. It is worth noting that when a file exists, is_file is N times faster than file_exists. If the file does not exist, is_file is slower than file_exists. The conclusion is, the file_exits function does not affect the speed because the file actually exists. However, the effect of is_file is significant. So the first time I read The py code, I found that it is used like this: if (file_exists (DATA_DIR .'~ Runtime. php') & is_file (DATA_DIR .'~ Runtime. php') & IS_DEPLOY) {include_once DATA_DIR .'~ Runtime. php';} else {......} Daniel is Daniel. The code is profound, but the following code: for ($ I = 0; $ I <10000; $ I ++) {is_file (_ ROOT __. '/App. php '); file_exists (_ ROOT __. '/App. php ');} The XDebug test result is: php: is_file ------------- total self: 0.5 mstotal cum: 0.5 mscils: 10,000 php: file_exists ------------- total self: 41 mstotal cum: 41. mscils: 10,000 is used for the first time. When there is no file, file_exists is used to judge whether it is faster than is_file. In the future, file_exists is still used, but it is much slower than is_file and requires both functions to be used. So ~ In that case, The py code is really not worth reference. You can use is_file () directly to compile php. I will think of efficiency and efficiency at any time, because you never know the host status of the website that runs your program. In addition, most hosts have restrictions on iis and cpu usage. Do not purchase any hosts that are not restricted. I have always used file-exits to determine whether a file exists, that day I found that is_file was faster than file_exits and wrote a code to test $ start_time = get_microtime (); for ($ I = 0; $ I <10000; $ I ++) {if(is_file('url.txt ') {// do nothing;} echo 'is _ file time-consuming --> '. (get_microtime ()-$ start_time ). '<br>'; $ start_time = get_microtime (); for ($ I = 0; $ I <10000; $ I ++) {if(file_exists('url.txt ')) {// do nothing;} echo 'file _ exits --> '. (get_microtime ()-$ Start_time ). '<br>'; function get_microtime () // time {list ($ usec, $ sec) = explode ('', microtime (); return (float) $ usec + (floatemediate‑secure‑example example) is a one-second test, and you will find a strange result. 1.if url.txt does exist, is_file takes about 0.007sec. file_exists and 0.16sec. is_file functions are indeed at an astonishing speed! However, it may seem a little too early. 2. If url.txt does not have is_file time 0.53sec. file_exists time 0.21sec.3. it seems that the file_exits function does not affect the speed because the file actually exists, but is_file has a big impact. Tip and comment: the result of the is_file (file) function will be cached. Use clearstatcache () to clear the cache. The main content of the following section is: file_exists () is affected by the execution permission of the directory:
Observe the permissions: testhas No permissions for www. the execution file index.php has the read and write permissions, and the execution file test.txt has the read and write permissions. Running result: the execution permission of the Directory affects file_exists () 1. Grant test the maximum permission-755. As a result, the file_exists () function is restricted by the directory permission. What directory permissions will affect file_exists? I have done a few experiments: 1. Any parent directory of the file. The file does not exist only when the write permission is enabled; 2. Any parent directory of the file reports that the file does not exist only when the read permission is limited; 3. When all the parent directories have the execution permission, the system reports that the file exists and everything works normally. The result indicates that when determining whether a file exists, file_exists () recursively determines whether each directory has the execution permission. Change the file path to a relative path to get the same result. The summary is not mentioned in the php manual. file_exists is affected by directory execution permissions. This article tells developers who have problems in this regard. This is the only rule. The additional information is very useful for your remarks. After your comments, I checked the manual again. Indeed, if the directory does not have the execution permission, the php-fpm application process will not be able to search for files under this directory, of course, the file does not exist (actually there is no executable directory ). The content of the manual, which can be considered together, is mainly the execution permission. I will not discuss the read and write permissions in detail here (it is generally easy to understand ).
Clearly, the manual provides clear instructions on directory execution permissions. The execution permission on the directory is the right to be searched.

Related Article

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.