How to Displaying an image with path stored in Database? - php

I have stored images path in database and images in project folder. Now i am trying to view all IMAGES with their PRODUCTS in my HTML template. I have written the following code and its given me an empty images box in the output and when i open that image box in the new tab, it opens the following URL.
http://localhost/cms/1
Kindly tell me the way of referring images in 'img src ' tag.
include 'connect.php';
$image_query = "select * from product_images";
$image_query_run = mysql_query($image_query);
$image_query_fetch = mysql_fetch_array($image_query_run);
if (!$query=mysql_query ("select product_id,name from products")) {
echo mysql_error();
} else {
while ($query_array = mysql_fetch_array($query)) {
echo '</br><pre>';
$product_id = $query_array['product_id'];
echo "<a href='single_product.php?product_id=$product_id' >";
print_r ($query_array['name']);
echo "</a>";
echo "<img src=". $image_query_fetch
['images'] ." alt=\"\" />";
echo '</pre>';
}
}
} else {
echo "You need to Log in to visit this Page";
}

Add your local image path before source
example
echo "<img src='http://localhost/cms/1/". $image_query_fetch['images'] .'" alt=\"\" />";
*Use PHP *
<?php echo "http://" . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI']; ?>

You can use the HTML base tag to set a base URL that relative URLs will be resolved from.
See -> http://www.w3schools.com/tags/tag_base.asp

Related

How to take an image from My SQL database and add it to a .html page

I have some images in a database which I would like to add to a html page
This is my current code
<div class = "gridrow">
<?php
foreach (range(1, 4) as $value) {
$result = $conn->query("select * from products where product_ID = '".$value."'");
$row = $result->fetch_array();
$name_p1 = $row['product'];
$price_p1 = $row['price'];
$image = "<img src='{$row['image']}'>";
echo "<div class = 'productwindow' >";
echo "<div class = 'productimage'><".$image."></div>";
echo "<div class = 'productvar'><p>".$name_p1."</p></div>";
echo "<div class = 'productvar'><p>$".$price_p1."</p></div>";
echo "</div>";
}
?>
</div>
This is what the page looks like
These are the errors I get
How can I make these images show correctly?
You're storing image data in the database, while the img src tag wants image URLs, that's why it's getting confused and you're getting errors.
The quick way around it is to convert the image data to base64 and pipe it in the src tag like so:
$image = '<img src="data:image/png;base64,'.base64_encode($row['image']).'">';
This is at best a hack, and not a great idea for a host of reasons, it also assumes all your images are PNG.

echo my images on my index page PHP

i built a CMS and my image shows succesfully in the admin page(back-end) once i upload an image with the form but once on the main index page(front-end) it shows that "broke image link"
here is my function to post the uploaded image on my admin page:
function gettestimony() {
$query = mysql_query("SELECT * FROM testimony") or die(mysql_error());
while($post = mysql_fetch_assoc($query)){
echo "<img src =\"" . $post['photo']."\">";
echo "<p>" . $post['imagename'] . "</p>";
echo "<br>";
echo "<p>" . $post['test'] . "</p>";
echo "<br>";
echo "<p>" . $post['author'] . "</p>";
echo "<br>";
echo "Delete";
echo "Edit";
echo "<br>";
}
}
and the directory is htdoc/blah/admin/include/functions.php
and the image is saved in htdoc/blah/admin/image/
for my main index, this is the function to post it:
function gettestimony() {
$query = mysql_query("SELECT * FROM testimony") or die(mysql_error());
while($post = mysql_fetch_assoc($query)){
echo "<div class='column second'>";
echo "<p>" . $post['test'] . "</p>";
echo "<img src =\"" . $post['photo']."\">";
echo "</div>";
}
}
this is the code i use in both functions:
echo "<img src =\"" . $post['photo']."\">";
so like i said it works fine in admin page but my main page it doesn't....someone please help
Your image's path relative to the path of your function is irrelevant. As far as that function is concerned the path to the image is just a string.
You need to make sure the path to the image is correct relative to the HTML file that's viewing it. The easy way to make it work is to just use the full URL to the image:
www.something.com/admin/images/myimage.php

Load Video with PHP

I am trying to load a video file in php using by selecting the file with _GET.
Here is the code
<?php
$id = $_GET["id"];
$video = "vid" . $id . ".mp4";
if (isset($_GET["id"]) AND $_GET["id"] == $id) {
header('Content-Type: video/mp4');
print file_get_contents($video);
}
else {
echo "File not found.";
}
?>
This is my web link.
http://www.animesour.com/video.php?id=555 This will get the file vid555.mp4
However, when you visit the link it doesn't load the video or play it.
It will load images fine, but doesn't seem to work when it's video.
Basically I need it so that when I link http://www.animesour.com/video.php?id=555 in an I frame, that it will load the video. Thank you in advance.
<?php
if (isset($_GET["id"])) {
$id = $_GET["id"];
$video = "{URL TO VIDEO BASED ON $id}";
echo "<video controls><source src='{$video}' type='video/mp4'></video>";
} else {
echo "File not found.";
}
?>

Display an Image from PHP mysql table

I have a PHP script to echo the contents of a Mysql table.
I'm using it as a small CMS for a static page.
What I want to know is how can I go about displaying an Image in my PHP script.
I have a form that submits the Date, Title, Message, and Image.
I'm using the Image field to insert the URL of an image.
Whats the correct way of displaying the image on a page.
Below is my code:
<?php
$con = mysql_connect("localhost","name","password");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("databaseName", $con);
$result = mysql_query("SELECT * FROM Blog");
while($row = mysql_fetch_array($result))
{
echo $row['Date'];
echo "<br />";
echo $row['Title'];
echo "<br />";
echo $row['Message'];
echo "<br />";
echo $row['Image'];
echo "<br />";
echo "<br />";
echo "<br />";
}
mysql_close($con);
?>
This is the outcome:
17th Feb
Title
Here is my Message
http‍://vickybeeching.com/blog/wp-content/uploads/2011/09/canthearyou.jpeg
18th Feb
Title
Here is my Message
http‍://vickybeeching.com/blog/wp-content/uploads/2011/09/canthearyou.jpeg
but I want the page to display the image not the URL.
I'm guessing just using the <img></img> tags, but I'm not sure where in the PHP.
You can write code in image tag instead of echo directly.
Try this code.
echo "<img src='".$row['Image']."'/>";
Try this code, but give image folder path perfectly
<?php
$imagepath = "../uploads/";
echo "<img src='".$imagepath.$row['Image']. "' alt='' height='200' width='200' /> ";
?>
According to need , you can change height and width of the image. but clarity differs.
Thanks
echo $row['Image'];
replace like this
echo '<img src="'.$row['Image'].'" alt="" />';

php - Decide whether an image exist and if it does show it

I have a code that shows a different image depending where on the page I am, but some places don't have an image so it displays a "no image" icon. I want to add a condition that checks if there really is an image in the given path and if returns false don't do anything. I have no idea how to do it.
This is the original code:
<?php
$search=get_search_query();
$first=$search[0];
if ($first=="#"){
echo "<html>";
echo "<img src='http://chusmix.com/Imagenes/grupos/".substr(get_search_query(), 1). ".jpg'>";
}
?>
What I need to know is which function do I use to get a true/false of that image path. Thanks
Use file_exists
$image_path = 'Imagenes/grupos/' . substr(get_search_query(), 1) . '.jpg';
if (file_exists($image_path)) {
echo "<img src='http://chusmix.com/Imagenes/grupos/".substr(get_search_query(), 1). ".jpg'>";
} else {
echo "No image";
}
http://php.net/manual/en/function.file-exists.php
You can use file_exists

Categories