3

Web ルート フォルダの外に保存されているすべての画像を表示したいと考えています。私を助けてください。1 つの画像を繰り返し表示することしかできません。たとえば、フォルダに 5 つの画像がある場合、ブラウザには 1 つの画像だけが 5 回表示されます。これについて私を助けてください。私はこの問題に1か月以上取り組んできました。私は初心者です。ヘルプ。ありがとうございました。これが私が使用しているコードです。

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();

}
?>
4

1 に答える 1

3

問題は、call_images.php に渡す QueryString 変数を解析するのではなく、同じデータベース クエリを実行していることです。これは、データベースが毎回返す最初の画像を返すだけです。これは(うまくいけば)修正されたバージョンです。

<?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()1 フィールド分のデータしか必要なかったので、これを使用しました。
  • readfile()fopenのfpassthru()呼び出しが必要ですが、ファイルの内容をメモリにバッファリングしません。
于 2010-05-03T23:27:45.640 に答える