mr_bob Posted July 21, 2021 Share Posted July 21, 2021 Hi, sorry for a noob question, but I'm having problem with this small code that doesn't work. I have secret image path written in php file which must be given to HTML parameter. After this I want to make PHP send file of mime type "audio/mpeg3". The thing is to hide actual file paths. Every code is as simple as it can get hopefully. I run these files locally with Firefox and they are located in the same folder: phptest.html, phptest.php, car.jpg. I have HTML file: <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> </head> <body> <p>Image given by .php file:</p> <img src="phptest.php?id=1"> </body> </html> It's really simple, just look at body tag. The whole phptest.php file: <?php $path=''; if ($_GET["id"] === "1"){ //More "if" statements planned $path = 'car.jpg'; } header('Content-Type: image/jpeg'); //Don't need too much headers? readfile($path); exit(); ?> PHP is supposed to read file with "id" paramenter. But Somehow this doesn't work. Quote Link to comment https://forums.phpfreaks.com/topic/313405-give-image-to-html-through-php-doesnt-work/ Share on other sites More sharing options...
Strider64 Posted July 21, 2021 Share Posted July 21, 2021 (edited) Well, the path won't be "hidden" even if you get it to work properly as anyone can get the path by looking at the HTML once the image is displayed. That is if I am understanding you properly? Edited July 21, 2021 by Strider64 Quote Link to comment https://forums.phpfreaks.com/topic/313405-give-image-to-html-through-php-doesnt-work/#findComment-1588453 Share on other sites More sharing options...
mr_bob Posted July 21, 2021 Author Share Posted July 21, 2021 So the line <img src="phptest.php?id=1"> is going to be changed if you click "view page source" or when you click "Save image as..."? Well that is a problem because some people claim that "URL will be hidden from HTML". Quote Link to comment https://forums.phpfreaks.com/topic/313405-give-image-to-html-through-php-doesnt-work/#findComment-1588454 Share on other sites More sharing options...
mac_gyver Posted July 21, 2021 Share Posted July 21, 2021 technically, the code you have posted should work. unfortunately, telling us something doesn't work doesn't help. exactly what symptom, error, or output do you get? Quote Link to comment https://forums.phpfreaks.com/topic/313405-give-image-to-html-through-php-doesnt-work/#findComment-1588455 Share on other sites More sharing options...
mr_bob Posted July 21, 2021 Author Share Posted July 21, 2021 Just now, mac_gyver said: technically, the code you have posted should work. unfortunately, telling us something doesn't work doesn't help. exactly what symptom, error, or output do you get? I'm getting this page when clicking on .html file in Firefox: Quote Link to comment https://forums.phpfreaks.com/topic/313405-give-image-to-html-through-php-doesnt-work/#findComment-1588456 Share on other sites More sharing options...
Solution mac_gyver Posted July 21, 2021 Solution Share Posted July 21, 2021 2 minutes ago, mr_bob said: clicking on .html file in Firefox that opens the file through the file system, not the web server, so, any links in the html will also be read through the file system. you must use a url to the html file, such as http://localhost/phptest.html. this will cause the src="phptest.php?id=1" attribute to be read through http://localhost/phptest.php?id=1 Quote Link to comment https://forums.phpfreaks.com/topic/313405-give-image-to-html-through-php-doesnt-work/#findComment-1588457 Share on other sites More sharing options...
mr_bob Posted July 21, 2021 Author Share Posted July 21, 2021 Maybe it will open in Live Server extension of VS Code? A very handy tool helps test everything because uploading to a web server is more complicated. Though i'm getting the same output (no image) when url is http://127.0.0.1:5500/phptest.html with message in VS Code "Live Reload is not possible without body or head tag.". So as you said, it's not a file system now, or it should be uploaded to a web server to make it work. Quote Link to comment https://forums.phpfreaks.com/topic/313405-give-image-to-html-through-php-doesnt-work/#findComment-1588458 Share on other sites More sharing options...
dodgeitorelse3 Posted July 21, 2021 Share Posted July 21, 2021 A web server typically uses port 80 so what do you get without the :5500 in address? Quote Link to comment https://forums.phpfreaks.com/topic/313405-give-image-to-html-through-php-doesnt-work/#findComment-1588463 Share on other sites More sharing options...
mr_bob Posted July 21, 2021 Author Share Posted July 21, 2021 (edited) 4 hours ago, mac_gyver said: that opens the file through the file system, not the web server, so, any links in the html will also be read through the file system. you must use a url to the html file, such as http://localhost/phptest.html. this will cause the src="phptest.php?id=1" attribute to be read through http://localhost/phptest.php?id=1 10 minutes ago, dodgeitorelse3 said: A web server typically uses port 80 so what do you get without the :5500 in address? edit: I'll check this out when I can return to PC where those files are thanks. After uploading to a web server the image showed indeed. Thanks a lot for pointing out on this. The code is working then. Appreciate all the replies! Edited July 21, 2021 by mr_bob Quote Link to comment https://forums.phpfreaks.com/topic/313405-give-image-to-html-through-php-doesnt-work/#findComment-1588464 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.