簡體   English   中英

顯示來自Web根目錄public_html之外的文件夾中的圖像

[英]Display image from a folder outside the web root directory public_html

我想在public_html外部的文件夾中上傳圖像,這意味着public_html和upload(我的圖像文件夾名稱)彼此平行,我的目錄結構像

public_html網站上傳圖片

<?php session_start(); ?>

<?php
include 'connect.php';

$title=$_REQUEST['pt'];
$brand=strtoupper($_REQUEST['b']);
$author=$_REQUEST['authod'];
$des=$_REQUEST['des'];
$category=$_REQUEST['c'];
$sub_category=$_REQUEST['s'];
$oprice=$_REQUEST['o'];
$sprice=$_REQUEST['sp'];
$nm =$_FILES['f']['name'];
$fol =time()."_".rand(0,99)."_".$nm;

$path="/home/mydomain/upload/".$fol;



$tmp=$_FILES['f']['tmp_name'];
move_uploaded_file($tmp,$path );


$sq = mysql_query("insert into 
bachhaoffer(id,title,brand,author,des,category,scategory,oprice,sprice,image) values ('','$title','$brand', '$author' , '$des','$category','$sub_category','$oprice','$sprice','$path')");
if($sq)
{

echo "<script>window.location.href='../'</script>";
}else
{
echo "<script>alert('no')</script>";
}
?>

上傳工作正常,但是當我想顯示圖像時,向我顯示錯誤。 請給我正確的方式顯示圖像。

我正在嘗試獲取圖像網址並顯示圖像,所以我創建了名稱為img.php的文件

<?php
$mime_type = mime_content_type("/image_path/{$_GET['file']}");
header('Content-Type: '.$mime_type);
readfile("/image_path/{$_GET['file']}");
?> 

並使用顯示

<img src="img.php?file=photo.jpg" />

顯示類似警告的錯誤:無法修改標頭信息-行3上/home/mydomain/public_html/img.php中已經發送過的標頭(輸出始於/home/mydomain/public_html/img.php:1)

直接路徑不起作用,因為您無法從根文件夾之外獲取內容。 另一種方法是從服務器目錄路徑讀取php腳本中的圖像文件,然后使用header提供文件。 首先創建img.php編寫此代碼

    <?php
       $mime_type = mime_content_type("/image_path/{$_GET['file']}");
       header('Content-Type: '.$mime_type);
       readfile("/image_path/{$_GET['file']}");
    ?>

然后,在HTML中的img標記上使用<img src="img.php?file=photo.jpg" /> 希望它能工作。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM