satoshi
satoshi

Reputation: 4103

PHP readfile fails but gives no error

Can anyone help me with this nonsense problem?

<?php
if(!readfile($path))
{
    $error = error_get_last();
    imageError('*'.$error['type'].','.$error['message'].','.$error['file'].','.$error['line'].'*'); // Outputs the error on an image
    exit;
}
?>

This outputs an image showing ",,," - basically the error is empty. I think the problem can be related to the fact that I'm trying to read the same file 10 times in the same moment. I've something like this:

<img src="images/10/seo-friendly.jpg" />
<img src="images/10/seo-friendly2.jpg" />
<img src="images/10/seo-friendly3.jpg" />
<img src="images/10/seo-friendly4.jpg" />
<img src="images/10/seo-friendly5.jpg" />
<img src="images/10/seo-friendly6.jpg" />
<img src="images/10/seo-friendly7.jpg" />
<img src="images/10/seo-friendly8.jpg" />
<img src="images/10/seo-friendly9.jpg" />
<img src="images/10/seo-friendly10.jpg" />

Then I have a rewrite rule in my .htaccess like:

RewriteRule images/([0-9]+)/.*\.(png|jpg|gif) resize.php?id=$1 [L]

So, what happens is that I basically request the same file 10 times in the same moment. Most of the times it works properly, but sometimes I get that problem (just for 1-3 of the 10 images).

Any clue is really appreciated.

Thank you.

Upvotes: 2

Views: 1356

Answers (2)

satoshi
satoshi

Reputation: 4103

The problem was due to bad concurrency. I was obviously trying to read an image while writing on it (or vice versa)

Upvotes: 1

Jonathan
Jonathan

Reputation: 63

readfile() returns the number of bytes read from the file. So, it's entirely possible that the file you're trying to read is empty and no error occurred.

If, indeed, there is an error, you can use set_error_handler() to capture the error and handle it as desired.

Upvotes: 2

Related Questions