2010-05-03 80 views
3

我想要顯示存儲在我的Web根文件夾之外的所有圖像。請幫幫我。我只能重複顯示一個圖像。例如,如果我的文件夾中有5個圖像,我的瀏覽器上僅顯示一個圖像5次。請幫助我。我已經在這個問題上工作了一個多月了。我是新手。幫幫我。謝謝。這是我正在使用的代碼。使用PHP顯示來自外部Web根文件夾的所有圖像

images.php

<?php 
    // Get our database connector 
require("includes/copta.php"); 

// Grab the data from our people table 
$sql = "select * from people"; 

$result = mysql_query($sql) or die ("Could not access DB: " . mysql_error()); 

$imgLocation = " /uploadfile/"; 

while ($row = mysql_fetch_array($result)) 
{ 
    $imgName = $row["filename"]; 
    $imgPath = $imgLocation . $imgName; 

    echo "<img src=\"call_images.php?imgPath=" . $imgName . "\" alt=\"\"><br/>"; 
    echo $row['id'] . " " . $imgName. "<br />"; 

} 

?> 

call_images.php

<?php 
    // Get our database connector 
require("includes/copta.php"); 

$imgLocation = '/ uploadz/'; 

$sql = "select * from people"; 

$result = mysql_query($sql) or 
    die ("Could not access DB: " . mysql_error()); 

while ($row = mysql_fetch_array($result)) { 

    $imgName = $row["filename"]; 
    $imgPath = $imgLocation . $imgName; 


    // Make sure the file exists 
    if(!file_exists($imgPath) || !is_file($imgPath)) { 
     header('HTTP/1.0 404 Not Found'); 
     die('The file does not exist'); 
    } 

    // Make sure the file is an image 
    $imgData = getimagesize($imgPath); 
    if(!$imgData) { 
     header('HTTP/1.0 403 Forbidden'); 
     die('The file you requested is not an image.'); 
    } 


    // Set the appropriate content-type 
    // and provide the content-length. 

    header("Pragma: public"); 
    header("Expires: 0"); 
    header("Cache-Control: must-revalidate, post-check=0, pre-check=0"); 

    header("Content-Type: image/jpg"); 
    header("Content-length: " . filesize($imgPath)); 

    // Print the image data 
    readfile($imgPath); 
    exit(); 

} 
?> 
+0

偏題:你應該選擇並縮進樣式(http://en.wikipedia.org/wiki/Indent_style)並一致地應用它。 ''SELECT *'通常是錯誤的做法(http://stackoverflow.com/questions/321299/what-is-the-reason-not-to-use-select),就像'或死(... )'(http://www.phpfreaks.com/blog/or-die-must-die,雖然你可以在這種情況下爲它做一個例子,因爲你不輸出格式良好的文檔)並輸出mysql_error所有人都可以看到(http://msdn.microsoft.com/zh-cn/library/ms995351.aspx#securityerrormessages_topic2)。 – outis 2010-05-04 00:19:16

回答

3

問題是你不解析查詢字符串變量傳遞給call_images.php,而是運行相同的數據庫查詢,這將僅返回數據庫每次返回的第一個映像。這是一個(希望)更正版本。

<?php 
// Get our database connector 
require("includes/copta.php"); 

$imgLocation = '/ uploadz/'; 

$fn = mysql_real_escape_string($_GET['imgPath']); 

$sql = "select filename from people WHERE filename = '{$fn}'"; 

$result = mysql_query($sql) or 
    die ("Could not access DB: " . mysql_error()); 

if (mysql_num_rows($result) == 0) { 
    header('HTTP/1.0 404 Not Found'); 
    die('The file does not exist'); 
} 
$imgName = mysql_result($result, 0, 0); 
$imgPath = $imgLocation . $imgName; 

// Make sure the file exists 
if(!file_exists($imgPath) || !is_file($imgPath)) { 
    header('HTTP/1.0 404 Not Found'); 
    die('The file does not exist'); 
} 

// Make sure the file is an image 
$imgData = getimagesize($imgPath); 
if(!$imgData) { 
    header('HTTP/1.0 403 Forbidden'); 
    die('The file you requested is not an image.'); 
} 


// Set the appropriate content-type 
// and provide the content-length. 

header("Pragma: public"); 
header("Expires: 0"); 
header("Cache-Control: must-revalidate, post-check=0, pre-check=0"); 

header("Content-Type: image/jpg"); 
header("Content-length: " . filesize($imgPath)); 

// Print the image data 
readfile($imgPath); 
exit(); 
?> 

瞭解這些變化是什麼:

  • $fn = mysql_real_escape_string($_GET['imgPath']);讓你通過查詢字符串傳遞的變量,然後逃逸,所以我們可以再次通過數據庫運行它。通過這種方式,我們可以確定用戶沒有使用相對路徑來嘗試公開他們不應該訪問的映像(除非您有數據庫記錄;安全性是您所做的)。
  • 我完全刪除了循環,沒有必要
  • 我用mysql_result()因爲我們只需要一個字段的數據。
  • 我建議切換readfile()fpassthru(),這需要調用fopen,但不會緩衝內存中文件的內容。
+0

codeline: $ fn = mysql_real_escape_string($ _ GET ['imgPath']);似乎沒有工作。當我通過回顯瀏覽器進行檢查時,它不會產生任何值。請驗證 – user331859 2010-05-04 06:57:51

+0

請將以下代碼添加到頁面頂部併發布輸出:'die(print_r($ _ GET,true));' – Dereleased 2010-05-04 18:02:57

+0

此代碼仍然不起作用。請我急需幫助。我哪裏出錯了? – user331859 2010-06-20 17:55:52

相關問題