Readfile function takes too long

Hi,

My first script generates .jpg images and saves them in some catalog on my server. The second script should read .jpg file and send it as an output to browser. Here is it :
[php]<?php
header(‘Content-type: image/jpg’);
$i = $_GET[‘i’];
$uid = $_GET[‘uid’];
if (file_exists(“graph/$uid/p$i.jpg”)){
readfile(“graph/$uid/p$i.jpg”);
}
else { readfile(“empty.jpg”) ; }
?>[/php]

Everything works just fine besides the time which takes to output images. Let’s say when the first script save image in the catalogue, the second script has to be run few times (around 15 seconds) to display correct image. Do anyone know where is the problem ?

Are they running at the same time?
Are your hard drives rattling/aged.
Are the pictures HUGE and you didn’t dedicate large buffers to handling that type of load?
Are you using some type of PHP cache feature which oddly enough talks to the slowest thing in the computer the hard drive prior to output?

Are they running at the same time?

No, of course no.
Are your hard drives rattling/aged.

Script is not executed on my PC :slight_smile: I have account in hosting company.
Are the pictures HUGE and you didn’t dedicate large buffers to handling that type of load?

No, they are not. 35 KB
Are you using some type of PHP cache feature which oddly enough talks to the slowest thing in the computer the hard drive prior to output?
I use only the code which I attached.

If all you’re trying to do is display the file, then you don’t need to use readfile() at all, just use normal html.

Besides displaying the image I need to hide path to file, that’s why I wrote script.

it would far easier to just put in a publicly accessible area. trying to hide things is what gets people in trouble. You’re better off messing with mod rewrite or controlling access to the folders with htaccess.

Ok I will use the easier method but please answer my question :

What is the reason of that kind of delay with readfile() when .jpg is already on server and script does not see it within 10-15 seconds (sometimes few/more than few refresh of page) ?

Because its actually looking at the file contents instead of displaying it. You might also look at using symlink, not sure if it’ll work with dynamically created files, but it would be easier to use than trying to customize a mod rewrite string.

This sounds like a problem specifically with your host. If your server has some kind of raid mirroring or load balancing it could cause that kind of delay.

Yeah my list of questions are on that line either the drives themselves are slowing down the read function or the servers buffers are making it parse the file in chunks then display the output after its reassembled.

You could try starting the ob handler and set the buffers manually can’t remember off the top of my head how to do that but I remember having to do it when pushing and resizing pictures on a catalog of files I had a few years back.

Sponsor our Newsletter | Privacy Policy | Terms of Service