如何在单个页面内提供位于 www 根目录上方的多个图像?

发布于 2024-11-19 06:02:51 字数 177 浏览 1 评论 0原文

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

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

I am hoping to offer users a user submitted image gallery. I have written a upload script which saves the file above the www root. I know I can serve the file by specifying the page header and then using readfile, however I am planning to throw the images within a table to be displayed with other information and dont think the header/readfile is the best solution. I am thinking maybe symlinks but I am not sure.

What is the best method to achieve this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

稚然 2024-11-26 06:02:51

您需要一个像 getimage.php 这样的脚本,它发送图像标题并回显其内容。然后在 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 可访问目录中的图像。

You'll need a script like getimage.php which sends the image headers and echo's out its contents. Then in your HTML, you just utilize it as the <img src=''> in your HTML. The only purpose of getimage.php is to retrieve and output the image. It remains separate from whatever PHP you use to generate the HTML sent to the browser.

Additionally, you can check if the user has a valid session and permission to view the image in getimage.php and if not, send a some kind of access-denied image instead.

The contents of getimage.php are small and simple:

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

In your HTML:

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

It then becomes the browser's job to call getimage.php?imgId=12345 as the path to the image. The browser has no idea it is calling a PHP script, rather than an image in a web accessible directory.

好久不见√ 2024-11-26 06:02:51

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

ln -s /webroot/pictures /outside-of-webroot/uploads

如果您使用 Apache 服务器,您还可以查看 mod_alias。
我听说使用 mod_alias 并通过 .htaccess 配置它时存在一些问题。不幸的是我对 mod_alias 没有任何经验。

If the script is running on a Unix server, you might try to create a symlink in your web root that links to the directory outside of your web root.

ln -s /webroot/pictures /outside-of-webroot/uploads

If you're using an Apache server you could also have a look at mod_alias.
I've heard that there are a few issues when using mod_alias and configuring it through .htaccess. Unfortunately I don't have any experience with mod_alias whatsoever.

隔岸观火 2024-11-26 06:02:51

对我来说一直很有效的方法是让用户将他们的图像直接上传到我的 mysql 数据库中。 PHP 将编码为 base64 并存储到 blob 中。然后,您执行类似于迈克尔所说的操作来检索并显示图像。我包含了我在 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"); ?>

下一个显示文件:

<?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">

Something that always has worked well for me is to have users upload their images directly into my mysql db. The PHP will encode into base64 and store into a blob. Then you do something similar to what michael said to retrieve and display the image. I've included some code from a project I was working on in 2008. I wouldn't copy it exactly if it's a method you're interested in using since it's old code.

This is the PHP to upload and store into a DB. Obviously replace your info and connect to your own DB.

<?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"); ?>

This next one displays the file:

<?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"];

?>

Then:

<img src="image.php?id=your_img_id">
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文