Jump to content

Recommended Posts

An image is saved in the root directory.
The page "label.php" uses "readfile" to access and display the image using code like this:

$file = '../image.jpg'; // IN ROOT DIRECTORY
$type = 'image/jpeg';
header('Content-Type:'.$type);
header('Content-Length: ' . filesize($file));
readfile($file);

The image then gets embedded into an html page and displayed like this:
Dear Customer, here is your image: <img src="label.php?label=<?= $filename ?>.jpg">

QUESTION: in the event of an error, how do I dipslay the readfile error on the customer's html page?
Even though I have error trapping in the "readfile" code in "label.php" file -- like if a parameter is missing, I'll have exit( "NO IMAGE GENERATED" ).  But this error message won't display on the customer's html page because html treats <?= $filename ?>.jpg" like it's a real image instead of displaying the readfile's "NO IMAGE GENERATED" exit error.

Make sense? No. I probably have to rewrite my question 😃

Edited by ChenXiu
Link to comment
https://forums.phpfreaks.com/topic/313468-readfile-image-error-trapping/
Share on other sites

you would have an actual image containing whatever text or picture you want to use to indicate that there's no image to display and output it instead.

for your images, you should store the actual file names in a database table, then use the id (auto-increment) value as the ?label=x parameter in the src="..." link, then query in the label.php code to get the corresponding file name. this will prevent someone from supplying a filename to something like your database connection credentials file and have the readfile() statement output the content of that file.

20 minutes ago, mac_gyver said:

you would have an actual image containing whatever text or picture

Interesting! Both your ideas - the "placeholder image" with the words "no image here" sounds perfect, and, the storing the real filenames in a database sound excellent.

I'm thinking because you made these suggestions, you must be saying there's no way for PHP code (on my html page) to verify what my readfile.php page is generating, right?

For example, my html page says:
Dear Customer, here is your image: <img src="label.php?label=<?= $filename ?>.jpg">

So there's no such line of PHP code I could precede that with?
For example, something that like this pseudocode:  <?php  if( ! image ("label.php?label=<?= $filename ?>.jpg")) echo "no image"; ?>

I'm assuming this cannot be done -- I tried lots of stuff that didn't work.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.