使用绝对路径显示图像

时间:2012-06-05 14:10:04

标签: php html

我在上传时使用uploadify将img上传到服务器我保存了数据库的绝对路径:

/var/www/html/workbench/photogallery/uploads/logo2.jpg

现在我想在浏览器中显示相同的方法,以下方法不起作用

<div id="photos">
    <h3>Photo title</h3>
    <P class="like"><a href="#">Like</a> </P>
    <p class="date">date </p>
    <p class="pclear" />
    <div id="image">
      <img src="<?php echo $result_set['path']; ?>" />
    </div>
    <p class="about">about image goes here</p>
</div>

上面的代码不起作用。当我手动编辑路径上传/ logo2.jpg它完美无缺

我该如何解决这个问题?

3 个答案:

答案 0 :(得分:5)

您需要使用网址,而不是路径。

此:

/var/www/html/workbench/photogallery/uploads/logo2.jpg

是物理路径,即找到映像的服务器上的地址。您需要使用可供访问者使用的网址。我猜你有一个映射到该服务器的名称(类似localhost或www.example.com)。

从结构上我猜你的网址就像是

http://www.example.com/photogallery/uploads/logo2.jpg

www.example.com 是您用于访问该应用程序的基本网址

答案 1 :(得分:2)

试试这个:

<img src="/photogallery/uploads/<?php echo basename($result_set['path']) ?>" />

答案 2 :(得分:0)

试试这个对我有用,并保持图像的位置隐藏在浏览器中。

<img src='fake.php' style='max-width:90px;'/>

创建一个名为fake.php的文件,如下所示

<?php
$thePic = "/var/www/html/workbench/photogallery/uploads/logo2.jpg";
$image = imagecreatefromjpeg($thePic); 
// Output image and free up the memory 
header("Content-type: image/jpeg"); 
imagejpeg($image); 
imagedestroy($image); 
?>

您可以调整fake.php文件中的代码,以便从发布的数据或数据库中检索$ thePic的位置等。

相关问题