2011-08-09 35 views
-3

可能重複:
List all folders on my computer (php)PHP - 在localhost目錄文件夾和文件

我曾嘗試:

$handle = opendir($path); 

不過的路徑是什麼?我把除廚房水槽以外的所有東西都放在裏面!我無法讓它工作。我現在在我的本地主機上。

我所做的:

opendir(dirname(__FILE__)); 

這裏是一個要工作......

$dir = dirname(__FILE__); 

// Open a known directory, and proceed to read its contents 
if(is_dir($dir)) 
{ 
if($dh = opendir($dir)) 
    { 
    while(($file = readdir($dh)) !== false) 
     { 
     echo "filename: ".$file."<br />"; 
     } 
    closedir($dh); 
    } 
} 

會做一些清潔拿到我是想的信息。但是,感謝Stackoverflow上的「一些」,我更喜歡這個代碼更適合localhost應用程序。

foreach(glob("*") as $filename) 
{ 
echo $filename."<br />"; 
} 

回答

4

您正在尋找glob(簡單的東西)或DirectoryIterator(更多的面向對象方法)。 C:

<?php 
// all files in current directory (including '.' and '..') 
foreach (glob("*") as $filename) { 
    echo "$filename size " . filesize($filename) . "\n"; 
} 
?> 

<?php 
// all files in current directory (excluding'.' and '..') 
$dir = new DirectoryIterator(dirname(__FILE__)); 
foreach ($dir as $fileinfo) { 
    if (!$fileinfo->isDot()) { 
     var_dump($fileinfo->getFilename()); 
    } 
} 
?> 
+0

嘿!謝啦!我完全仰望glob(),這正是我需要的。完美的本地主機,易於使用,OMG謝謝。 – MP123

+0

你是男人,是一個真正的職業。你讓另一個所謂的專業人士看起來像信息高速公路上的一個鍋洞。保持良好的工作! – MP123

+0

「完美的本地主機」...是否有一個原因是指你的計算機的DNS主機名指向127.0.0.1?它是否像你的寵物暱稱? Spock真的會被這種風俗所吸引。 –

5

$path是路徑要打開的目錄。

c:\users\MP123\Photos

/home/MP123/Photos

這是真的「讀the PHP manual,這對如何列出文件夾和文件的完整的例子」,而不是「問我問題的幫助專業人士」鍵入主題。

+0

我只是想(從W /一些修改相應的文檔頁面的例子)\的Documents and Settings \用戶\桌面\照片,並得到資源ID#227。不知道這意味着什麼? – MP123

+1

@ MP123你甚至讀過'opendir()'手冊嗎?請特別注意標記爲「返回值」的部分(http://php.net/manual/en/function.opendir.php#refsect1-function.opendir-returnvalues)。正如丹提到,手冊頁也充滿了示例 – Phil

+0

@ MP123它意味着你打開該目錄,現在有一個句柄指向它...現在複製並粘貼鏈接手冊頁的代碼,以列出那裏的文件和文件夾。 –