Php 如何在一个页面中为位于www根目录上方的多个图像提供服务?

Php 如何在一个页面中为位于www根目录上方的多个图像提供服务?,php,upload,symlink,image-gallery,Php,Upload,Symlink,Image Gallery,我希望为用户提供一个用户提交的图像库。我写了一个上传脚本,它将文件保存在www根目录之上。我知道我可以通过指定页眉,然后使用readfile来提供文件,但是我计划将图像放入一个表中,与其他信息一起显示,我认为页眉/readfile不是最好的解决方案。我想可能是符号链接,但我不确定 实现这一点的最佳方法是什么?您需要一个脚本,如getimage.php,它发送图像标题并输出echo的内容。然后在HTML中,您只需将其用作HTML中的。getimage.php的唯一目的是检索和输出图像。它与用于生成

我希望为用户提供一个用户提交的图像库。我写了一个上传脚本,它将文件保存在www根目录之上。我知道我可以通过指定页眉,然后使用readfile来提供文件,但是我计划将图像放入一个表中,与其他信息一起显示,我认为页眉/readfile不是最好的解决方案。我想可能是符号链接,但我不确定


实现这一点的最佳方法是什么?

您需要一个脚本,如
getimage.php
,它发送图像标题并输出echo的内容。然后在HTML中,您只需将其用作HTML中的
getimage.php
的唯一目的是检索和输出图像。它与用于生成发送到浏览器的HTML的任何PHP保持独立

此外,您可以检查用户是否具有在
getimage.php
中查看图像的有效会话和权限,如果没有,则发送某种类型的拒绝访问图像

getimage.php
的内容小而简单:

// Check user permissions if necessary...

// Retrieve your image from $_GET['imgId'] however appropriate to your file structure
// Whatever is necessary to determine the image file path from the imgId parameter.

// Output the image.
$img = file_get_contents("/path/to/image.jpg");
header("Content-type: image/jpeg");
echo($img);
exit();
在HTML中:

<!-- as many as you need -->
<img src='getimage.php?imgId=12345' />
<img src='getimage.php?imgId=23456' />
<img src='getimage.php?imgId=34567' />


然后,浏览器的任务就是调用
getimage.php?imgId=12345
作为图像的路径。浏览器不知道它调用的是PHP脚本,而不是web可访问目录中的图像。

如果脚本在Unix服务器上运行,您可以尝试在web根目录中创建一个符号链接,链接到web根目录之外的目录

ln -s /webroot/pictures /outside-of-webroot/uploads
如果您使用的是Apache服务器,还可以查看mod_别名。
我听说在使用mod_alias并通过.htaccess配置它时会出现一些问题。不幸的是,我对mod_alias没有任何经验。

让用户直接将他们的图像上传到我的mysql数据库中,这对我来说一直都很有效。PHP将编码为base64并存储到blob中。然后你做一些与michael所说的相似的事情来检索和显示图像。我在2008年的一个项目中加入了一些代码。如果这是您感兴趣使用的方法,我不会完全复制它,因为它是旧代码

这是要上传并存储到数据库中的PHP。显然,替换您的信息并连接到您自己的数据库

<?php
include("auth.php");
// uploadimg.php               
// By Tyler Biscoe             
// 09 Mar 2008                 
// Test file for image uploads
include("connect.php");
include("include/header.php");
$max_file_size = 786432;
$max_kb = $max_file_size/1024;

if($_POST["imgsubmit"])
{
if($_FILES["file"]["size"] > $max_file_size)
    {
        $error = "Error: File size must be under ". $max_kb . " kb.";
    }

if (!($_FILES["file"]["type"] == "image/gif") && !($_FILES["file"]["type"] == "image/jpeg") && !($_FILES["file"]["type"] == "image/pjpeg"))
    {
        $error .= "Error: Invalid file type. Use gif or jpg files only.";

    }

if(!$error)
    {
        echo "<div id='alertBox'> Image has been successfully uploaded! </div>";
        $handle = fopen($_FILES["file"]["tmp_name"],'r');
        $file_content = fread($handle,$_FILES["file"]["size"]);
        fclose($handle);
        $encoded = chunk_split(base64_encode($file_content)); 
        $id = $_POST["userid"];
        echo $_FILES["file"]["tmp_name"];
        $default_exist_sql = "SELECT * FROM members WHERE id='".$id."'";
        $default_result = mysql_query($default_exist_sql);
        $results = mysql_fetch_array($default_result);
        if(!$results["default_image"])
        {
            $insert_sql = "UPDATE members SET default_image = '$encoded' WHERE id='". $id ."'";
            mysql_query($insert_sql);

        }

        $sql = "INSERT INTO images (userid, sixfourdata) VALUES ('$id','$encoded')";
        mysql_query($sql);
    }
    else
    {
        echo "<div id='alertBox'>". $error . "</div>";
    }  
}
?>
<br />
<font class="heading"> Upload images </font>
<br /><br />

    <form enctype = "multipart/form-data" action = "<?php $_SERVER['PHP_SELF']; ?>" method =        "post" name = "uploadImage">
<input type = "hidden" name="userid" value = "<?php echo $_GET["userid"]; ?>" >
<input id="stextBox" type="file" name="file" size="35"><br />
<input type="submit" name="imgsubmit" value="Upload">
</form>
<?php include("include/footer.php"); ?>

通过对它们进行符号链接,您放弃了将它们保持在文档根目录之上所获得的大部分安全性。除非出于某种原因绝对至关重要,否则我不会将图像存储在数据库中。编码、存储、检索、解码是一个很长的过程,还有一些效率很低的数据库。我想这取决于应用程序。多年来,我已经无数次地使用这个脚本,或者某种形式的脚本。我测试中的base64解码(从08年开始,所以我的记忆可能有缺陷)是正常的。此外,作为会员制的一部分,用户拥有自己的图像库,您已经可以查询数据库,查看他与哪些图像关联。还应注意,编码和存储仅在上传时完成。和检索(无论如何都在进行)和解码是分开的。无论如何,我总是乐于接受教育,所以告诉我哪里错了!!:)谢谢你的回答。我不知道您可以在
标记中调用PHP脚本。
<?php
// image.php                    
// By Tyler Biscoe              
// 09 Mar 2008                  
// File used to display pictures
include("connect.php");

$imgid = $_GET["id"];

$result = mysql_query("SELECT * FROM images WHERE imgid=" . $imgid . "");

$image = mysql_fetch_array($result);
echo base64_decode($image["sixfourdata"]);
echo $image["sixfourdata"];

?>
<img src="image.php?id=your_img_id">