0

I am passing an image link from PHP to be displayed on a HTML page. But, when the page is rendered, the image does not show, instead, a place holder is shown.

PHP

$details = getimagesize($config['thumb_dir'].$value['thumbName']);
if ($details !== false){
    $content.= "<h2>".$value['title']. "</h2>"."<p><img src= ".$config['thumb_dir'].$value['thumbName']." width =".$details[0]." height = ".$details[0] ." alt= ".$value['filename'] ." /></p>";

HTML output

<h2>Welcome to the Home page</h2><h2>Title for image 1</h2>
<p>
    <img src= /home/rraja01/public_www/w1fma/thumbs/thumb-landscape-large.jpg width = 150 height = 150 alt= landscape-large.jpg />
</p>
8
  • 1
    what is the pages extension is it .php or .html if the later then it wont work it need to be named .php etc just as you mentioned "on a HTML page" ? Commented Dec 30, 2013 at 19:18
  • also could you show the output of the html page as in the code onto the result your using? Commented Dec 30, 2013 at 19:19
  • Please, don't put spaces between your HTML attributes name and value. You should also escape characters: <img src=\"".$config['thumb_dir'].$value['thumbName']."\"/> Commented Dec 30, 2013 at 19:24
  • the src tag in your html output must have double quotes around the path. try putting escaped quotes around the php variable. Commented Dec 30, 2013 at 19:24
  • 1
    @JeffNoel i know that i wanted to make sure that was not the mistake in the first :-) Commented Dec 30, 2013 at 19:32

2 Answers 2

1

Your $config['thumb_dir'] variable is returning the full path on the server and including /home/rraja01/public_www/ at the front -- very few servers have the root mapped to the website. You may need to do something like this:

$website_path = substr($config['thumb_dir'], 24);
$content.= "<h2>".$value['title']. "</h2>".'<p><img src="'.$website_path.$value['thumbName'].'" width="'.$details[0].'" height="'.$details[0].'" alt="'.$value['filename'].'" /></p>';
Sign up to request clarification or add additional context in comments.

2 Comments

below is what i am putting into the $config: $config['app_dir'] = dirname(dirname(FILE)); $config['upload_dir'] = $config['app_dir'] . '/uploads/'; $config['template_dir'] = $config['app_dir'] . '/template/'; $config['view_dir'] = $config['app_dir'] . '/views/'; $config['thumb_dir'] = $config['app_dir'] . '/thumbs/';
dirname returns the path to the file, which I presume is actually __FILE__ -- there's no need to do it twice and when you echo it out to the browser, it's giving too much of a path... you only want the path relative to the domain, not the server.
0

You have to use the relative path of your image (relative to the document root):

$details = getimagesize($config['thumb_dir'].$value['thumbName']);

$relative_path = str_replace($_SERVER['DOCUMENT_ROOT'], '', $config['thumb_dir']);

if ($details !== false) {
    $content .= '<h2>'.$value['title'].'</h2>'
             .  '<p><img src="'.$relative_path.$value['thumbName'].'" width="'.$details[0].'" height="'.$details[0].'" alt="'.$value['filename'].'" /></p>';
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.