2014-02-21 63 views
-2

你好,我使用此代碼輸出圖像從一個文件名爲圖像水珠功能輸出的所有文件

<?php 
//Path to folder which contains images 
$dirname = $_POST['dir']; 

//Use glob function to get the files 
//Note that we have used " * " inside this function. If you want to get only JPEG or PNG use 
//below line and commnent $images variable currently in use 
$images = glob($dirname."*"); 

//Display image using foreach loop 
foreach($images as $image) { 

//print the image to browser with anchor tag (Use if you want really :)) 
echo '<p><img style="height:176px; width:221px; float:right;" src="'.$image.'" /></p>'; 
} 
?> 

我把它叫做image.php,我用這種形式讓用戶選擇目錄

<form method="POST" action="image.php"> 
<input type="text" name="dir" placeholder="choose directory" /> 
<input type="submit" value="choose" /> 
</form> 

當我運行代碼它輸出文件夾中的所有文件我有文件如test.php和文件作爲js和css和一個名爲圖像的文件當我運行它時輸出js和css以及所有.php文件文件把它不輸出圖像什麼錯誤

+0

可能重複http://stackoverflow.com/questions/19543136/php -foreach和 - 圓頂封裝體功能) –

回答

1

這是非常危險的。任何人都可以鍵入他們想要的任何文件夾,並立即看到(並在PHP文件的情況下,執行)該目錄中的所有文件。

注意在代碼中關於「只想JPEG或PNG」的評論,那麼你可以這樣做:

$images = glob($dirname."*.{png,jpg,jpeg,gif}",GLOB_BRACE); 

這將只允許這些類型的圖像文件。

你也應該知道glob不是遞歸的。你必須手動遞歸通過目錄。

0

還是這個,如果你不希望過濾擴展:

//Path to folder which contains images 
$dirname = $_POST['dir']; 

//Use glob function to get the files 
//Note that we have used " * " inside this function. If you want to get only JPEG or PNG use 
//below line and commnent $images variable currently in use 
$images = glob($dirname."/"."*"); 

//Display image using foreach loop 
foreach($images as $image) { 

//print the image to browser with anchor tag (Use if you want really :)) 
echo '<p><img style="height:176px; width:221px; float:right;" src="'.$image.'" /></p>'; 
} 
[PHP的foreach和水珠()函數(的