what am i trying to do is echo an image using php,
my code is really simple..
i have stored the path of the image in mysql db..
the path of the image is: ../users/profiles/23/images/dps/1409947526.jpg
now i am using the following code to output this picture:
mysql_connect("localhost", "root", "") or die("error!");
mysql_select_db("xone");
$query = mysql_query("SELECT * FROM userdpcover WHERE id='23'");
$result = mysql_fetch_array($query);
$dir = $result['dp_address'];
$dp_name = $result['dp_name'];
$dp = $dir.$dp_name;
echo $dp;
echo "<img src='$dp' />";
but when i run this code, all i get is an broken image!
thanks in advance!
Have you tried this?
echo "<img src='".$dp."' />";
Please try this and please check your image path url correct....
<?php
//here your code
?>//close php tag and try this ...
<img src="<?php echo $dp; ?>"/>
<?php
//your code here
?>
View the source of the output HTML. This will tell you the image path. You probably need to modify it.
Also, remember to set your base URL in a PHP config file to avoid repeating URL paths.
Related
I can't display images from database.
I have folder called images next to index.php where I keep images.
I suppose I have wrongly defined folder path with images or sth else.
<?php
$connection = new mysqli("localhost", "root", "", "crud");
$sql = "SELECT * FROM test";
$res = $connection->query($sql);
if(#$res->num_rows > 0)
{
while($row = $res->fetch_assoc())
{
?>
<img src="<?php "C:/xampp/htdocs/img_mysql/images/".$row['image'] ?>" style="width:170px;height:120px" />
<?php
}
}
?>
I put picture with how look my website when I try display images.
2 Issues:
You are not outputing anything.
You are using a path to the file on your computer rather than of the
server itself.
Fix it like this:
<img src="<?php echo "/images/".$row['image'] ?>" style="width:170px;height:120px" />
You shouldn't use a domain in your frontend pathes for the simple reason that in production you will have a different domain. If you really want to for some reason, a fine solution will be using a constant, something like this:
[In a file included from all pages]:
define("IMAGES_PATH", "http://localhost/images/");
Then
<img src="<?php echo IMAGES_PATH . $row['image'] ?>" style="width:170px;height:120px" />
How can I echo an image using PHP?
This is what I have:
echo "<img src="Images/Picture.GIF">";//This should echo my image
If by "echo" you mean outputting the image in a browser, you need to read it first then send it with echo. Something like this should work:
$content = file_get_contents('Images/Picture.GIF');
header('Content-Type: image/gif');
echo $content;
You cant echo an image using PHP.
PHP echo
echo — Output one or more strings
echo is for strings only.
However, you can echo the image source - img src=""
Just make sure you put the picture extension at the end of the file you are grabbing. - .jpg .png etc.
You just need to grab the image source from somewhere.
Example (using $_GET):
<?php
if($_SERVER['REQUEST_METHOD']=='GET'){
$img_src=$_GET['img_src']
}
echo "<img src='/images/test/" . $img_src . "' alt='img'>";
?>
Upload the image in your media files
Get the url/path of the uploaded image
Do this!
$image='<img src="https://www.yoursite.com/wp-content/uploads/2022/02/dope.png"/>';
echo $image;
How do I get my image id to be echoed in another page?
Page Portfolio:
[ image one ]
[ image two ]
[ image one ] has id of image1. When clicking on image1, it will direct to a new page to display in a larger size.
so far, here is my code.
Page Portfolio
<img src="image1.jpg"/>
<img src="image2.jpg"/>
view.php
<?php $id=$_GET['id']; ?>
<?php if($id == 'image1'){
echo '<img src="orange.jpg"/>';} ?>
<?php if($id == 'image2'){
echo '<img src="milk.jpg"/>';} ?>
It is acceptable if I use this code for a few pictures, but I am going to use it for a lot of pictures. Any suggestion or tips? Is it possible to echo based on the id of the images?
Any help is appreciated. Thank you for your time.
I'm really sorry that I forgot to mention that I am not getting the image from database. Please take a look at the view.php I've edited above. I'm not sure if I explain myself clear enough.
Try this
<?php $id=$_GET['id'];
echo '<img src="'.$id.'.jpg"/>';
?>
You could use the GET value as part of the src attribute, like so:
<?php
echo '<img src="' . $_GET['id'] . '".jpg" />';
?>
But note that this requires every image to have the same jpeg extension.
You CAN'T echo img src=... in the php because you already doing this in the original HTML as a source, you need to print the picture.
//phpFile.php
<?php echo "<img src..">;
will be parsed as an HTML document, so in the html
the source will be an html document and NOT an Image.
you need to replace img to iframe or
use this in the php
$file = $_GET['id']; //or full path to file
$type = 'image/jpeg';
header('Content-Type:'.$type);
header('Content-Length: ' . filesize($file));
readfile($file);
While it might be a bit overkill and the other answers having a more straight approach, I figured I'd give you this option as well. (This is for your view.php file of course :) )
<?php
$id = $_GET['id'];
$validImages = array
(
'image1.jpg',
'image2.jpg',
'image3.jpg'
);
if(in_array($id,$validImages)){
<-- DISPLAY IMAGE HERE -->
}else{
die('Invalid image');
}
And if you desire #Daniel Krom's edition with the header(); part it's just a simple matter of putting it inside of the if(){} clause instead of the echo :)
Thank you for all your suggestions! I manage to get it work using the following codes. :) - Nurul
Page Portfolio
<img src="image1.jpg" id="image1"/>
<img src="image2.jpg" id="image2"/>
view.php
<?php $id=$_GET['id']; ?>
<img src="<?php echo $id; ?>.jpg"/>
(if and only if the extension of all images are the same just like FDekker said.)
The code below is generating a result like this: http://localhost/my_website/ contact-us
$base_url="http://localhost/my_website/";
echo $link= "$base_url contact-us ";
But I am trying to get a result like this : http://localhost/my_website/contact-us
I have also tried the following code
$base_url="http://localhost/my_website/";
echo $link= "$base_url.contact-us ";
but the result is like this http://localhost/my_website/.contact-us
Could you please show me how to solve this problem?
Edit
I am very sorry, I did't clearly mention the exact problem I am facing here. I thought the above example would help my case. Actually I am trying to create a link that I will send at users email address.
My code
$base_url="http://localhost/my_website/";
$random_hash="1";
echo $link="
<a href='$base_url account/confirm_registration/$random_hash' target='_blank'>$base_url account/confirm_registration/$random_hash</a>";
But it is generating like this
http://localhost/my_website/ account/confirm_registration/1
echo $link = $base_url."contact-us";
$base_url="http://localhost/my_website/";
echo $link= $base_url."contact-us";
You just need to split it
Tested and works:
$base_url="http://localhost/my_website/";
echo $link=$base_url."contact-us";
$base_url="http://localhost/my_website/";
$link=$base_url."contact-us";
echo $link;
You need to learn about basic string concatenation: http://php.net/manual/en/language.operators.string.php
Try this:
$base_url = "http://localhost/my_website/";
$random_hash = "1";
$url_page = "account/confirm_registration/$random_hash";
$url = $base_url . $url_page;
$link = "<a href='$url'>$url</a>";
echo $link;
I have a the following code that i intend to should return a filepath name from mysql in the php section and then display the image in html. But i am only getting up a tiny thumbnail in my browser. By the way "username" and "imagefile" are the only columns in the table "images".
I'm sure there is just some silly mistake but i need a fresh pair of eyes to spot it. Thanks a lot in advance. P.S i know i should really me moving over to mysqli but i will simply translate at a later date. Cheers
<?php
session_start();
$username = $_SESSION['username'];
$con = mysql_connect('localhost','root','password');
mysql_select_db("db");
$profileimage = mysql_query("
SELECT * FROM images WHERE username='$username'
");
$row = mysql_fetch_array($profileimage);
$showimage = $row['imagefile'];
?>
<html>
<img src = "$showimage">
</html>
First off, HTML doesn't know what "$showimage" means. That is a PHP variable and HTML cannot interpret it. You need to output it so that HTML can just deal with the result.
So if the value for $showimage is "/images/foo.jpg" you would need something like:
<img src="<?php echo $showimage; ?>" />
which would give you
<img src="/images/foo.jpg" />
Now, switching things to mysqli is as simple as replacing mysql with mysqli. It's no more complicated than that. Since it looks like you are just starting to learn about these things you may as well, when you go to improve things, learn about PDO.
Is this your current real code or is it a simplified version? If it is your real code the problem is in the HTML part where the PHP variable is unknown, you should do this:
<html>
<img src ="<?php echo $showimage; ?>" />
</html>
<?php
$db = mysqli_connect("localhost:3306","root","","databasename");
$sql = "SELECT * FROM table_name ";
$sth = $db->query($sql);
while($result=mysqli_fetch_array($sth)){
echo '<img src="data:image/jpeg;base64,'.base64_encode( $result['image'] ).'" height="100" width="100"/>';
}
?>
Works for me