简体   繁体   中英

Get image filepath using PHP and display in HTML

I have followed several posts and made progress, but I still am unable to display a jpeg image using the html img src tags which is located in my webroot folder under www/SnapShots.

I would like to use PHP to search a local mysql db and return the newest image filepath. I would then like to use that filepath to display the image.

I have tried using both a separate getImage.php and showimage.html as well as as a single file containing both php and html which I will share below. Currently I am able to echo the correct filepath, which when copied and pasted manually into an img src works, however it does not work by referencing the variable or .php url.

webcam.php:

<?php
echo '<!DOCTYPE html>';
echo '<html>';
echo '<body>';
echo '<h1>Display Recent WebCam Image</h1><br />';

//$id = $_GET['id'];
//Connect to mysql DB
$link = mysql_connect("localhost", "root", "password");
mysql_select_db("temperature_database");
//Select thew newest image filepath
$sql = "SELECT imgFilePath FROM tempLog WHERE datetime=(SELECT MAX(tempLog.datetime) FROM tempLog);";
$result = mysql_query("$sql");
$row = mysql_fetch_assoc($result);
mysql_close($link);

//header("Content-type: image/jpeg");
header("Content-type: text/plain");
$imgpath = $row['imgFilePath'];
//echo $imgpath;


echo '<img src="{$imgpath}", alt="Most recent WebCam Shot", width="800", height="600"><br />';
?>
</body>
</html>

I have tried numerous ways of inserting the $imgpath variable and or referencing it from a separate html page ie: .

A point in the right direction would be much appreciated.

As of now webcam.php outputs the following, so there also appears to be an issue with interpreting the html tags.

<!DOCTYPE html><html><body><h1>Display Recent WebCam Image</h1><br /><img src="{$imgpath}", alt="Most recent WebCam Shot", width="800", height="600"><br /></body>

Use double quotes instead:

// code
echo "<img src='{$imgpath}' alt='Most recent WebCam Shot' width='800' height='600'><br />";

PHP does not replace variables inside single quoted strings.

Update

If I understand well your question, you want to read the contents of a image and use it in a <img> tag.

You have to split the code in two files (I'll illustrate with a simplification):

index.html

<p>Webcam image:</p>
<img src="view_image.php">

view_image.php

<?php
$image_path = function_to_get_image_path();
header("Content-type: image/jpeg");
readfile($image_path);

This is the approach you could take.

I recommend you start with an easy sample:

Create a folder with three files:

A sample image called image.jpg

A HTML file called index.html with the following code:

<img src="view_image.php">

And a file called view_image.php with this content:

<?php 
header("content-type: image/jpeg");
readfile("image.jpg");

Note that php has to be enabled in your web server.

您也可以像这样使用它:

echo '<img src="'.{$imgpath}.'", alt="Most recent WebCam Shot", width="800", height="600"><br />';

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM