I'm loading a folder full of images in, to create a jQuery Image Gallery.
There are currently 100 images being loaded in to create the gallery. I've got all that to load without issue.
All I want to do is make the image(s) that are loaded, load in randomly.
How do I achieve this?
My code is :
<?php
$folder = "images/";
$handle = opendir($folder);
while(($file = readdir($handle)) !== false) {
if($file != "." && $file != "..")
{
echo ("<img src=\"".$folder.$file."\">");
}
}
?>
Thanks in advance.
Just store all the image paths in an array and do a random shuffle of the array. And then echo the elements
<?php
$folder = "images/";
$handle = opendir($folder);
$imageArr = array();
while(($file = readdir($handle)) !== false) {
if($file != "." && $file != "..")
{
$imageArr[] = $file;
}
shuffle($imageArr); // this will randomly shuffle the image paths
foreach($imageArr as $img) // now echo the image tags
{
echo ("<img src=\"".$folder.$img."\">");
}
}
?>
Traverse the directory and store the image file names into an array and randomly select path names from the array.
A basic example:
$dir = new DirectoryIterator($path_to_images);
$files = array();
foreach($dir as $file) {
if (!$fileinfo->isDot()) {
$files[] = $file->getPathname();
}
}//$files now stores the paths to the images.
You can try something like this:
<?php
$folder = "images/";
$handle = opendir($folder);
$picturesPathArray;
while(($file = readdir($handle)) !== false) {
if($file != "." && $file != "..")
$picturesPathArray[] = $folder.$file;
}
shuffle($picturesPathArray);
foreach($picturesPathArray as $path) {
echo ("<img src=\"".$path."\">");
}
?>
Related
So, simply want my webdrive folder to be "browseable", so I found this script that does it for me.
The problem is, that the list is not sorted by name
How needs the code to be modified so its sorted by filename?
<?php
$dir_open = opendir('.');
while(false !== ($filename = readdir($dir_open))){
if($filename != "." && $filename != ".."){
$link = "<a href='./$filename'> $filename </a><br />";
echo $link;
}
}
closedir($dir_open);
?>
You can store the filenames in an array, then sort them using sort()
$files = [];
$dir_open = opendir('.');
while(false !== ($filename = readdir($dir_open))){
if($filename != "." && $filename != ".."){
$files[] = $filename; // store filename
}
}
closedir($dir_open);
// Then, sort and display
sort($files);
foreach ($files as $filename) {
$link = "<a href='./$filename'> $filename </a><br />";
echo $link;
}
the code is
<?php
$files = array();
$dir = opendir('/xampp/htdocs/myfun/template/home');
while(($file = readdir($dir)) !== false) {
if($file !== '.' && $file !== '..') {
$files[] = $file;
}
}
closedir($dir);
//sort($files);
$i=0;
foreach($files as $key) {
echo "<iframe align='center' width='100%' height='605px' src='$key' title='$files[$i]'></iframe>";
break;
}
?>
i want to show templates onclick next/prev button.horizontally.
like slider.
please help
Correct logic to iterate a directory in while loop is false != ($file = readdir($dir)), where you assign file name first and check it's not false.
Also you can print title with $key itself. $files[$i] and $i variable is not required.
In order to iterate through all files of directory remove break, this will cause to end the loop after first iteration.
<?php
$files = array();
$dir = opendir('/xampp/htdocs/myfun/template/home');
while(false != ($file = readdir($dir)))
{
if($file !== '.' && $file !== '..')
{
$files[] = $file;
}
}
closedir($dir);
//sort($files);
foreach($files as $key)
{
echo "<iframe align='center' width='100%' height='605px' src='$key' title='$key'></iframe>";
}
?>
I am trying to sort a list of directories according to the contents of a text file within each directory.
So far, I am displaying the list of directories:
$user = $_GET['user'];
$task_list = $_GET['list'];
if ($handle = opendir("../users/$user/tasks/$task_list/"))
{
$files = array();
while (false !== ($file = readdir($handle)))
{
if ($file != "." && $file != ".." && $file != ".htaccess")
{
array_push($files, $file);
}
}
closedir($handle);
}
//Display tasks
sort($files);
foreach ($files as $file)
{
echo "$file";
}
Each directory has a text file within it called due.txt, I would like to sort the list of directories according to the contents of this file due.txt.
So far, I have tried:
$user = $_GET['user'];
$task_list = $_GET['list'];
if ($handle = opendir("../users/$user/tasks/$task_list/"))
{
$files = array();
$tasksSort = array();
while (false !== ($file = readdir($handle)))
{
if ($file != "." && $file != ".." && $file != ".htaccess")
{
$taskSort = file_get_contents("../users/$user/tasks/$task_list/$file/due.txt");
array_push($files, $file);
array_push($tasksSort, $taskSort);
}
closedir($handle);
}
//Sort tasks and display
sort($tasksSort);
foreach ($files as $file)
{
echo "$file";
}
}
But the $tasksSort array doesn't seem to have any content to sort...?
sort($tasksSort);
foreach ($tasksSort as $file)
{
echo "$file";
}
I was wondering if anyone could help me write a PHP script for me that renames all the files in a directory in a sequence.
So...
DSC_10342.JPG -> 1.JPG
DSC_10343.JPG -> 2.JPG
DSC_10344.JPG -> 3.JPG
and so on.
Here's my version:
// open the current directory (change this to modify where you're looking)
$dir = opendir('.');
$i = 1;
// loop through all the files in the directory
while (false !== ($file = readdir($dir)))
{
// if the extension is '.jpg'
if (strtolower(pathinfo($file, PATHINFO_EXTENSION)) == 'jpg')
{
// do the rename based on the current iteration
$newName = $i . '.jpg';
rename($file, $newName);
// increase for the next loop
$i++;
}
}
// close the directory handle
closedir($dir);
Use rename to rename the files. You can use this handy script to loop through all files in a directory:
<?php
if ($handle = opendir('.')) {
while (false !== ($file = readdir($handle))) {
if ($file != "." && $file != "..") {
echo "$file\n";
}
}
closedir($handle);
}
?>
Then it's just a matter of looking at the filenames ($file) and figuring out what number to give them. If you need more help than that, just tell me and I'll give more details.
Try this:
$handler = opendir($directory);
$index = 1;
while ($file = readdir($handler)) {
if ($file != "." && $file != "..") {
rename($directory."/".$file, $directory."/".$index.".JPG");
$index++;
}
}
closedir($handler);
Using someone's snippet it would look like this:
<?php
$path = '.';
$i = 1;
if ($handle = opendir($path)) {
while (false !== ($file = readdir($handle))) {
if ($file != "." && $file != ".." && is_file($path.'/'.$file)) {
$oldname = $path.'/'.$file;
$path_info = pathinfo($oldname);
rename($oldname, $path.'/'.($i++).'.'.$path_info['extension']);
}
}
closedir($handle);
}
?>
It will rename files with all extensions and skip directories that may be inside your directory.
i have a propably rather simple question:
I'm using the following script to read a folder?
$count = 0;
if ($handle = opendir(PATH)) {
$retval = array();
while (false !== ($file = readdir($handle))) {
$ext = pathinfo($file, PATHINFO_EXTENSION);
if ($file != '.' && $file != '..' && $file != '.DS_Store' && $file != 'Thumbs.db') {
$retval[$count] = $file;
$count = $count + 1;
} else {
//no proper file
}
}
closedir($handle);
}
if a file is an image I print it as an image: print "";
However i wonder how i can display FOLDERS? If i have a subfolder inside of the folder i'm currently running through? how can i print that one?
use glob instead of scandir
$dirs = glob(PATH."/*", GLOB_ONLYDIR);
$images = glob(PATH."/*.[jJ][pJ][gG]");
foreach ($dirs as $name) echo "<b>$name</b><br>\n";
foreach ($images as $name) echo $name."<br>\n";
Use scandir instead of readdir. http://us.php.net/manual/en/function.scandir.php