cancel
Showing results for 
Search instead for 
Did you mean: 

Problems using readfile in PHP script to deliver images

aenea
Grafter
Posts: 31
Registered: ‎30-07-2007

Problems using readfile in PHP script to deliver images

I have been using a PHP script to enable me to display inline images without making the image files viewable directly through http.
I've been using this for a couple of years now and it used to work fine. Recently, however, it has become very slow, drastically delaying the loading of my pages. Peformance is not consistent. Sometimes a page will load instantly, other times it will hang waiting on one or more of the images to load. It can take as much as 1.5 minutes to load an image that, on another ocassion, takes just 115 miliseconds. Has anyone any idea what is causing this and do you have any suggestions on how to fix it?
The HTML code used to display an image is
<img src="load-image.php?image=filename">

and the php script, load-image.php,  is as follows:
-----------------
$img = $_GET['image'];
$file = "images/$img";
if(!file_exists($file)) {
  readfile("images/error.gif");
  }
else { 
    readfile($file);
  }
-----------------------
10 REPLIES 10
7up
Community Veteran
Posts: 15,824
Thanks: 1,579
Fixes: 17
Registered: ‎01-08-2007

Re: Problems using readfile in PHP script to deliver images

Is that ALL the code in the script? - I can't see that being any trouble.
It may however possibly be the server load. Are you using PNs hosting?
I need a new signature... i'm bored of the old one!
csogilvie
Grafter
Posts: 5,852
Registered: ‎04-04-2007

Re: Problems using readfile in PHP script to deliver images

Is that the entire contents of the file? If so, you're missing the <?php tag.
spraxyt
Resting Legend
Posts: 10,063
Thanks: 674
Fixes: 75
Registered: ‎06-04-2007

Re: Problems using readfile in PHP script to deliver images

I've just tried this loading a 75kB JPEG image. Loading instantly for me (Vista with FF 6.0).
David
aenea
Grafter
Posts: 31
Registered: ‎30-07-2007

Re: Problems using readfile in PHP script to deliver images

Thanks for the thoughts. I think I've solved it. As suggested, I hadn't incuded the complete PHP code. The complete script was actually as follows:
<?php
/* This little program will load an image file from the images protected directory
    use as <img src="load-image.php?image=filename">
*/

require("session.inc");
if (!$_SESSION['id']) {
  readfile("images/error.gif");
} else {
$img = $_GET['image'];
$file = "images/$img";
if(!file_exists($file)) {
  readfile("images/error.gif");
  }
else { 
    readfile($file);
  }
}
?>
And the file session.inc (which is included in all my PHP scripts) is as follows:
<?
  session_save_path('/files/home1/dunbarney/sessions/'); 
  session_start(); //start php session
?>
Your comments started me looking at this to see if that might be having any effect. Bingo! When I remove the session_save_path from this file, my problem goes away.
The session_save_path line got inserted in the first half of last year, when there were problems with the new CGI server using /tmp for session files. The recommended solution was to specify the save path within one's own filespace, so this is what I did. I notice that there are a lot of files in my sessions directory, so perhaps I need to do some housekeeping to delete them. Or, have PlusNet fixed the problem that made me put this line in in the first place, in which case maybe I can continue using the default. Has anyone any advice on this?
Gabe
Grafter
Posts: 767
Registered: ‎29-10-2008

Re: Problems using readfile in PHP script to deliver images

I think PN have increased the garbage collection from /tmp, but it still seems to fill up occasionally, so I guess you'd still be safer with your own session_save_path. You probably need to reset the session.gc_probability, as in this script. (The Debian default for gc is off, because it runs a separate garbage collector on its default session folder - which PN doesn't use.)
Gabe
aenea
Grafter
Posts: 31
Registered: ‎30-07-2007

Re: Problems using readfile in PHP script to deliver images

Well you were right about PN not having fixed the problem with /tmp. By this morning my scripts were failing due to /tmp being full up.
Will have to wait till later to give your script a try  but, in the meantime, I created an empty 'sessions2' directory and pointed the session_save_path to that but, alas, that doesn't fix my problem. The long delays in image loading are back. Could the problem be due to my having the session_save_path directive included in every PHP file? In that case, I wonder if your method will fix it by usingin php.ini instead . . . time will tell.
aenea
Grafter
Posts: 31
Registered: ‎30-07-2007

Re: Problems using readfile in PHP script to deliver images

I have fixed my problem by removing the session_start and check on $_SESSION['id'] from my load-image.php script,confirming that it is something about sessions that is the root of my problem. However, this does leave a slight security hole as it means there is no longer a check that this script has been called from one of mine. So, ideally, I'd like to get to the bottom of why the session_start is causing the problem in the first place.
spraxyt
Resting Legend
Posts: 10,063
Thanks: 674
Fixes: 75
Registered: ‎06-04-2007

Re: Problems using readfile in PHP script to deliver images

This sounds like a potential file locking problem on the session file. Presumably the script is called multiple times on a page to load different images. Do any of the images fail to load?
David
aenea
Grafter
Posts: 31
Registered: ‎30-07-2007

Re: Problems using readfile in PHP script to deliver images

Yes, that's correct. The script is called multiple times to load a series of images. None of the images fail to load, but  can take a *very* long time to load. While this happens, the placeholder for the image remains blank, displaying the 'alt' tag if there is one and the whole page is frozen with the message 'waiting for ccgi.dunbarney.plus.com . . .'. appearing on the status bar.
Which images it affects, varies from ocassion to ocassion. File locking sounds a plausible explanation. Presumably it is the session file which is getting locked. Any suggestions on how to avoid this?
aenea
Grafter
Posts: 31
Registered: ‎30-07-2007

Re: Problems using readfile in PHP script to deliver images

Hmm. I tried putting a session_write_close(); statement immediately after the session_start(); command in the load-image.php script but that doesn't fix the problem. So it doesn;t look as if it's the session file locking that's the culprit. Any other ideas?