i am looking for a way to just show folders in php.
i am getting files like this returned from my amazon foreach loop using undesigneds s3 class.
Computer Fix Files/files/driver-ml-dlan-usb-windows-r2.exe
here my foreach.
foreach ($contents as $file){
$folder = rtrim($file['name'], "/");
if (!preg_match("/[0-9\.\-\_]/i", $folder)) { ?>
<?php echo $folder; ?>
<?php }
}
ok this kinda works but what i really want to do is just list the first folder.
so instead of this.
Computer Fix Files/files/
it would just show.
Computer Fix Files
any help please????
echo substr($folder, 0, strpos($folder, '/'));
echo reset(explode('/', $folder));
should work.
Related
My directory structure is like D:/dir1/dir2/project_dir/dir3/dir4/dir5/cache/all_files
So i want to get all the files under cache folder.
So i wrote
glob("project_dir/*/*/*/cache/*");
But cache folder is also there in dir3 or dir4 like
D:/dir1/dir2/project_dir/dir3/dir4/dir5/dir6/dir7/cache/all_files
or
D:/dir1/dir2/project_dir/dir3/cache/all_files
So can anyone give me the regex to get the files from 'cache' folder,
like
glob("project_dir/*/cache/*");
Btw, this is not working because it is searching for immediate directory after project_dir.
Thanks in advance.
Hope this helps!
Supports PHP 5 and 7 only,check your version before using this code
for more info refer : http://php.net/manual/en/class.directoryiterator.php
$searchCacheFolderUnder = 'D:\xampp\htdocs\mygit\\';
$pathOfAllCacheFolders = array();
$dir = new DirectoryIterator(realpath($searchCacheFolderUnder));
foreach ($dir as $fileInfo) {
if($fileInfo->isDir()) {
// optimize strpos with inbuild directory iterator methods
if( strpos($fileInfo->getPathname(), 'cache') !== false ){
$pathOfAllCacheFolders[] = $fileInfo->getPathname();
}
}
}
// contains all cache folders path
echo "<pre>";
print_r($pathOfAllCacheFolders);
echo "</pre>";
// loop on all cache folders
foreach($pathOfAllCacheFolders as $cachePath)
$dirCache = new DirectoryIterator(realpath($cachePath));
foreach ($dirCache as $fileInfo) {
//$fileInfo object will have everything you need
echo "<pre>";
print_r($fileInfo);
echo "</pre>";
}
You have to modify this code a bit to meet your requirement. Refer Directory Iterator for more available options.
You can use glob('*') but the only perk for using this is it ignores all hidden files. If you want all the files that includes hidden files in a folder then use:
$files = glob('project_dir/cache/{,.}*', GLOB_BRACE);
Remember this will return all the files for "." and "" and even the directory entries . and .. Let me know in case of any issues.
Hope this helps.
i am trying to find a solution for the following problem:
I have two kinds of files in a folder of my webserver:
locked files__date_time.txt
locked files_date_time.txt
I would like to rename the files of the second type with the single underscore between "filename" and "date" like "locked files_date_time.txt" to "locked_files_date_time.txt" with a simple php-script.
This for i found a sample script, but its not working.
<?php
$directory = '/daten/www/htdocs/files/locked/';
foreach (glob($directory."*locked files*") as $filename) {
$file = realpath($filename);
rename($file, str_replace("*locked files*","locked_files",$file));
}
?>
Script is geeting executed, without error, but nothing happens with the files.
What is wrong there?
Would be pleased about some support. Thanks very much in advance.
Greetz
To update the file names this line needs to be updated:
Old:
str_replace("*locked files*","locked_files",$file));
New:
str_replace("locked__files","locked_files",$file));
To find the files that you are looking for this line needs to be updated:
Old:
foreach (glob($directory."*locked files*") as $filename) {
New:
foreach (glob($directory."*locked__files*") as $filename) {
That is looking for files that match what is between the double quotes.
<?php
$directory = '/daten/www/htdocs/files/locked/';
foreach (glob($directory."*locked files*") as $filename) {
$file = realpath($filename);
rename($file, str_replace("locked__files","locked_files",$file));
}
?>
It will solve your problem
rename($file, preg_replace("/locked files/","locked_files",$file));
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'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