Current location: Hot Scripts Forums » Programming Languages » PHP » Select one type of file only from directory


Select one type of file only from directory

Reply
  #1 (permalink)  
Old 04-17-04, 01:55 PM
Bonzo's Avatar
Bonzo Bonzo is offline
Coding Addict
 
Join Date: Jan 2004
Posts: 340
Thanks: 0
Thanked 0 Times in 0 Posts
Select one type of file only from directory

I have a bit of code I am using to list all the files in a directory. But all I realy want are the "jpg" can anyone tell me either how to select the jpg only or how to delete everything apart from jpg from the output list.

Thanks Anthony

PHP Code:

function get_dirlist($start_dir) {

exec("ls -R $start_dir",$f_list);
$dir_str $start_dir;
$filelist[0] = $start_dir$i 1;
for (
$count=0$count<count($f_list); $count++) {
   if (
$f_list[$count] == "") { continue; }
   if (
substr($f_list[$count],strlen($f_list[$count])-1,1) == ":") {
     
$dir_str substr($f_list[$count],0,strlen($f_list[$count])-1);
     
$filelist[$i] = $dir_str;
     
$i++;
   } else {
     
$file_str "$dir_str/$f_list[$count]";
     if (
is_file($file_str)) {
       
$filelist[$i] = $file_str;
       
$i++;
     }
   }
}
return 
$filelist;

Reply With Quote
  #2 (permalink)  
Old 04-18-04, 04:55 AM
Stefan's Avatar
Stefan Stefan is offline
Junior Code Guru
 
Join Date: Jun 2003
Location: Utrecht, The Netherlands
Posts: 599
Thanks: 0
Thanked 0 Times in 0 Posts
what about:

PHP Code:

function get_dirlist($start_dir) {

exec("ls -R $start_dir *.jpg",$f_list);
$dir_str $start_dir;
$filelist[0] = $start_dir$i 1;
for (
$count=0$count<count($f_list); $count++) {
   if (
$f_list[$count] == "") { continue; }
   if (
substr($f_list[$count],strlen($f_list[$count])-1,1) == ":") {
     
$dir_str substr($f_list[$count],0,strlen($f_list[$count])-1);
     
$filelist[$i] = $dir_str;
     
$i++;
   } else {
     
$file_str "$dir_str/$f_list[$count]";
     if (
is_file($file_str)) {
       
$filelist[$i] = $file_str;
       
$i++;
     }
   }
}
return 
$filelist;

Reply With Quote
  #3 (permalink)  
Old 04-18-04, 08:10 AM
Bonzo's Avatar
Bonzo Bonzo is offline
Coding Addict
 
Join Date: Jan 2004
Posts: 340
Thanks: 0
Thanked 0 Times in 0 Posts
Thanks for the info Stefan but it had no effect.

I tried putting the results into an array and using ' *.jpg ' but was not sure that '*.jpg' could be used. Now I see it can I will have another go tonight.

Any other help would be gratfuly recived.

Anthony
Reply With Quote
  #4 (permalink)  
Old 04-26-04, 04:40 PM
Bonzo's Avatar
Bonzo Bonzo is offline
Coding Addict
 
Join Date: Jan 2004
Posts: 340
Thanks: 0
Thanked 0 Times in 0 Posts
I am still trying to get this to work and have found another method that I still cant get to work.

PHP Code:

$myDirectory opendir("$image_path");


    while(
$entryName readdir($myDirectory))
    {
    
$dirArray[]=$entryName;
    }

 function 
select_png($select)
{
    return(
$select == '*.png');
}

// line to view what results I am getting ; currently Array ( )
 
print_r(array_filter($dirArray"select_png")); 
Can someone help me out ?

Anthony
Reply With Quote
  #5 (permalink)  
Old 04-27-04, 12:49 AM
blaw's Avatar
blaw blaw is offline
Junior Code Guru
 
Join Date: Dec 2003
Location: Vancouver, BC, Canada
Posts: 550
Thanks: 0
Thanked 0 Times in 0 Posts
Hi there,

Maybe you want to this.

PHP Code:

$myDirectory opendir("$image_path");

while(
$entryName readdir($myDirectory)) {
    if (
substr($entryName, -3) == 'jpg') {
        
$dirArray[]=$entryName;
    }

Trick here is that you check if each file you're getting from the directory resource is a jpg file or not by looking at the last three characters of it (with substr()). It doesn't check the actual file type, so even if it's an myapp.exe file renamed as myapp.jpg, this code will pick it up.

Stefan's is to filter the non-jpg files BEFORE putting them into the directory resource, while mine is AFTER. Ideally, you should go with Stefan's, because it's less memory consuming, depending on how many non-jpg files you are looking at. (but mine also works on Windows...!)
__________________
Blavv =|
Reply With Quote
  #6 (permalink)  
Old 04-27-04, 04:02 AM
Bonzo's Avatar
Bonzo Bonzo is offline
Coding Addict
 
Join Date: Jan 2004
Posts: 340
Thanks: 0
Thanked 0 Times in 0 Posts
Dont let NeverMind see this he will probably laugh himself to death !!!

This is a right old " mish mash " and there must be a betterway; I could not get your method to work blaw but I have come up with the below using it.

Anthony

PHP Code:

$myDirectory opendir("$image_path");

// What I want is something to only read jpg here from the directory
while($entryName readdir($myDirectory))
{
$dirArray[]=$entryName;
}

$indexcount count($dirArray);

for (
$i=2$i<$indexcount$i++ )
  {
    if ( 
substr($dirArray[$i], -3) == 'jpg')
    {echo 
$dirArray[$i]; } // Just to test the output
// Output is banner.jpgkatie.jpg.......etc.
// So I need to build another array from this - very longwinded way of doing it
  

Reply With Quote
  #7 (permalink)  
Old 04-27-04, 04:58 AM
Bonzo's Avatar
Bonzo Bonzo is offline
Coding Addict
 
Join Date: Jan 2004
Posts: 340
Thanks: 0
Thanked 0 Times in 0 Posts
Dont know what I was doing wrong but :

PHP Code:

$myDirectory opendir("$image_path");

while(
$entryName readdir($myDirectory)) {
    if (
substr($entryName, -3) == 'jpg') {
        
$dirArray[]=$entryName;
    }
}

closedir($myDirectory);

$indexcount count($dirArray);
for (
$i=0$i<$indexcount$i++ )
{ echo 
$dirArray[$i];}
// Output banner.jpgkatie.jpg......etc. 
Now works Anthony
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Upload Script Problem!!! seanknighton Perl 0 03-21-04 09:54 PM
restrict file size per directory? johnnytalk PHP 0 03-21-04 05:24 PM
i know how to delete a file! but how to delete a directory? forcer JavaScript 3 01-28-04 07:00 AM
Need Epinions-lite system in PHP & MYSQL wali001 Job Offers & Assistance 4 01-12-04 06:02 AM
Upload file type and size limiter! Arctic ASP 1 08-02-03 07:06 PM


All times are GMT -5. The time now is 10:26 AM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.