Problems using readfile in PHP script to deliver images
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page
- Plusnet Community
- :
- Forum
- :
- Help with my Plusnet services
- :
- Everything else
- :
- Problems using readfile in PHP script to deliver ...
Problems using readfile in PHP script to deliver images
23-08-2011 12:24 AM
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Report to Moderator
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);
}
-----------------------
Re: Problems using readfile in PHP script to deliver images
23-08-2011 5:44 PM
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Report to Moderator
It may however possibly be the server load. Are you using PNs hosting?
Re: Problems using readfile in PHP script to deliver images
23-08-2011 6:20 PM
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Report to Moderator
Re: Problems using readfile in PHP script to deliver images
23-08-2011 11:55 PM
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Report to Moderator
Re: Problems using readfile in PHP script to deliver images
24-08-2011 2:03 AM
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Report to Moderator
<?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?
Re: Problems using readfile in PHP script to deliver images
24-08-2011 9:28 AM
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Report to Moderator
Gabe
Re: Problems using readfile in PHP script to deliver images
24-08-2011 10:56 AM
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Report to Moderator
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.
Re: Problems using readfile in PHP script to deliver images
24-08-2011 12:11 PM
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Report to Moderator
Re: Problems using readfile in PHP script to deliver images
24-08-2011 1:47 PM
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Report to Moderator
Re: Problems using readfile in PHP script to deliver images
24-08-2011 2:49 PM
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Report to Moderator
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?
Re: Problems using readfile in PHP script to deliver images
24-08-2011 3:19 PM
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Report to Moderator
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page
- Plusnet Community
- :
- Forum
- :
- Help with my Plusnet services
- :
- Everything else
- :
- Problems using readfile in PHP script to deliver ...