I have this code that shows uploaded images:
<?php
$dir = "img/*.jpg";
$images = glob( $dir );
foreach( $images as $image ):
echo "<img src='" . $image . "' />";
endforeach;
?>
but I need my last uploaded image to appear first in line. I know it's right there in front of me, but I can't see it. How do I do that?
Try reversing the array before your foreach.
<?php
$dir = "img/*.jpg";
$images = glob( $dir );
$images = array_reverse($images);
foreach( $images as $image ):
echo "<img src='" . $image . "' />";
endforeach;
?>
A safer bet would be to use the filemtime() function to get the modification time of each file and sorting according to that. See this code:
<?php
$dir = "img/*.jpg";
$images = glob( $dir );
//Add this portion
foreach( $images as $image ) {
$imagesModTimeArray[filemtime($image)] = $image;
}
krsort($imagesModTimeArray);
$images=array_values(imagesModTimeArray);
//New Portion End
foreach( $images as $image ):
echo "<img src='" . $image . "' />";
endforeach;
?>
Related
this script show 50 random images
that change turning on themselves.
but sometimes show the same images
how don't show the same images?
my code
<?php
$all_images = glob("wp-content/themes/mysite/img-company/{*.jpg, *.JPG, *.JPEG, *.png, *.PNG}", GLOB_BRACE);
$images = glob("wp-content/themes/mysite/img-company/{*.jpg, *.JPG, *.JPEG, *.png, *.PNG}", GLOB_BRACE);
shuffle($all_images);
foreach ($all_images as $index => $image ) {
if ($index == 50) break; // Only print 50 images
$image_name = basename($image);
$randomImage = $images[array_rand($images)];
echo "<li><img src='/wp-content/themes/mysite/img-company/{$image_name}' /><img src='/$randomImage' /></li>";
}
?>
The obvious way would be to delete a presented image from the array:
$randomImage = $images[array_rand($images)];
$images = array_diff($images, array($randomImage));
Another solution: Simply use unset for removing element from array like this:
$random = array_rand($images);
$randomImage = $images[$random];
unset($images[$random]);
I'm totally new to PHP and not an Advanced web developper but I really like to learn. My problem is the following: I want to make a projects page which would show every projects located in subfolders. In clear, that page would include a thumbnail and a two lines description (in a text file) from each folder to display it as a table and on which a user could click to reach the right project page. Here is the code I am presently stuck with:
<?php
$dir = "*/";
$images = glob($dir."main.jpg" );
$myFiles = glob($dir."description.txt");
$fh = fopen($myFiles, 'r');
$theData = fread($fh, 5);
fclose($fh);
foreach( $images as $image ):
echo '<div class="projects"><div class="projects-img-container">';
echo" <img class='projet-img' src='". '/projects/' . $image . "'/>";
echo '</div><div class="project-description">';
foreach( $myfiles as $myfile ):
echo $theData;
endforeach;
echo '</div>';
endforeach;
echo '</div>';
?>
description.txt as follow:
Gold Collection
Deck 1
Any help would be appreciated, as for the moment the images are showing fine, but no text is displayed. Then would also come the link to the respective folder...
Should look something like this image:
http://i.stack.imgur.com/yjwX7.jpg
Thank you for your help!
Check out the comments & code within the modified script below for suggestions along with the ways I've implemented those suggestions...
<?php
$dir = "*/";
$images = glob($dir."main.jpg" );
/* This doesn't work because fopen() can't take an array
* $myFiles = glob($dir."description.txt");
* $fh = fopen($myFiles, 'r');
* $theData = fread($fh, 5);
* fclose($fh);
*/
echo '<div class="projects">'; // I think you meant to put this outside the loop
foreach( $images as $image ) {
echo '<div class="projects-img-container">';
echo" <img class='projet-img' src='". '/projects/' . $image . "'/>";
echo '</div><div class="project-description">';
$dn = dirname($image);
# $myFiles = glob($dn."/description.txt"); // don't think we need to glob() this
$fh = fopen($dn."/description.txt", 'r');
$theData = fread($fh, 5);
echo $theData;
fclose($fh);
/* Not sure it makes sense to loop $myfiles - only 1 description.txt can
* exist in any one directory, right?
* foreach( $myfiles as $myfile ):
* echo $theData;
*}
*/
# Whatever you want to do with the directory link would be done with $dn here.
echo '</div>';
}
echo '</div>';
?>
After review this seems to work well for me. thanks to you Peter!
<?php
$dir = "*/";
$images = glob($dir."main.jpg" );
echo '<div class="projects-container">';
foreach( $images as $image ) {
$dn = dirname($image);
echo '<div class="projects">';
echo '<div class="projects-img-container">';
echo" <a href='".$dn."'><img class='projet-img' src='". '/projects/' . $image . "'/></a>";
echo '</div><div class="project-description">';
$fh = fopen($dn."/description.txt", 'r');
$theData = fread($fh, 5);
echo" <a href='".$dn."'>".$theData."</a>";
fclose($fh);
echo '</div>';
echo '<div style="clear: both"></div>';
echo "</a>";
echo '</div>';
}
echo '</div>';
?>
This question already has answers here:
PHP file listing multiple file extensions
(5 answers)
Closed 9 years ago.
I am very new to php and I would like to iterate through an images directory in the root folder and post these images to a webpage. I am using the following code to do so:
<?php
$dir = "images/*.jpg";
$images = glob( $dir );
foreach( $images as $image ):
echo "<img src='" . $image . "' />";
endforeach;
?>
My question is how can I modify this code to post more than .jpg images, such as .gif and jpg with the .jpeg extension without creating multiple directory variables?
Also, is there a more efficient way to post these images?
Thank you very much!
The non clever brute force way to do this would be to just run the loop for all different image types.
$dir = "images/*.jpg";
$images = glob( $dir );
foreach( $images as $image ):
echo "<img src='" . $image . "' />";
endforeach;
$dir = "images/*.png";
$images = glob( $dir );
foreach( $images as $image ):
echo "<img src='" . $image . "' />";
endforeach;
$dir = "images/*.gif";
$images = glob( $dir );
foreach( $images as $image ):
echo "<img src='" . $image . "' />";
endforeach;
etc...
An elegant way would be creating an array and then iterate it.
<?php
$imgs = array("images/*.jpg","images/*.gif","images/*.pnj");
foreach( $imgs as $type ){
$images = glob( $type );
foreach( $images as $image ):
echo "<img src='" . $image . "' />";
endforeach;
endforeach;
?>
As you are building an Array ($images is an array) you can push new files to this array.
$file_extensions = ['jpg', 'png'];
$images_total = [];
foreach ( $file_extensions as $extension ) {
$dir = "images/*." . $extension;
$images = glob( $dir );
foreach $images as $image {
$images_total[] = $image;
}
}
And now use your final code:
<?php
foreach( $images_total as $image ):
echo "<img src='" . $image . "' />";
endforeach;
?>
I didnt test it, but this is an idea :)
I reply appeared while I typed: As a difference, in my method you get to build an array containing every single file in the folder before start printing HTML. This may allow you to shuffle or maybe manipulate the array before outputting to the user :)
<?php
$dir = new DirectoryIterator('../images/main_body_image/');
foreach ($dir as $file)
{
$images [] = array (
$file->getPathname() . "\n";
$file->getFilename(). "\n";
);
}
?>
shuffle($images);
Can you help me with the code above? I want to add images to an array using DirectoryIterator, and then shuffle images to generate randomized images. Thank you for your valuable inputs.
This depends on what you are doing with the images, and what path your in at the time, but the following will put their full pathname into an Array.
<?php
$dir = new DirectoryIterator('../images/main_body_image/');
foreach($dir as $file){
$images[] = $file->getPathname();
}
shuffle($images); $imgs = '';
foreach($images as $v){
$imgs .= "<img scr='http://{$_SERVER['SERVER_NAME']}/$v' alt='randomImage' />";
}
//put the below wherever you want in your code
echo $imgs;
?>
I have the following code to search the folder /images/ for images and echo them. However, it displays the images from a random order everytime I refresh the page. The images are named 1, 2, 3, 4 and so on. Any way to make it so that the last number (ex: 4) is the first one being displayed and so on?
<?php
$dirname = "images";
$images = scandir($dirname);
shuffle($images);
$ignore = Array(".", "..");
foreach($images as $curimg){
if(!in_array($curimg, $ignore)) {
echo "<img src=\"". $dirname . '/' . $curimg ."\">" ;
}
}
?>
Thanks in advance.
This is due to your shuffle. You are randomizing your array. Let me introduce you to: http://php.net/manual/en/function.array-reverse.php which is
<?php
$dirname = "images";
$images = scandir($dirname);
$images = arsort(array_reverse($images, true));
$ignore = Array(".", "..");
foreach($images as $curimg){
if(!in_array($curimg, $ignore)) {
echo "<img src=\"". $dirname . '/' . $curimg ."\">" ;
}
}
?>
Update:
$dirname = "Images";
$images = scandir($dirname);
sort($images,SORT_NUMERIC);
krsort($images);
$ignore = Array(".", "..");
foreach($images as $curimg){
if(!in_array($curimg, $ignore)) {
echo "<img src=\"". $dirname . '/' . $curimg ."\"> \n" ;
}
}
What I have been working with:
Without the sort(); and krsort(); i return:
<img src="Images/1.png">
<img src="Images/10.png">
<img src="Images/11.png">
<img src="Images/2.png">
<img src="Images/3.png">
<img src="Images/4.png">
<img src="Images/5.png">
<img src="Images/6.png">
<img src="Images/7.png">
<img src="Images/8.png">
<img src="Images/9.png">
With the krsort and sort.. I return:
<img src="Images/11.png">
<img src="Images/10.png">
<img src="Images/9.png">
<img src="Images/8.png">
<img src="Images/7.png">
<img src="Images/6.png">
<img src="Images/5.png">
<img src="Images/4.png">
<img src="Images/3.png">
<img src="Images/2.png">
<img src="Images/1.png">
Which I presume is what you are looking for.
http://www.php.net/manual/en/function.array-reverse.php
should be the right function instead of shuffle
UPDATE:
Better would be to sort it directly via scandir:
$images = scandir($dirname, SCANDIR_SORT_DESCENDING);
You could try to load image names into array, than sort array and then echo image tags
<?php
$dirname = "images";
$files = scandir($dirname, 1); // using SCANDIR_SORT_DESCENDING PHP 5.4+ ONLY!
foreach ($files as $file) {
if ($file !== '.' && $file !== '..') {
echo "<img src=\"". $dirname . '/' . $file ."\">" ;
}
}
?>