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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

 

Link to comment
Share on other sites

<?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?

Link to comment
Share on other sites

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>";

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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!

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.