Scan files in a directory and sub-directory and store their path in array using php

Asked
Active3 hr before
Viewed126 times

9 Answers

usingdirectoryfiles
90%

I want not scan all the files in a directory and its sub-directory. And get their path in an array. Like path to the file in the directory in array will be just ,I am able to scan single directory, but it returns all the files and sub-directories without any ways to differentiate. ,What is the best way to get all the files in the directory and sub-directory with its path?,$file is an SPLFileInfo-object. Its __toString() method will give you the filename, but there are several other methods that are useful as well!

Using the DirectoryIterator from SPL is probably the best way to do it:

$it = new RecursiveIteratorIterator(new RecursiveDirectoryIterator('.'));
foreach($it as $file) echo $file.
"\n";
load more v
88%

Returns an array of filenames on success, or false on failure. If directory is not a directory, then boolean false is returned, and an error of level E_WARNING is generated. ,scandir — List files and directories inside the specified path, Returns an array of files and directories from the directory. ,A URL can be used as a filename with this function if the fopen wrappers have been enabled. See fopen() for more details on how to specify the filename. See the Supported Protocols and Wrappers for links to information about what abilities the various wrappers have, notes on their usage, and information on any predefined variables they may provide.

Array
   (
      [0] => .
      [1] => ..
      [2] => bar.php[3] => foo.txt[4] => somedir
   )
Array
   (
      [0] => somedir[1] => foo.txt[2] => bar.php[3] => ..
      [4] => .
   )
72%

How to get names of all the subfolders and files present in a directory using PHP?,PHP program to print an arithmetic progression series using inbuilt functions,How to find out where a function is defined using PHP ?,How to get the time of the last modification of the current page in PHP?

65%

When using the local driver, all file operations are relative to the root directory defined in your filesystems configuration file. By default, this value is set to the storage/app directory. Therefore, the following method would write to storage/app/example.txt:,When using the local driver, public visibility translates to 0755 permissions for directories and 0644 permissions for files. You can modify the permissions mappings in your application's filesystems configuration file:,Once you have created and registered the extension's service provider, you may use the dropbox driver in your config/filesystems.php configuration file.,The public disk included in your application's filesystems configuration file is intended for files that are going to be publicly accessible. By default, the public disk uses the local driver and stores its files in storage/app/public.

When using the local driver, all file operations are relative to the root directory defined in your filesystems configuration file. By default, this value is set to the storage/app directory. Therefore, the following method would write to storage/app/example.txt:

use Illuminate\ Support\ Facades\ Storage;

Storage::disk('local') - > put('example.txt', 'Contents');
load more v
75%

$path    = './';
$files = scandir($path);
$files = array_diff(scandir($path), array('.', '..'));
foreach($files as $file){
  echo "<a href='$file'>$file</a>";
}
load more v
40%

Then we can setup a new folder instance:,Path of the current folder. Folder::pwd() will return the same information.,Returns the current folder.,The Folder and File utilities are convenience classes to help you read from and write/append to files; list files within a folder and other common directory related tasks.

use Cake\ Filesystem\ Folder;
use Cake\ Filesystem\ File;
load more v
22%

You can use the PHP scandir() function to list files and directories inside the specified path.,You can create a new and empty directory by calling the PHP mkdir() function with the path and name of the directory to be created, as shown in the example below:,The PHP code in the following example will search the documents directory and list all the files with .text extension. It will not search the subdirectories.,To make the mkdir() function work, the parent directories in the directory path parameter has to exist already, for example, if you specify the directory path as testdir/subdir than the testdir has to exist otherwise PHP will generate an error.

< ? php
// The directory path
$dir = "testdir";

// Check the existence of directory
if (!file_exists($dir)) {
   // Attempt to create directory
   if (mkdir($dir)) {
      echo "Directory created successfully.";
   } else {
      echo "ERROR: Directory could not be created.";
   }
} else {
   echo "ERROR: Directory already exists.";
} ?
>
load more v
60%

Paths are almost always relative to your main index.php file.,If the second parameter is set to true, any directories contained within the supplied root path will be deleted as well.,Sub-folders contained within the directory will be mapped as well. If you wish to control the recursion depth, you can do so using the second parameter (integer). A depth of 1 will only map the top level directory:,An array containing info on the supplied directory’s contents

helper('filesystem');
load more v
48%

A couple of weeks ago I posted how to read through a directory with PHP using the opendir() readdir() and closedir() functions and now look at the glob() function. glob() returns the filenames into an array and supports pattern matching so it can be very easy to find e.g. jpg images in a particular directory.,This serves as an introduction to using glob() to find files with PHP. Read the glob manual page for more details and for information about the other flags.,If no files matched the pattern then the array will be empty.,Remove directory Linux

bar.txt A regular file
baz A directory
foo.txt A regular file
link2foo.txt A symbolic link to foo.txt
load more v

Other "using-directory" queries related to "Scan files in a directory and sub-directory and store their path in array using php"