[英]Display all images from outside web root folder using PHP
I want to display all images that are stored outside my web root folder. 我想显示存储在我的Web根文件夹之外的所有图像。 Please help me.
请帮我。 I am only able to display one image repeatedly.
我只能重复显示一个图像。 For example, if I have 5 images in my folder, only one image is displayed on my browser 5 times.
例如,如果我的文件夹中有5个图像,则浏览器中只显示一个图像5次。 Please help me on this.
请帮帮我。 I've been working on this problem for over a month now.
我已经在这个问题上工作了一个多月了。 I'm a newbie.
我是新手。 Help.
救命。 Thank you.
谢谢。 Here is the code I'm using.
这是我正在使用的代码。
images.php 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 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();
}
?>
The problem is you're not parsing the QueryString variable you pass to call_images.php, but instead running the same database query, which will just return the first image that the database comes back with every time. 问题是你没有解析传递给call_images.php的QueryString变量,而是运行相同的数据库查询,它只返回数据库每次返回的第一个图像。 Here is a (hopefully) corrected version.
这是一个(希望)更正的版本。
<?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();
?>
What to know about these changes: 如何了解这些变化:
$fn = mysql_real_escape_string($_GET['imgPath']);
gets the variable you passed via querystring, and then escapes it so we can run it through the database again. mysql_result()
since we only needed one field's worth of data. mysql_result()
因为我们只需要一个字段的数据。 readfile()
for fpassthru()
, which requires a call to fopen, but does not buffer the contents of the file in memory. fpassthru()
切换readfile()
,这需要调用fopen,但不会在内存中缓冲文件的内容。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.