When I run the PHP file, the image is not show, instead just the altattribute value shown which is 14.JPG . The page source is also accurate and the image is also exist in that folder.
Note - there are also some echo statements before this code. they are for other purposes.
Do I need to use headers also when displaying image this way?
$dir = "C:/xampp/htdocs/PHP";
$file = "14.JPG";
if ( file_exists($dir) == false ){
echo 'Directory \''. $dir. '\' not found!';
}
else{
echo '<img src="'. $dir. '/'. $file. '" alt="'. $file. '"/>';
}
The problem is you're getting the file path mixed up with the URL. The file path is C:/xampp/htdocs/PHP and the URL is http://localhost/PHP. You need to use the URL when setting links to file and images. Your image source should be http://localhost/PHP/14.JPG but you've set it to C:/xampp/htdocs/PHP/14.JPG. Change it and it'll work for you.
You apply wrong approach please use relative url make uploads folder in your PHP folder
$dir = "uploads/";
$file = "14.JPG";
if ( file_exists($dir) == false ){
echo $dir .'not found!';
}
else{
echo '<img src="'. $dir. '/'. $file. '" alt="'. $file. '"/>';
}
Change This
$dir = "C:/xampp/htdocs/PHP";
To This
$dir = "PHP/";
Related
<?php
$dir= 'C:\xampp\htdocs\img';
echo "<img src='".$dir."\america.jpg' alt='icon'>";
?>
I just written this code for test and I couldn't get able to display image on browser.
The path you are using is not correct, try this:
$path = '/img/';
$imgName = 'america.jpg';
echo '<img src="'. $path.$imgName .'" alt="icon">';
// If img folder is present in the same directory
or
$path = '../img/';
$imgName = 'america.jpg';
echo '<img src="'. $path.$imgName .'" alt="icon">';
// If img folder is present one directory upwards from current directory
I have the path to an image file /files/uploads/1 but as you can see, I don't have an extension so I can't display the image. How can I get the extension of the file so I can display it? Thanks!
EDIT: Here is come code I have tried. BMP, PNG, JPG, JPEG and GIF are the only possible extensions, but $path ends up never getting assigned a value.
$exts = array('bmp','png','jpg','jpeg','gif');
foreach ($exts as $ext) {
if (file_exists("/files/uploads/" . $id . "." . $ext)) {
$path = "/files/uploads/" . $id . "." . $ext;
}
}
Personally, I had a problem where I had an unnecessary slash (/) before my path in the file_exists check, but the solution to the question is still the same.
$exts = array('bmp','png','jpg','jpeg','gif','swf','psd','tiff','jpc','jp2','jpx','jb2','swc','iff','wbmp','xbm','ico');
foreach ($exts as $ext) {
if (file_exists("files/uploads/" . $id . "." . $ext)) {
$path = "/files/uploads/" . $id . "." . $ext;
}
}
You don't need a file extension to show an image.
<img src="/files/uploads/1">
Will show the image. It's the same way the placehold.it images work
<img src="http://placehold.it/350x150">
First of all , I think when you get the file name for the image that means no need to add extension , so you have the file name and image folder
it should simply look like this :
$fullpath = ('uploadword/'.$filename);
// then call it
<img src='".$fullpath."'>
The normal scenario to upload all the images in specific folder then get the filename and save it in data base
move_uploaded_file($file_tmp,"upload/".$filename);
Some professional make it more sophisticated to make filename unique ,because they expected some duplicated values and many reasons , here is one simple technique :
$anynameorfunction = "";// random number etc...
$newname = $anynameorfunction.$filename;
move_uploaded_file($file_tmp,"upload/".$newname);
So, when they call it again it should be simple like this
$fullpath = ('uploadword/'.$newname);
// then call it
<img src='".$newname."'>
This is in very simple way and i wish you get what i mean
I'm trying to get all images from a directory and print them. I know my URL is correct because I am echoing the the $dirname along with a .jpg image I have saved in the directory. What am I doing wrong?
<?php
$dirname = "uploads/{$site_country}_{$site_state}_{$site_name}/{$record_id}/";
$images = glob($dirname . "*.jpg");
foreach($images as $image) {
echo '<img src="'.$image.'"/>';
}
//check to see if $dirname is correct
echo '<img src="' . $dirname . "IMG_0002.JPG" . '"/>';
?>
//edit
I have solved my problem, by removing the jpg extention and just pulling "*" for everything in the folder. Although it works for me now, its not best practice. It must be something to do with different .jpg file formats?
You used lowercase file extension here.
$images = glob($dirname . "*.jpg");
And uppercase JPG in your test output.
echo '<img src="' . $dirname . "IMG_0002.JPG" . '"/>';
If you're using a linux system then file extensions are case sensitive.
You need to prepend the dirname to glob's result to get the full paths.
So something like:
$dirname = "uploads/{$site_country}_{$site_state}_{$site_name}/{$record_id}/";
$images = glob($dirname . "*.jpg");
foreach($images as $image) {
echo '<img src="'.$dirname.$image.'"/>';
}
I want to be able to present the photo of a user. That user may be of the type Doctor, Nurse or Patient and each person belonging to each of those types has a distinct ID which identifies him. I have the photos of each type of users separated in three folders inside the directory /images/user_upload/ and a given user may or may not have a photo. If he has a photo, I want to present the photo with a name which is the same as his ID (Note that I don't know the extension of this file, only the name). If he doesn't I want to present a default image. I have a function called printUserPhoto for this end.
function printUserPhoto()
{
$path = '/images/user_upload/' .ucfirst($_SESSION['listtype']). '/' .$_SESSION['id']. '.*';
$source = glob($path);
if(empty($source))
echo '<img src="images/default_profile_img.png" />';
else
echo '<img src="',$source[0],'" />';
}
Although, I always get an empty array in the variable $source, even though the image exists. What am I doing wrong?
This way with glob
function printUserPhoto()
{
$pathToDocumentRoot = $_SERVER['DOCUMENT_ROOT'];
$path = $pathToDocumentRoot . '/images/user_upload/' .ucfirst($_SESSION['listtype']). '/' .$_SESSION['id']. '.*';
$source = glob($path);
if(empty($source)){
echo '<img src="images/default_profile_img.png" />';
} else {
echo '<img src="',$source[0],'" />';
}
}
Maybe limit the filetypes user can upload. With that you could do something like this:
function printUserPhoto()
{
$pathToDocumentRoot = $_SERVER['DOCUMENT_ROOT'];
$path = $pathToDocumentRoot . '/images/user_upload/' .ucfirst($_SESSION['listtype']). '/' .$_SESSION['id'];
$file = "images/default_profile_img.png";
if(file_exists($path . '.jpg')){
$file = $path . '.jpg';
}else if(file_exists($path . '.png')){
$file = $path . '.png';
}else if(file_exists($path . '.gif')){
$file = $path . '.gif';
}
echo '<img src="' . $file . '" />';
}
Also as being said by #Phil you are now referring to the file system root / being absolute path not relative to your document root.
I need to build a dynamic image gallery plugin for Joomla! that will go into a specific folder and pull out all images from the folder and show first of them as a large preview image and the rest in a list. Afterwards I will need to make the preview image open in a lightbox if clicked and in the lightbox I need to have the small thumbnails of listed images as well.
But know I need just php to go to the folder and pull out all the images from the folder specified. I have googled a bit and found some solution but this does not work for some reason I don't understand. Could someone tell me please, what is wrong with the code?
Thanks!
<div id="images">
<?php
$images_dir = 'images/';
$scan = scandir($images_dir);
echo '<img src="' . $images_dir . $scan[2] . '"alt="image" />';
?>
<ul id="smallimages">
<?php
for($i=0; $i<count($scan); $i++){
$ext = substr($scan[$i], strpos($scan[$i], '.'), strlen($scan[$i]-1));
$filetypes = array('.jpg', '.JPEG', '.jpeg');
if(in_array($ext, $filetypes)){
echo '<li><img src="' . $images_dir . $scan[$i] . '" alt="' . $scan[$i] . '"></li>';} }?></ul>
</div>
I think it's because you haven't defined the path correctly. Try using the following:
$images_dir = JUri::root() . 'plugins/content/plg_new/images';
JUri::root() is the root of your Joomla site so change the path from there on accordingly to where ever your images are located
Hope this helps
Does this work for you?
<?php
$image_directory="hrevert/images/";
$pictures = glob("$image_directory*.{gif,jpg,png}", GLOB_BRACE);
//displays the first image
$first_img=reset($pictures);
echo "<img src=\"".$first_img."\" width='20px' />";
//loops through other images and prints them
echo "<ul>";
foreach($pictures as $picture) {
echo "<a href='$picture'><img src=\"".$picture."\" width='20px' /></a>";
}
echo "</ul>"
?>