DamienRoche Posted September 22, 2008 Share Posted September 22, 2008 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. Quote Link to comment Share on other sites More sharing options...
xtopolis Posted September 22, 2008 Share Posted September 22, 2008 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. Quote Link to comment Share on other sites More sharing options...
DamienRoche Posted September 22, 2008 Author Share Posted September 22, 2008 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. Quote Link to comment Share on other sites More sharing options...
xtopolis Posted September 22, 2008 Share Posted September 22, 2008 <?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? Quote Link to comment Share on other sites More sharing options...
JasonLewis Posted September 22, 2008 Share Posted September 22, 2008 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>"; Quote Link to comment Share on other sites More sharing options...
DamienRoche Posted September 22, 2008 Author Share Posted September 22, 2008 Thanks! That array works great. However, it is still spitting out those strange filenames. I didn't realize glob was better for this. I'll try that. Thanks. Quote Link to comment Share on other sites More sharing options...
xtopolis Posted September 22, 2008 Share Posted September 22, 2008 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. Quote Link to comment Share on other sites More sharing options...
DamienRoche Posted September 22, 2008 Author Share Posted September 22, 2008 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! Quote Link to comment Share on other sites More sharing options...
JasonLewis Posted September 22, 2008 Share Posted September 22, 2008 One more thing. You don't need to place $loop inside the []. Unless you want the array index to start at 1 then just putting [] would be fine. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.