Welcome Guest, Not a member yet? Register   Sign In
searching files in a folder
#1

[eluser]A.M.F[/eluser]
how can i search for images inside a folder and count them?

e.g
i want to print all the images, in the folder FOLDER, that their name starts with an A

thanks
#2

[eluser]xwero[/eluser]
You can use get_filenames from the file helper to get the filenames from the folder and then you do a loop
Code:
function filter_directory($path,$regex)
{
    $files = get_filenames($path);
    $extensions = array('.jpg','.gif','.png');

    foreach($files as $key => $file)
    {
         if(!preg_match($regex,$file) || !in_array(strtolower(strrchr($file,'.')),$extensions))
         {
             unset($files[$key]);
         }
    }
    return $files;
}
The usage for your example is
Code:
$files = filter_directory('my/albums','/^a/i');
#3

[eluser]A.M.F[/eluser]
that what i tried to do but i keep getting the same error:
Quote:A PHP Error was encountered

Severity: Warning

Message: Invalid argument supplied for foreach()

Filename: content/gallery.php

Line Number: 12

although the direcotry does exists and it is on my local server on my computer (i am using WAMP). when i am trying to go to directory through the browser i get a FROBIDDEN error. why?
#4

[eluser]xwero[/eluser]
Change your apache settings to allow script access for that directory.

I've modified the function: $path instead of 'path/to/directory'.
#5

[eluser]A.M.F[/eluser]
[quote author="xwero" date="1203699831"]Change your apache settings to allow script access for that directory.
[/quote]

how can i do it?
*this directory is inside my CI directory
#6

[eluser]xwero[/eluser]
you have to change your httpd.conf or add a .htaccess file with following line in it
Quote:Allow from all
or
Quote:Allow from 127.0.0.1
#7

[eluser]m4rw3r[/eluser]
If you only want to filter with a prefix/suffix etc, you can use the glob() function: www.php.net/glob
#8

[eluser]xwero[/eluser]
nice call m4rw3r
Code:
function filter_directory($pathregex,$nameregex)
{
    $files = glob(sql_regcase($pathregex)); //sql_regcase for case insensitive match
    foreach($files as $key => $file)
    {
         if(!preg_match($nameregex,$file))
         {
             unset($files[$key]);
         }
    }
    return $files;
}
and the usage is
Code:
$files = filter_directory(real_path('my/albums').'/*\.jpg|gif|png','/^a/i');
I have no experience with the glob function so i'm not sure if it works but if it does it's better because it has no dependencies.




Theme © iAndrew 2016 - Forum software by © MyBB