Calling image from Mysql DB path PHP - php

Hello dear Overflowers!
I have a little code that so far works so well,
it gets the corresponding Image ID to the corresponding House ID, also Caption and filename are no problem!
I just fail at displaying the image... :(
`
include("functions/config.php");
$images_dir = "/some/url/that/is/correct/cms/houses/";
$query = "SELECT houses.*, gallery_photos.* ".
"FROM houses LEFT JOIN gallery_photos ".
"ON houses.id = gallery_photos.photo_category";
$result = mysql_query($query) or die(mysql_error());
// Print out the contents of each row into a table
while($row = mysql_fetch_array($result)){
$house_id = $row['id'];
$photo_category = $row['photo_category'];
$photo_caption = $row['photo_caption'];
$photo_filename = $row['photo_filename'];
echo "House ID ". $house_id. " - ". $photo_category." - ". $photo_caption." - <img src='".$images_dir.$photo_filename."' />";
echo "<br />";
}
?>`
So as said, everything get selected well, even the image path is correct when you click Image Info in the browser, but I simply do not get the image to display, am I missing something very trivial here?
Thanks in advance!

Check Image Name in Table as well as in folder where you store particular image.

<img src='".$images_dir.$photo_filename."' />
It misses like .jpg or .png whatever the extension of your img is

Check with tool like Firebug what is the path in src attribute of img element. Then copy this link and paste it in browser to see if it opens (with your website name - so if link is '/img/houses/house1.jpg' then you paste to browser 'www.mypage.com/img/houses/house1.jpg').
If it does not then:
Check if path is correct (it will count from web root folder) (your path should be probably like '/cms/houses/5953.jpg')
Check of path has correct letter sizes ('house' vs. 'House')
Check if you can open file on server (maybe file is corrupted)
Check if you have rights to read file

All right guys, finally found the mistake, which I hope that with my anwser, some pople in the future can avoid it!
As it seems some Hosts do not like some of the PHP naming conventions, so what I had to do, so the files would actually be stored in the folder, was change the command to get them from "Tmp/In memory" to the final destination folder.
For better understanding, here what I changed :
copy($photos_uploaded['tmp_name'][$counter],
$images_dir . '/' . $filename);
to
move_uploaded_file($photos_uploaded['tmp_name'][$counter],
$images_dir . '/' . $filename);
I hope no one else ever has to fiddle this long and spend so much money on Coffee ever again.
Thanks to all that tried to help anyway!

Related

Set img src without knowing extension

So I have a few images in the server (public_html/img/profile_pictures/).
This is how I currently set the image:
echo "<img src='img/profile_pictures/main_photo.png'/>";
The main_photo can change each day, but if it changes to main_photo.jpg insted, it wont show (because the extension is hardcoded on that line(.png)). Is it possible to display the photo without knowing the extension for the image file?
If you want a PHP code, then try this. This code will look for main_photo.* inside your folder and automatically set the extension upon finding one.
Remember to set the path properly
<?php
$yourPhotoPath = "img/profile_pictures/";
foreach (glob($yourPhotoPath.'main_photo.*') as $filename) {
$pathInfo = pathinfo($filename);
$extension = $pathInfo['extension'];
$fileName = chop($pathInfo['basename'], $extension);
echo "<img src='".$yourPhotoPath.$fileName.$extension."'/>";
}
?>
if a Photo isn't loaded, it's width and size is null.
Although I would advise you to write a class that checks and loads images, I get a feeling you want a simple solution. so, given by the premise that the photo is either
<img src='img/profile_pictures/main_photo.png'/>
or
<img src='img/profile_pictures/main_photo.jpg'/>
and that neither this path nor this filename ever changes and in the folder is only one picture,
you could simply echo both.
The img of the one that is empty will not be shown.
A better way was to write a class that loads your photo and checks if the photo is really there, like
$path = 'img/profile_pictures/main_photo.png';
if(!file_exists('img/profile_pictures/main_photo.png'))
{
//use the jpg path
$path = 'img/profile_pictures/main_photo.jpg';
}
You can ofc just inline this if case, but it's bad practise to intermix buisinesslogic and format logic, so I advice you to write a class for it.

PHP if else noimage

I'm basically a designer who hand codes HTML, but am a hack when it comes to PHP. I've been asked to add image icons within a product search results table on a PHP/MySQL site that has had many programmers over a decade, causing a sloppy mess of code.
I have the following code at the top of a search results page that calls out the name of the image:
$image2 = $row['item'] . ".jpg";
$imagefile2 = $_SERVER['DOCUMENT_ROOT'] . "/product_images/$imagefile2";
if(file_exists($imagefile2)){
Within the table itself I hacked this to get the correct image to show:
print "<td><center><img src=/product_images/$image2 width=80><br>
Of course, if there is no image, there is a broken image link. I dod not know the proper syntax to tell the server that IF there is no image, THEN show noimage.jpg (located in same folder). This is probably a couple lines of added code at best, but after a couple hours of searches and attempts I surrender.
Check to see if the file exists and if it does, set the image2 variable to point to it. If it doesn't exist, set the image2 variable to point to the "no image" image.
if(file_exists($_SERVER['DOCUMENT_ROOT'] . "/product_images/".$row['item'].".jpg")){
$image2=$row['item'].".jpg";
}else{
$image2="/product_images/noimage.png";
}
<?php
if(file_exists($_SERVER['DOCUMENT_ROOT'] . "/product_images/".$row['item'].".jpg")){
$image2=$row['item'].".jpg";
}else{
// Specify No Image File Path to Variable
$image2="/product_images/noimage.png";
}
?>
<td><center><img src="<?php echo $image2; ?>" width="80"><br>

Unable to load image to MYSQL table

As the title says, I can't insert & display any image to & from mySQL db table. Image table looks like the photo attached and my php code looks like this:
<?php
$sql2 = "SELECT * FROM images";
$records2 = mysql_query($sql2);
while($images = mysql_fetch_assoc($records2)) {
echo $images['path'];
}
?>
The path is displayed in the page, not the actual image.
I've made the MYSQL connection so it is fine.
This is an answer that I had prepared beforehand, but never submitted it at the time.
Consult "Footnotes".
You're just echoing the path here and not the image source.
I.e.: <img src..>.
You might have problems with this also, in the way you set that path for it, if and when you access your executable from a different location.
echo "<img src=\"$images['path']\">";
If that doesn't work, then that will mean that the path you've set for it, should have been as a full server path starting at the root, and not a relative path.
If so, then you'll need to add to it.
I.e.: echo "<img src=\"/var/usr/htdocs/Projects/facebook/img/$images['path']\">";
or and as an example from the screenshot you left:
echo "<img src=\"/Projects/facebook/img/$images['path']\">";
Footnotes:
"It works now #Fred-ii- the mySQL path wasn't quite accurate – Sergiu Turus 5 hours ago"
It seems that I was right all along.

Image Path isn't displaying image

Am wondering why these image isn't showing even after properly retrieving the image path from MYSQL i've gone through the code and i still can't figure out why this is:
<?php
$sql = "SELECT description, quantity, product_price, category, product_img_url";
$sql .= " FROM *****, *****";
$sql .= " WHERE product_id = product_img_id";
$sql .= " AND product_id = ?";
$sql .= " LIMIT 1";
$stmt = $mysqli->prepare($sql);
$stmt->bind_param('s', $id);
$stmt->execute();
$stmt->store_result();
if($stmt->num_rows==1){
$stmt->bind_result($desc,$qty,$pr,$cat,$product_img_url);
$stmt->fetch();
?>
<div id="infobox">
<h3>Product View</h3>
<p align="left">
<?php
if(file_exists($product_img_url)){
echo '<img src="'.$product_img_url.'" alt="" width="300" height="300" />';
}else{
echo '<img src="" alt="product image" />';
}
?>
Please can someone tell me why this is happening?
If the image file does exist, then this might have something to do with the path.
I would use firebug to play with the image src path and try various things like putting a slash in the beginning, or removing it, or entering absolute path of image.
Once I get the image to show up in firebug, I would then proceed to fix the issue in the code.
As i am see you code and i just want to ask you that you have store complete path of your image in db or you have store just some part of it.
as i can see you have retrieve image path from mysql but the browser not able to retrieve image from your server path it may be because of path you r providing and and where the file store not match. the best way to check your file path and mysql path use
var_dump(your result set) and check you output weather image url match with your server image path, and i am prety much sure that your path will not match.
Can you please try with the following path structure
echo '<img src="/application_root_directory/'.$product_img_url.'" alt="" width="300" height="300" />';
Well, You can do like:
$product_img_url="guarantee.png"; ?>
<img src="images\<?php echo $product_img_url; ?>" alt="Hello">
It will not installed by default you have to install it manually. Either you can do goto tools in menubar>add-ons, and search in search fox for firebug or goto firebug website to install it.
Thanks everyone for all useful ideas presented, I appreciate. I've found a way to resolve the relative/absolute links. The images are now being displayed as they should! What I did was; since I have sub directories under my root folder, I created separate 'Meta' php scripts in each sub directory with names like INC, __ADM, etc. Each script links to another script in a toplevel directory. All I placed within the scripts were just paths, that I got using DOCUMENT_ROOT, doing this I was able to get the actual path to the image folder, that I then placed in the DB. Hope this idea helps someone else!

PHP/html/mysql Image display

Hey my php/mysql/html people,
I have this variable that holds a path to an image and then insert it into a db (yes, I am using mysql_real_escape_string) which works perfectly.
$file_name = $_FILES["file"]["name"];
$path='images\ '. $file_name;
insert into...blah blah blah
The path is later pulled from the db and stored in said variable.
$path = $row['file_path'];
I am trying to display it with:
// the contents of $path in this case is: images\ cats.jpg
echo "<img src=" . $path . ">";
However the image breaks because the only thing that src pics up is: images\
and not the actualname+extension of the image. I know this probably has to do with the slashes, but I am a newbie and could use some help. Thanks in advance!
your
$path='images\ '. $file_name;
should be
$path='images\'. $file_name;
Print $path variable & see the values.
echo $path;exit;
You will get value on web browser.
Copy the value displayed on browser & paste it in the URL, if you see the image, your saved path is correct, if not, you have gone wrong in specifying the relative path too the resource.

Categories