Jump to content

[SOLVED] Folder Contents Function?


sKunKbad

Recommended Posts

I'm looking for a function that puts the contents of a folder (the file names) into an array. Is there such a beast? For instance, if there is a folder off of my root called /tips/, and I want to list the files that are in it, such as tip1.txt, tip2.txt, etc.

Link to comment
Share on other sites

Why is it that with dir or scandir they always have the "." and the ".." listed as files?

 

Because they are. Everything is a file in linux . and .. are no exception.

 

. is a link pointing to the current directory, while

.. is a link pointing to the current directories parent.

 

You can simply avoid them within output by using an if(). eg;

 

<?php

  $d = scandir('foo');
  foreach($d as $f) {
    if ($f != '.' || $f != '..') {
      echo $f;
    }
  }

?>

Link to comment
Share on other sites

Oh!

 

Just to think I wrote a nice script to read the contents of a folder as well and sort the files and folders separately in alphabetical order.

 

If I'd have known about scandir() it'd have made my script a whole lot shorter... :(

Link to comment
Share on other sites

Why is it that with dir or scandir they always have the "." and the ".." listed as files?

 

Because they are. Everything is a file in linux . and .. are no exception.

 

. is a link pointing to the current directory, while

.. is a link pointing to the current directories parent.

 

You can simply avoid them within output by using an if(). eg;

 

<?php

  $d = scandir('foo');
  foreach($d as $f) {
    if ($f != '.' || $f != '..') {
      echo $f;
    }
  }

?>

 

In order for that to work, I had to replace the || with &&. Thanks for your help.

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.