Jump to content

Give Image to HTML through PHP-doesn't work


mr_bob
Go to solution Solved by mac_gyver,

Recommended Posts

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.

Link to comment
Share on other sites

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".

Link to comment
Share on other sites

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:

Qo4BkBA.jpg

Link to comment
Share on other sites

  • Solution
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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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 by mr_bob
Link to comment
Share on other sites

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.