PHP / HTML - Load image from outside root folder -


i trying load in image in <img> tag within html. images folder in same folder root folder, meaning need go 1 folder access images folder.

i looked around internet , found method using requests within src attribute image, unfortunately without success.

folder structure:

img >     somefile.png     foo.png     bar.png html >     index.php     imagerequest.php     //other root files 

i cannot use following: <img src="../img/somefile.png"> because points towards non existant (duh!). attempted use request.

my html

<div>    <img src="imagerequest.php?requested=somefile.png"> </div> 

my php (imagerequest.php)

    $filename = $_get['requested'];     fetchimage();      function fetchimage(){         global $filename;         if(!isset($filename)) return;         $filepath = dirname($_server['document_root'])."/img/".$filename;         if(file_exists($filepath)){             readfile($filepath);         }     } 

tl;dr: use image folder outside root folder within <img> tag.

get image using php base64 image , output content along proper headers:

[imagerequest.php?requested=some_file.png]  $imagesdir = __dir__.'/../'; $content = file_get_contents($imagesdir.$_get['requested']); $content = base64_encode($content);  $data = "data:image/png;base64,{$content}";  header('content-type: image/png'); echo base64_decode($data);` 

Comments

Popular posts from this blog

javascript - Laravel datatable invalid JSON response -

java - Exception in thread "main" org.springframework.context.ApplicationContextException: Unable to start embedded container; -

sql server 2008 - My Sql Code Get An Error Of Msg 245, Level 16, State 1, Line 1 Conversion failed when converting the varchar value '8:45 AM' to data type int -