4

Hi,

I have the following files in a directory called content: index.php, page1.php, page2.php and page3.php ONLY.

Then I have this code:

 $column = scandir("content");
 foreach ($column as $value) {   
  $stvalue = str_replace(".php", "", $value);    
  echo "<div>$stvalue</div>";
}

Nevertheless, this is what I get:

<div>.</div>
<div>..</div>
<div>index</div>
<div>page1</div>
<div>page2</div>
<div>page3</div>

Whats with the first 2 elements? I dont have files named like that so I dont get it.

Thank you.

Cain Nuke
  • 2,843
  • 5
  • 42
  • 65
  • 1
    The dot(.) and the double dot (..) are Linux directories. They are present in each folder. The dot(.) is referencing the current directory while the double dot(..) references the parent directory – MaD Apr 28 '16 at 06:38
  • I can figure that out but they should not appear because I dont need them. How can I get rid of them? Besides Im not even on linux but on windows. – Cain Nuke Apr 28 '16 at 06:43
  • You can filter the special directories. Using array_diff or explicitly check for them with if statement. you can use the glob() function to fetch only .php files for example. This way you avoid the special directories but be careful as only .php files will be shown. For instance. glob("*.php"); – MaD Apr 28 '16 at 06:49
  • You mean like with preg_match? – Cain Nuke Apr 28 '16 at 06:50
  • 1
    Some options that I can think about: glob("*.php"); or $files = array_diff(scandir("content"), array('..', '.')); or if($value != '.' and $value != '..') { ... } – MaD Apr 28 '16 at 06:54
  • Thanks. I think I will use array_diff(scandir("content"), array('..', '.')); because looks like the shorter way. – Cain Nuke Apr 28 '16 at 06:58
  • If my answer indeed helped you. Kindly accept it. Thank you. – MaD Apr 28 '16 at 07:18

3 Answers3

5

. - is a special directory referencing the current directory.

.. - is also a special directory and its referencing the parent directory.

To remove the special directories I can think of some options:

1.

foreach(glob("*.php") as $filename) {
    echo "<div>$filename</div>";
}

2.

$files = array_diff(scandir("content"), array('..', '.'));
foreach($files as $file) { ... }

3.

foreach ($files as $file) {    
    if($file != '.' and $file != '..') { ... } 
}

All of the above are alternatives. You don't need to use scandir() if you use glob() and vice versa. glob() - expects a pattern. It is possible to also provide it with the path like this:
glob("[path]/*.php") - this will list any php file located in path. glob() documentation can be found here PHP - glob()

MaD
  • 780
  • 7
  • 19
  • If I use glob then I dont need scandir? – Cain Nuke Apr 28 '16 at 07:23
  • No. It's an alternative way. Glob excepts a pattern so you can specify to list any pattern you want. See the documentation for it: http://php.net/manual/en/function.glob.php – MaD Apr 28 '16 at 07:24
  • I looked at it but I cant figure out how to use it along with scandir. – Cain Nuke Apr 28 '16 at 07:27
  • You don't use it with scandir. Glob is another way of listing the files in a directory(s). When you use glob. don't use scandir. Its alternative. Also, see the example I've written in the answer. Try printing the files like this: print_r(glob("[path]/*.php")); – MaD Apr 28 '16 at 07:29
  • Is it possible to specify the directory name? – Cain Nuke Apr 28 '16 at 07:31
1
$column = scandir("content");
foreach ($column as $value) {   
$stvalue = str_replace(".php", "", $value); 
if($stvalue != '.' || $stvalue != '..')   {
echo "<div>$stvalue</div>";
}
}
Bender
  • 705
  • 11
  • 25
0

. refers to the current directory. .. refers to the parent directory. This is why typing cd .. on a command prompt will change to the parent directory. It's been this way in DOS, every version of Windows, Mac OS X, it's not just Linux and UNIX variants.

If you don't want to display them on your page, just skip the first two entries using array_slice.

sahuk
  • 91
  • 1
  • 6