开发者

PHP : Finding Files on Directory

开发者 https://www.devze.com 2022-12-14 23:23 出处:网络
Is it possible to f开发者_如何学编程ind a file on C:/ (windows) using php script? If yes, is there any manual of sample code/workaround.

Is it possible to f开发者_如何学编程ind a file on C:/ (windows) using php script? If yes, is there any manual of sample code/workaround.

Edit : The webserver are on same PC as C:/ Directory in.

Thank you


If you're looking for a recursive search you might be interested in the spl's 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;
  }
}


As long as you only want to look in one directory, then yes, coding it using the PHP functions is quicker. But if you want to search recursively through the tree for a particular filename then it'll probably be a lot quicker to shell out:

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

But I believe that these days NT has file indexing built in to the OS - so there must be hooks exposed somewhere for an even faster search.

C.


I use scandir for this purpose, like so:

$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>';

Go read up on http://php.net/manual/en/function.scandir.php And for help on manululating the resulting array: http://php.net/manual/en/ref.array.php


If you are talking about the server on whcih PHP is installed, it should be infinitely possible with the PHP file command, as long as you are accessing areas of the HDD that share permissions with the webserver. The first example on PHP.net says as much ...

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

If you are talking about your user's machine, they can select a file to upload using a <input id="uploader" name="uploader" type="file"> form element, but you cannot browse it using any server side language, because it is exactly that.


Unless you're talking about the C: drive on the server, or a command-line version of PHP running on your Windows machine, PHP as a server side language, doesn't have access to your C: drive.

If you are referring to the C: drive on the server, then the readdir function (as haim evgi suggested) should work for you.

Remember to read the warning on the readdir reference page, and remember to open the directory first. The function reference provides sample code.


I assume you want to run from the actual PHP interpreter (ie as a local script), you can use SPL(php 5)

see here and here for just 2 examples. More examples if you read the PHP manual and lots of others on the web if you search hard enough.

0

精彩评论

暂无评论...
验证码 换一张
取 消