I have a small problem: I want to show images from directory, what I successfully programmed, but just from directory which has a static name. How can I add here some filter, or something like that, when I send in variable osc number 31322357 it will automatically find directory 31322357 - Automatic Board and show images? Thanks.
Here is my script:
<?
//path to directory to scan
$osc=$_GET['osc'];
$directory="./$osc/";
//get all image files with a .jpg extension.
$images = glob("$directory{*.jpg,*.JPG,*.png}", GLOB_BRACE);
//print each file name
foreach($images as $image)
{
echo "<img src='$image' style='width:100px;height:auto'>";
}
?>
<?php
...
$dirs = glob("./$osc*",GLOB_ONLYDIR);
$images = array();
if(count($dirs))
foreach($dirs as $dir){
$images = array_merge($images,glob("$dir/{*.jpg,*.JPG,*.png}", GLOB_BRACE));
}
?>
Use a wildcard * in your directory name:
$directory = "./$osc*/";
$images = glob("$directory*.{jpg,JPG,png}", GLOB_BRACE);
Note that this will match 31322357 - Automatic Board and 31322357 - Other stuff, etc. and list files from all matching directories.
Related
This question already has answers here:
List all the files and folders in a Directory with PHP recursive function
(20 answers)
Closed 5 years ago.
below code only list files in directories and does not search sub-directories:
<?php
//path to directory to scan. i have included a wildcard for a sub directory
$directory = "/var/www/*/";
//get all image files with a .jpg extension.
$images = glob("" . $directory . "*.jpg");
$imgs = '';
// create array
foreach($images as $image){ $imgs[] = "$image"; }
//shuffle array
shuffle($imgs);
//select first 20 images in randomized array
$imgs = array_slice($imgs, 0, 1000);
//display images
foreach ($imgs as $img) {
echo $img . '<br>';
}
?>
but How can list all specific extension (for example *.jpg) in all directories and
sub directories of a path by using PHP?
You should use a recursive function.
This can help you:
List all the files and folders in a Directory with PHP recursive function
Look at the answer and change it to your own needs. (wanted to help you using your own code but it's too far from the right way doing it).
my first question here...
I'm trying to display the images dynamically from a folder inside a "fotorama" div. With this code:
<?php
$dir = "/example/images/category/";
$images = glob($dir.'*.{JPG,jpg,gif,png,bmp}', GLOB_BRACE);
foreach ($images as $image) {
echo "<img src='".$dir.".".$image."' />";
};?>
I know this have been asked before; but after reading everything I found: For example (here, here and here) plus many other "similar" questions.
They all recommend pretty much the same code (with slight differences in syntax between each other)
I've tried every variant of this code I've found but it displays nothing.
I know the ($dir) is fine, because when I try:
echo '<img src="'.$dir."img1.jpg".'">'; It displays that particular image. The problem must be in the glob part because when I use var_dump($images); before the foreach loop it shows this: array (0) {}.
I don't know what is the problem...
Edit:
With help from Sam's answer, the code now works I found out I had forgotten to correct something in the GLOB_BRACE part. The corrected code should be something like this:
<?php
$dir = "c:/xampp/htdocs/example/images/category/";
$images = glob($dir.'*.{JPG,jpg,gif,png,bmp}', GLOB_BRACE);
foreach ($images as $image) {
echo "<img src='".$image."' />";
};
?>
For future reference the path on Xampp localhost for the glob()function to work is: C:/xampp/htdocs/your-folder-structure
glob() looks for a filesystem path, whereas the src attribute of the image tag is pointing at a url location, probably relative to your docroot (but could be anywhere depending on your routing rules).
So, for example, if the /example/images/ url points to /var/www/example/images/ on the filesystem, you'd want to do:
<?php
$dir = "/var/www/example/images/category/";
$url = "/example/images/category/";
$images = glob($dir.'*.{JPG,jpg,gif,png,bmp}', GLOB_BRACE);
foreach ($images as $image) {
echo "<img src='{$url}{$image}' />";
};?>
You'll need to figure out where on the filesystem that directory is in your specific case though.
I am a newbie at PHP and I'm learning.
I've made a basic script where you can upload an image to a director on the server. I want the image names to get a number at the end so that the name won't be duplicated.
This is my script to add 1 to the name (I'm really bad at "for loops"):
for(x=0; $imageName => 50000; x++){
$imageFolderName = $imageName.$x;
}
Please tell me if I'm doing this totally wrong.
Adding to Niet's answer, you can do a foreach loop on all the files in your folder and prepend a number to the file name like so:
<?
$directory = 'directory_name';
$files = array_diff(scandir($directory), array('.', '..'));
$count = 0;
foreach($files as $file)
{
$count++;
rename($file, $count.'-'.$file);
}
?>
Alternatively you could rename the file to the timestamp of when it was uploaded and prepend some random characters to the file with the rand() function:
<?
$uploaded_name = 'generic-image.jpeg';
$new_name = time().rand(0, 999).$uploaded_name;
?>
You'll need to handle and move the uploaded files before and after the rename, but you get the general gist of how this would work.
Here's a potential trick to avoid looping:
$existingfiles = count(glob("files/*"));
// this assumes you are saving in a directory called files!
$finalName = $imageName.$existingfiles;
I'm sorry if the title is a little vague ... I'm still relatively new at PHP (3 months or so) Also, my native tongue is not English, so please bear with me :) I have also searched this site and google extensively to try and find a solution, but without any luck.
I have a script set up in my images directory that scans all the subdirectories, and then outputs a list of links that, if clicked, will take you to a page, where all the images of the selected subdirectory are displayed. The path to such a page would be:
www.mysite.com/images/list_images.php?folderName=RandomFolder
The code for this:
images/index.php
<?php
$path = 'images/' ;
$results = scandir($path);
for ($i=0;$i<count($results);$i++)
{
$result=$results[$i];
if ($result === '.' or $result === '..')
continue;
if (is_dir($path . '/' . $result))
{
echo "<a href='list_images.php?folderName=$result'>$result</a><br/>";
}
}
?>
--------------------
list_images.php
<?php
if(isset($_GET['folderName']))
$folder=$_GET['folderName'];
$path = 'images/'.$folder.'/' ;
$images = glob($path . '*.{jpg,jpeg,png,gif}', GLOB_BRACE);
foreach ($images as $image)
{
echo "<a href='$image'><img src='$image'/></a>";
}
?>
Now, my question:
In each of my image subdirectories I have another subdirectory called 'thumbs', that contains - yes, you guessed it - thumbnails. Each thumbnail is named exactly the same as its corresponding file in the directory above it. Now, how would I make the img src in the above code to point to the thumb?
Any help would be very welcome! Thank you in advance!
EDIT:
I looked over my code again, and I made few extra lines. It still doesn't work, but at least it now outputs thumbnails, which links to the larger image. Here's the new code:
list_images.php
if (isset($_GET['folderName'])) $folder=$_GET['folderName'];
$path = 'images/'.$folder.'/' ;
$thumb_path = ''.$path.'/thumbs/';
$thumbs = glob($thumb_path . '*.{jpg,jpeg,png,gif}', GLOB_BRACE);
$images = glob($path . '*.{jpg,jpeg,png,gif}', GLOB_BRACE);
foreach ($thumbs as $thumb){
foreach ($images as $image){
echo "<a class='fancybox' href='$image'><img src='$thumb'/></a>";
}
}
It kinda works now. The only problem is, that it outputs 13 identical thumbnails to each picture - and it does it 13 times (for a directory containing 13 image files) so there is 169 thumbnails in total.
Any ideas how to fix this?
If you are sure that the folder name is thumbs, there is no reason you can't hardcode this. Take a look at the following.
echo "<a href='$image'><img src='thumbs/$image'/></a>";
You could do a str_replace on the path.
If the path to the image is mydir\image01\pic01.jpg
str_replace('image01','image01\thumb',$image);
would point to mydir\image01\thumb\pic01.jpg
I am looking for some help with my code, I have looked elsewhere but am having difficulty to really understand what is going on with the code given elsewhere and I am hoping someone can help me.
I have one gallery page that uses $_POST to change the folder the gallery gets it images form based on the link clicked.
What I want now is to code a search function that looks through them all for a string (a jpg) when it finds it, it returns its img tags and displays the image.
I am having trouble making scandir work and display currently using this code
<?php
$dir = "/galleries/images/adult-cakes/images/";
$scan = scandir($dir);
echo $dir;
print_r($scan);
foreach ($scan as $output) {
echo "$output" . "<br />";
}
?>
that returns the echo dir but nothing else ( please note print was something I tried it was echo before and neither is working.
Then I need to get the output of all the gallery types, adult, anniversary etc and put them into a loop like so
search criteria = cake 1(.jpg)
put scandir info into $folderarray
search this folder until found -
galleries/images/$folderarray/images/
loop
if found then echo img tags with link to pic
if not display not found
This will get an array of all the files in directory $dir
<?php
$dir = "/galleries/images/adult-cakes/images/";
$images = glob($dir . '*');
?>
Do this to get all subdirectories of $Dir into array $DirArray:
$Dir = '/galleries/images/'; //
foreach ( $DirArray = array_filter(glob($Dir . '*'), 'is_dir') as $DirName ) {
$DirName = str_replace($Dir, '', $DirName); // Optionally, remove path from name to display
echo "Dir Name: $DirName <br />\n"; // Test
}
echo var_dump($DirArray); // Test
Modify accordingly