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>
Related
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.
I have the following shortcode which gets the images of that post (that will be in a folder named as the post's id ) and displays them echoing as many tag images as needed.
session_start();
$postID = $_SESSION['post_ID'];
$upload_dir = wp_upload_dir();
$path = $upload_dir['basedir'].'/'.'gallery/'.$postID;
if(file_exists($path)) {
$pathFile = $path.'/*';
$images = glob($pathFile);
if(!empty($images)) {
foreach($images as $image){
$alt = end(explode('/', $image));
echo '<img src="'.$image.'" alt="'.$alt.'" />';
}
} else echo 'No hay imágenes.';
} else echo 'No hay imágenes.';
?>
The problem is that the images aren't displayed, i get the followin error in the brower's console GET https://path-to-theme/wp-content/uploads/gallery/1658/wwg.jpg 404, per every image and i get this icon:
image
I have no idea why my images aren't displayed the images, the path is correct, the images are there, complitely no idea what I'm missing. It's not a permissions problem as i tried giving 755 permissions to the directories starting from /uploads/.. and so on including the images.
I'm going to assume that the URL you reference in your question is actually https://yourdomain/wp-content/uploads/gallery/1658/wwg.jpg and not https://path-to-theme/wp-content/uploads/gallery/1658/wwg.jpg. Right?
If that's the case, then it should be able to pull open that file. The first thing I'd check is whether you actually have the file located where you think it is: /wp-content/uploads/gallery/1658/
If the file is actually in that folder, and you can access it from your browser at https://yourdomain/wp-content/uploads/gallery/1658/wwg.jpg then my next suggestion would be to check the permission of each of the folders leading up to the file and make sure they are set to 755, and then make sure your file is set to 644.
Here are instructions on changing file permissions.
Let me know if that helps?
I fixed the problem. The issue wa sthat i wasn't uploading the images as wp wants me to, therefore the images, even they were in the folder, couldn't be displayed. So I had to change the way i uploaded them, by using wp functions and specifying the mime_type etc so wp could store their metadata in it's db in order to be able to acknowledge them and display them in a future.
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.
I'm trying to generate images on my website by pulling a thumbnail from one folder and the actual image from another. I have no idea what I am doing. This is what I have so far:
<?php
$thumbdirname = "images/thumbs/";
$imgdirname = "images/";
$mainimages = glob($imgdirname."*.{jpg,png,gif}", GLOB_BRACE);
$imagethumbs = glob($thumbdirname."*.{jpg,png,gif}", GLOB_BRACE);
foreach($imagethumbs as $image) {
echo '<a class="imageLink" href="$mainimages" data-lightbox="logo" data-title="Vivid Logo"><img src="'.$image.'"</a> ';
}
?>
I know that it will not work with just "$mainimages" for href, but I haven't been able to figure out what to put there. As is, it will pull up the right thumbnail, but not link to the full associated image.
I tried to put another foreach statement in, but I get four results from my two pictures (and their thumbnails). Which makes sense sense it is showing one of each combination:
(img1,thumb1),(img1,thumb2),(img2,thumb1),(img2,thumb2)
How should I change this to get it playing nicely?
You don't seem to be outputting from $mainimages so I'm going to ignore this for now.
You PHP code seems to be OK from initial glance. Only thing that is a bit iffy is that you are not closing you image tag.
If that does not help try counting the image array/object.
If that returns 0 try the below code.
// Find all files in that folder
$files = glob('images/gallery1/*');
// Display images
foreach($files as $file) {
echo '<img src="' . $file . '" />';
}
Hope that helps
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!