简体   繁体   中英

Display image from a folder outside the web root directory public_html

I want to upload an image in a folder that outside public_html mean public_html and upload(my image folder name) are parallel to each other my directory structure like

public_html website upload images

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

Upload Is working fine but when I want to display image then show me an error. please give me the right way to show an image.

I am Try to fetch image url and display image so I create file with name img.php

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

and display using

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

Show error like Warning: Cannot modify header information - headers already sent by (output started at /home/mydomain/public_html/img.php:1) in /home/mydomain/public_html/img.php on line 3

direct path won't work cause you can't get content from outside of root folder. Another way is Read image file in php script from server dir path and serve file using header . First Create img.php Write this code

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

After than use <img src="img.php?file=photo.jpg" /> on your img tag in html. Hope it will work.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM