2013-06-23 68 views
0

大家好,我試圖讀取一個名爲php,html,png,jpeg和其他格式的人名。我試圖過濾我想要phphtml文件的文件,然後將它們回顯出來。如何讀取目錄和過濾文件,然後將它們回顯出來?

這裏是我的嘗試,因爲它沒有任何echo文件,沒有工作:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml"> 
<head> 
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 
<title>Khviii ICT</title> 
</head> 

<body> 
<div align="center" ><h1>Welcome to KHVIII ICT</h1></div> 
<?php 
$dir="/people"; 
$allowed_view= array("php","html"); 
$files=scandir($dir); 
foreach ($files as $file) 
{ 
     $filexplosion=strtolower(end(explode('.',$file))); 
    if (in_array($allowed_view,$filexplosion)) 
     { 
    echo $file; 
    } 
} 
?> 
</body> 
</html> 
+2

隨着'水珠()'函數,一個後綴符'{JPEG,HTML}'和'GLOB_BRACE'。 – mario

+1

'/ people'可能是錯誤的 - 您需要使用絕對文件系統路徑。您的網絡主持人或管理員將能夠告訴您路徑。 –

+0

試試這個答案http://stackoverflow.com/questions/17250586/deleting-all-files-in-except-the-one-running-the-delete-code/17250780#17250780 – bystwn22

回答

0

後幾無二致我已經工作了,我使用蓋爾布()。 下面是代碼:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml"> 
<head> 
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 
<title>khvii index</title> 
</head> 

<body> 
<?php 
foreach (glob("people/"."*.php",GLOB_BRACE) as $filename) { 
    $phpexplode = end(explode("/",$filename)); 
    echo'<a href="'.$filename.'"><input type="button" value="'.$phpexplode.'"></a><br />'; 
} 
foreach (glob("people/"."*.html",GLOB_BRACE) as $filename) { 
    $htmlexplode = end(explode("/",$filename)); 
    echo'<a href="'.$filename.'"><input type="button" value="'.$htmlexplode.'"></a><br />'; 
} 
?> 
</body> 
</html> 
0

目錄路徑是不正確的in_array參數不是爲了。修正PHP代碼是如下:

$dir = __DIR__ . "/people"; 
    $allowed_view = array("php", "html"); 
    $files = scandir($dir); 
    foreach ($files as $file) { 
     $filexplosion = strtolower(end(explode('.', $file))); 
     if (in_array($filexplosion, $allowed_view)) { 
      echo $file; 
     } 
    } 
0

試試這個:

$files=scandir($dir); 
echo "I have found " . count($files) . " files<br>"; // Are you sure there are files found? 
foreach ($files as $file) 
{ 
$filexplosion = substr($file, strrpos($file, ".")); 
echo $filexplosion; // just to check if you're getting the right value 
if (in_array($allowed_view,$filexplosion)){ 
    echo $file; 
} 
+0

這工作,但不是作爲有效做glob() –

+0

是的,你說得對。好答案 :-) – Richard

相關問題