views:

293

answers:

2
if(preg_match('/^[a-zA-Z0-9]+\.(gif|jpe?g)$/',$row['filename'],$matches) &&
    is_readable($row['filepath'].$row['filename'])){    
  echo header('Content-Type: image/'.$matches[1]);    
  echo header('Content-Length: '.filesize($row['filepath'].$row['filename']));    
  echo file_get_contents($row['filepath'].$row['filename']);
} else {
  echo 'cant serve image: '.$row['filename'];
}

When I try to place the header('Content-Type': image/jpeg) for example, It only displays a blank page and the current url.

Im trying to display an image to replace my <img> tags.

filepath contains the physical path for the directory of the image.

A: 

You don't need to echo header, just header.

culebrón
+1  A: 

Not sure it'll solve the whole problem, but you should not use echo on the return value of the header function : to send a header, just use the header function, and don't echo anything :

header('Content-Type: image/'.$matches[1]);

An invalid image (ie, Firefox displays its URL instead of its content) is often caused by invalid output before / after the image date -- make sure you don't have anything sent to the browser other than the image itself.

Pascal MARTIN