Jump to content

readdir and put filenames into variables, or an array..


DamienRoche

Recommended Posts

I'm not sure how to approach this.

 

I have read a directory and produced a list of files:

 

file1    ///////// $file1
file20  ///////// $file2
file56  ///////// $file3
file23  ///////// $file4
file8   ///////// $file5

 

But how do I put this list into a set of variables as shown, or an array?

 

Greatly appreciate any help. Thanks.

Depends how you read the directory a little bit... but usually this is sufficient:

<?php
  $files[] = (however you read the directory);
?>

 

the [] puts the file at the end of the array each time.

 

If you're still having trouble, post your directory parsing code, ie: readdir, or glob, etc.. so we know how to further assist.

I'll try that but I'm also having another issue.

 

here is the code:

 

if ($handle = opendir('../success/')) {
$loop = 1;
    while (false !== ($file = readdir($handle))) {
        
	if(preg_match("/\./", "$file")){

	echo "file:$file<br>";
	$loop++;}}
	}

 

The output of this:

 

file:..

file:.

file:index.php

file:(4).php

file:(2).php

file:(3).php

file:(5).php

 

NOTE: I have two folders in this directory.

 

I don't know how to get rid of the first two empty file rows. If you notice, my directory is ../success/ which is the directory this code is in. How do I just check the current directory?

 

I'll give that array a bash but i have no idea where to start  :S

 

Thanks.

 

<?php
$filesArray = array(); //just defining the array

if ($handle = opendir('./')) {  //       "./"   means current directory
$loop = 1;
    while (false !== ($file = readdir($handle))) {
        
	if(preg_match("/\./", "$file")){

	echo "file:$file<br>";
                  $filesArray[] = $file;//add the file into the files array
	$loop++;}}
	}
?>

 

There are some } missing that you didn't copy, so I left them out as well.  That should get you started, but if you have more trouble, let us know what you are trying to do with the file listing... I'm not sure why you're preg-ing for "." ?  Trying to get only files based on the assumption that they will have an extension?

Just throw in an if statement and make sure that $file != . and $file != ..

Instead of using readdir() I would recommend using glob(). Far better.

 

$files = array();
foreach(glob("dir/*") as $file){
$files[] = array("path" => $file, "name" => basename($file));
}

echo "<pre>", print_r($files), "</pre>";

Throw in an is_dir/is_file check into Project's code:

<?php
$files = array();
foreach(glob("dir/*") as $file){
  if(!is_dir($file)){
    $files[] = array("path" => $file, "name" => basename($file));
  }
}

echo "<pre>", print_r($files), "</pre>";
?>

 

I used is_dir because I think I had trouble with is_file, but you can use either.

Just checking back to say glob worked a treat!

 

<?php

$loop = 1;
foreach(glob("*.*") as $file){

$files["$loop"] = $file;
echo "file:$file<br>";

$loop++;

}
echo "$files[1]";
?>

 

I also simplified to suit my tastes.

 

To make sure it is a file, I have simply used glob("*.*")

 

Thanks for all your help guys!

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.