我想要顯示存儲在我的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();
}
?>
偏題:你應該選擇並縮進樣式(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