0

I'm trying to serve all my images through a PHP script. Ultimately the goal is to replace the images dynamically if certain conditions are met, but for now I'm just trying to get the script to return a specific image file. I've got a .htaccess which is sending all image requests to this script but at the moment the image is not showing up on the page.

Full contents of PHP script:

$img = '/imgs/img1.jpg';
$type = 'image/jpeg';
header('Content-Type:'.$type);
header('Content-Length: ' . filesize($img));
readfile($img);

I want to get URL of the image request that was redirected to this script. Depending on conditions I want to either serve the image immediately or use the image to generate a new one with GD and return that.

7
  • 1
    Why download the image over HTTP? Why not serve it from disk at this point? You probably don't have the HTTP fopen wrapper enabled. You can also 302 redirect to the image, depending on your needs. Commented Mar 9, 2013 at 15:03
  • I agree, give a relative path to the image instead. Commented Mar 9, 2013 at 15:53
  • Changed the file path to /imgs/img1.jpg but still doesn't work Commented Mar 9, 2013 at 16:18
  • You are giving a path to the location in the hard drive not the docroot. Therefore, unless the file in located in your hard drive in /imgs/ it wont work. Commented Mar 9, 2013 at 16:45
  • How do you know the redirection in .htaccess is working? Maybe it isn't. Commented Mar 9, 2013 at 17:25

1 Answer 1

1

You are giving a path to the location in the hard drive not the docroot. Therefore, unless the file in located in your hard drive in /imgs/ it wont work. Try:

$img = $_SERVER['DOCUMENT_ROOT'] . '/imgs/img1.jpg';
$type = 'image/jpeg';
header('Content-Type:'.$type);
header('Content-Length: ' . filesize($img));
readfile($img);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

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