gpt4 book ai didi

php - 从 public_html 文件夹外的文件夹在站点上显示图像

转载 作者:太空宇宙 更新时间:2023-11-04 09:40:51 25 4
gpt4 key购买 nike

我正在使用 codeingiter 并在其中有一个图像目录。由于空间问题,我已将整个目录转移到同一台服务器上的新位置。

之前的路径是/var/www/html/site_folder/assets/images

新位置是/home/new_site/images

现在,当我想在网站上显示图像时,它无法加载。

我试过使用下面的代码

$file = '/home/new_site/images/3.jpg';

header('Content-Type: image/jpeg');

echo "<img src='".readfile($file)."' &glt;";

我有大约 50 张图片,我想将它们加载到我的网站上。

这适用于本地但不适用于服务器。我在本地使用 Linux,在服务器上使用 Centos

最佳答案

你把事情搞混了。你想做的是返回图像文件的内容,但你返回的是一个 HTML 片段。

您可以尝试多种解决方案:

  1. 创建指向新图像目录的符号链接(symbolic link),大多数内容可以保持原样。根据您的 Web 服务器配置,此方法可能有效或无效。
  2. 更改对图像的所有引用

    src="image5.jpg"

    做某事

    src="image.php?name=image5.jpg"

然后你需要脚本“image.php”,它将图像的名称作为查询参数,然后返回内容:

<?php
// take care to make some clever checks here
// otherwise you introduce a security nightmare
$file = '/home/new_site/images/' . htmlspecialchars($_GET["name"]);

header('Content-Type: image/jpeg');

readfile($file);
?>

关于php - 从 public_html 文件夹外的文件夹在站点上显示图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21673317/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com