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.'"/>';
}
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
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/";
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>"
?>
i made a function in which it prints all the file names with path inside a directory. but i want only first image to be fetched and if there is no image inside the directory print a custom path with a image . any help on this ?
function getphotos($album_name)
{
//path to directory to scan
$dir = ROOTPATH . "/data/img/gallery/$album_name/";
//get all files
$images = glob($dir . "{*.jpg,*.gif,*.jpeg}", GLOB_BRACE);
$ret = "";
foreach($images as $image)
{
$ret .= '<img src="'.$image.'" />';
}
return $ret;
}
$ret = '<img src="' . (empty($images) ? DEFAULTIMG : $images[0]) . '/>';
How can I display images from directory and get a corresponding description with each image, give the description exists.
in Directory //
01.png
01.txt
02.png
03.png
03.txt
etc.
to display as //
<img src="01.png"><br>This is the description from the text file named 01.txt
<img src="02.png"><br>
<img src="03.png"><br>This is the description from the text file named 03.txt
I've been searching and searching, but can't find anything, so if someone could point me in the right direction it would be greatly appreciated. Also this is a very useful thing to be able to do for people wanting to create very simple galleries or lists of images and names.
Thanks in advance!
This is what you're looking for, as the description must be dynamically captured from a corresponding .txt file:
$dir = './';
$files = glob( $dir . '*.png');
foreach( $files as $file) {
$filename = pathinfo( $file, PATHINFO_FILENAME) . '.txt';
$description = file_exists( $filename) ? file_get_contents( $filename) : '';
echo '<img src="' . $file . '"><br>' . $description;
}
What it does is grabs an array of *.png files using glob() from a given directory ($dir). Then, for each image, it gets the filename of the image (so 01.png would be 01), and appends .txt to get the name of the description file. Then, it loads the description file into the $description variable using file_get_contents() if the description file exists. It then outputs the desired HTML.
I assume you have the .php file in the same directory as the pictures and text files are.
You could use function glob() to read in all image-files as array from the directory, cut off the file extension (so '01.png' becomes '01') and append the file extension with string concatentation.
A working code example may look like this:
<?php
$path_to_directory = './';
$pics = glob($path_to_directory . '*.png');
foreach($pics as $pic)
{
$pic = basename($pic, '.png'); // remove file extension
echo '<img src=\"{$pic}.png\"><br>';
if(file_exists($pic . '.txt'))
{
echo file_get_contents("{$pic}.txt");
}
}
?>
So definitely have a look on these functions:
http://www.php.net/glob
http://www.php.net/basename
http://www.php.net/file_get_contents
Happy coding.
Your question is a bit confusing.
make an array with all information.
$pics = array('img' => '01.png', 'text' => 'This is the description');
foreach($pics as $pic) {
echo '<img src="'.$pic['name'].'" alt="">' . $pic['text'];
}
So you have to put your information in an array or a database otherwise you cannot map the desciption to your image.
When you want to read dynamicly the folder its a bit difficult.
You can look at readdir or glob then you can read all images get the name and load the textfile with file_get_contents but i think its not a really performant way.
Code Modified from here : http://php.net/manual/en/function.readdir.php
//path to directory to scan
$directory = "../images/team/harry/";
//get all image files with a .jpg extension.
$images = glob($directory . "*.jpg");
//print each file name
foreach($images as $image)
{
print "<img src=\"$image\"><br>This is the description from the text file named $image";
}
ok, so this won't print the contents of the text file, but I'm sure you can further modify the code above to figure that out
YEP
Updated version of ZnArKs code, as he missed that you wanted the content of the files
//path to directory to scan
$directory = "../images/team/harry/";
//get all image files with a .jpg extension.
$images = glob($directory . "*.png");
//print each file name
foreach($images as $image)
{
$textfile = substr($image, 0, -3) . "txt";
echo "<img src='{$image}'><br/>";
if(file_exists($textfile))
{
echo file_get_contents($textfile) . "<br/>";
}
}