Home  >  Article  >  Backend Development  >  How to get all the file names in the directory and determine the files and directories in php

How to get all the file names in the directory and determine the files and directories in php

WBOY
WBOYOriginal
2016-07-25 08:56:52855browse
  1. //Open the subdirectory common under the directory pic in the current directory.
  2. $handler = opendir('pic/common');
Copy code

2. Loop to read all files in the directory

  1. /*where $filename = readdir($handler)
  2. Every During the second loop, the read file name is assigned to $filename, $filename !== false.
  3. Be sure to use !==, because if a file name is called '0', or something is considered false by the system, using != will stop the loop
  4. */
  5. while( ($filename = readdir($ handler)) !== false )
  6. {
  7. //Skip files named '.' and '..' in the linux directory
  8. if($filename != “.” && $filename != “..”)
  9. {
  10. //Output file name
  11. echo $filename;
  12. }
  13. }
Copy code

3. Close the directory

  1. closedir($handler);
Copy code

Two ,php determines file and directory functions Code:

  1. //Check whether the target object logo.jpg in the upper-level directory is a file.
  2. $checkResult = is_file(’../logo.jpg’);
Copy code

Description: If the target object is a file, the system returns true, otherwise it returns false.

Code:

  1. //Check whether the target object logo.jpg in the upper-level directory is a directory.
  2. $checkResult = is_dir(’../logo.jpg’);
Copy code

Description: If the target object is a directory system, return true, otherwise return false.



Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn