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!
Related
I've got the broken images when I try to retrieve all jpg files from the folder where the name is 'imgProduct'.
The error message is 'Failed to load resource: the server responded with a status of 404 (Not Found)'
The directory of all images is C:\Sungsan\imgProduct.
And the directory of code below is C:\Sungsan\Website\view\shopPanel.php.
<?php
$productImg = glob("../imgProduct/*.jpg*");
echo var_dump($productImg).'<br>';
for($a = 0; $a < count($productImg); $a++) {
$num = $productImg[$a];
echo basename($num)."<br />";
echo '<img src="' .$num. '" alt="random image"> <br />';
}
?>
This image is the result that code above is excuted.
I was searching for how to troubleshoot this broken images but I have no idea. Hope to give recommendation or suggestion. Thanks
Your basepath makes ../imgProduct/img01.jpg to be img01.jpg. And I assume that your web root is not inside imgProduct, so you need to append it:
$num = basename($num);
echo "{$num}<br/><img src='/imgProduct/{$num}' alt='random image'> <br />";
Also don't mistaken server-side path with web-side url. ../imgPorduct/img01.jpg is going back in your URL from current website page, not from PHP file
I could be wrong, but by the looks of it you're trying to reference files in the wrong directory?
Change your image src to point to the folder containing the images (currently it's seemingly looking for 'img1.jpg' in the web pages current directory.
echo '<img src="../imgProduct/' .$num. '" alt="random image"> <br />';
After looking at your screenshot again and reading some of the comments, I've also noticed that you could likely also just remove basename as it is stripping out the rest of the file path.
Hope this helps.
I am building a simple webpage where I want to display a few images that are located on my computer # C:\xampp\htdocs\website\upload. I have stored their (relative) paths into the MySQL database, like this: website/upload/filename.extension.
Example:
INSERT INTO `photos` (`photoid`, `photopath`, `photoname`, `photoyear`)
VALUES ('20', 'website/upload/Putin.jpg', 'Putin', '2017');
The PHP code that's supposed to retrieve them is:
$query = "SELECT * FROM photos";
$result = mysqli_query($mysqli, $query);
while($photo = mysqli_fetch_assoc($result)) {
echo '<img src="'. $photo['photopath'] .'" alt="'.$photo['photoname'].'" height:"100" width="100"> <br />';
}
However, it only returns one picture and completely ignores the others, the result being something like this. And if I remove that particular picture from my database, it displays none of the others, although their location and filetype coincide.
What can I do so that all the images are displayed successfully?
Thank you!
Right-click on the images with errors and open Inspect Element, check whether the links are ok. If there's a 404 for images then the problem is with the link that you have stored.
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>
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.
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!