ChenXiu Posted August 2, 2021 Share Posted August 2, 2021 (edited) 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 August 2, 2021 by ChenXiu Quote Link to comment https://forums.phpfreaks.com/topic/313468-readfile-image-error-trapping/ Share on other sites More sharing options...
mac_gyver Posted August 2, 2021 Share Posted August 2, 2021 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. Quote Link to comment https://forums.phpfreaks.com/topic/313468-readfile-image-error-trapping/#findComment-1588781 Share on other sites More sharing options...
ChenXiu Posted August 2, 2021 Author Share Posted August 2, 2021 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. Quote Link to comment https://forums.phpfreaks.com/topic/313468-readfile-image-error-trapping/#findComment-1588783 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.