PHP: searching for files in a directory

Is it possible to find a file in C: / (windows) using php script? If yes, is there any tutorial on sample code / workaround.

Edit: The web server is on the same PC as C: / Directory.

thank

+3
source share
6 answers

If you are looking for a recursive search, you might be interested in spl RecursiveDirectoryIterator .

<?php
$path = 'C:/';
$file = 'issetup.exe';

$rdi = new RecursiveDirectoryIterator($path);
$rit = new RecursiveIteratorIterator($rdi);
foreach( $rit as $path=>$info ) {
  if ( $file===$info->getFilename() ) {
    echo $path, "\n";
    break;
  }
}
+3
source

While you want to look in only one directory, then yes, encoding it using PHP functions is faster. But if you want to search recursively through the tree for a specific file name, then it will probably be much faster to lay out:

$cmd="dir $fname /s"
chdir("C:/");
$found=explode("\n",`$cmd`);

, NT , , - - .

.

+2

scandir , :

$path = $_SERVER['DOCUMENT_ROOT'];
$files = scandir($path);
echo '<pre>';
if (count($files) > 2) {
  // first 2 entries are '.' and '..'
  $files = array_slice($files, 2);
  print_r($files);
}
echo '</pre>';

http://php.net/manual/en/function.scandir.php manululating : http://php.net/manual/en/ref.array.php

+1

, PHP, PHP, , -. PHP.net ...

http://php.net/manual/en/function.file.php

, <input id="uploader" name="uploader" type="file">, , .

0

C: PHP, Windows, PHP C:.

C: , readdir ( ) .

readdir . .

0

, PHP ( script), SPL (php 5)

see here and here in just 2 examples. Additional examples if you are reading the PHP manual and many others on the Internet if you are thoroughly searching.

0
source

Source: https://habr.com/ru/post/1726522/


All Articles