# \[PHP\] Read a dir into an Array

**URL:** <https://forum.kirupa.com/t/php-read-a-dir-into-an-array/182669>\
**Category:** programming\
**Created:** [March 20, 2006, 4:24pm UTC](https://forum.kirupa.com/t/php-read-a-dir-into-an-array/182669 "2006-03-20T16:24:26Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![jw06](https://avatars.discourse-cdn.com/v4/letter/j/838e76/32.png) [@jw06](https://forum.kirupa.com/u/jw06)\
**Post date:** [March 20, 2006, 4:24pm UTC](https://forum.kirupa.com/t/php-read-a-dir-into-an-array/182669/1 "2006-03-20T16:24:26Z")

</div>

I’m having problems reading a directory into an array.

here is what I’m using to get a list of files:

```php

&lt;?php
function ls($path){
	//Define Path
	//$path = "/home/omnifiles/public_html";
	
	$dir_handle = @opendir($path) or die("Unable to open $path");
	//echo "Directory Listing Of $path&lt;br /&gt;";
	
	while ($file = readdir($dir_handle)){
		if ($file != "." && $file != ".."){
"&lt;a href='$file'&gt;$file&lt;/a&gt; extension: ". showExtension($file) . " filesize: ". fsize($file) ." permissions: " . fileperms($file) . "&lt;br /&gt;";
		}
	}
	
	closedir($dir_handle);
}
```

---

<div class="post-metadata">

**Author:** ![Voccart](https://avatars.discourse-cdn.com/v4/letter/v/ea5d25/32.png) [@Voccart](https://forum.kirupa.com/u/Voccart)\
**Post date:** [March 20, 2006, 11:55pm UTC](https://forum.kirupa.com/t/php-read-a-dir-into-an-array/182669/2 "2006-03-20T23:55:35Z")

</div>

$file is filled with names of files.

to display them

change this:

```php
"<a href='$file'>$file</a> extension: ". showExtension($file) . " filesize: ". fsize($file) ." permissions: " . fileperms($file) . "<br />";

```

into this:

```php
echo "<a href='$file'>$file</a> extension: ". showExtension($file) . " filesize: ". fsize($file) ." permissions: " . fileperms($file) . "<br />";

```

---

<div class="post-metadata">

**Author:** ![jw06](https://avatars.discourse-cdn.com/v4/letter/j/838e76/32.png) [@jw06](https://forum.kirupa.com/u/jw06)\
**Post date:** [March 21, 2006, 4:36pm UTC](https://forum.kirupa.com/t/php-read-a-dir-into-an-array/182669/3 "2006-03-21T16:36:19Z")

</div>

> [@Voccart](#):
>
> $file is filled with names of files.
> 
> to display them
> 
> change this:
> 
> ```php
> "<a href='$file'>$file</a> extension: ". showExtension($file) . " filesize: ". fsize($file) ." permissions: " . fileperms($file) . "<br />";
> 
> ```
> 
> into this:
> 
> ```php
> echo "<a href='$file'>$file</a> extension: ". showExtension($file) . " filesize: ". fsize($file) ." permissions: " . fileperms($file) . "<br />";
> 
> ```

Ok, sorry, when I was editing the code for the forum, I messed it up. I had echo in there, but echo only echoes that 1 file name. I want the whole directory listing to be placed into an array.
