I want to open a directory and read all the files inside and put them into an array
, so far I have:
$imagesdir = $CFG->dataroot. /1/themeimages/ ;
Which gives me the path to the directory, whats the next step?
I want to open a directory and read all the files inside and put them into an array
, so far I have:
$imagesdir = $CFG->dataroot. /1/themeimages/ ;
Which gives me the path to the directory, whats the next step?
Since from your tag list I assume you re using Moodle, you can simply use the:
function get_directory_list($rootdir, $excludefiles= , $descend=true, $getdirs=false, $getfiles=true)
The function is contained in moodlelib.php.
Reading from the doc:
- Returns an array with all the filenames in * all subdirectories, relative to the given rootdir.
Please refer to the official doc for more information about the optional parameters.
Functions for reading content of files are also available in filelib.php.
A solution would be to use opendir
+ readdir
+ closedir
(quoting an example from the first page) :
$imagesdir = $CFG->dataroot. /1/themeimages/ ;
if ($handle = opendir($imagesdir)) {
while (false !== ($file = readdir($handle))) {
if ($file != "." && $file != "..") {
echo "$file
";
}
}
closedir($handle);
}
Another solution would be to use the [DirectoryIterator
class ; quoting the example from]4 the __construct
page :
$imagesdir = $CFG->dataroot. /1/themeimages/ ;
$dir = new DirectoryIterator($imagesdir);
foreach ($dir as $fileinfo) {
if (!$fileinfo->isDot()) {
var_dump($fileinfo->getFilename());
}
}
Of course, in each case, instead of just echoing or dumping the name of the file, you d have to put it into an array.
This would mean initializing the array before the loop :
$list_files = array();
And, inside the loop, use something like one of those two lines, depending on which solution you choose :
$list_files[] = $file;
$list_files[] = $fileinfo->getFilename();
Just use the built in function scandir:
Returns an array of files and directories from the directory .
So you would use it like this:
$array = scandir($imagesdir);
Of course you can use DirectoryIterator too, but this is much simplier.
You can also strip dot files:
$array = array_diff(scandir($imagesdir), array( . , .. ));
For a more OO approach use the DirectoryIterator class.
$images = array();
$imagesdir = $CFG->dataroot. /1/themeimages/ ;
foreach (new DirectoryIterator($imagesdir) as $file) {
if($file->isDot()) continue;
$images[] = $file;
}
here s the shortest solution if you also need a filter later on:
$imagesdir = $CFG->dataroot. /1/themeimages/*.* ;
foreach (glob($imagesdir) as $file)
array_push($files,$file);
php automatically excludes .
and ..
you can also specify your own mask if you don t need ALL files, as shown in above *.*
php also automatically creates the $files
array for you.
the last line can also be:
$files[]=$file;
I am trying to write a script to prevent brute-force login attempts in a website I m building. The logic goes something like this: User sends login information. Check if username and password is ...
<?php $con=mysql_connect("localhost","mts","mts"); if(!con) { die( unable to connect . mysql_error()); } mysql_select_db("mts",$con); /* date_default_timezone_set ("Asia/Calcutta"); $date = ...
I found this script online that creates a thumbnail out of a image but the thumbnail image is created with poor quality how can I improve the quality of the image. And is there a better way to create ...
如何确认来自正确来源的数字。
Most sites need some way to show the dates on the site in the users preferred timezone. Below are two lists that I found and then one method using the built in PHP DateTime class in PHP 5. I need ...
I wonder there is a way to post a message to a facebook business page with cURL? thanks
I want to create text as a watermark for an image. the water mark should have the following properties front: Impact color: white opacity: 31% Font style: regular, bold Bevel and Emboss size: 30 ...
How does php cast boolean variables? I was trying to save a boolean value to an array: $result["Users"]["is_login"] = true; but when I use debug the is_login value is blank. and when I do ...