Jump to content

Recommended Posts

My script below enters the name of a directory and the count of its contents into an array.

 

I think I captures the information correctly.

 

However I am not sure how to get it to echo it back out.

 

<?php

$dir = "/home2/sharingi/public_html/subdomains";

$dh = opendir($dir);


while (false !== ($filename = readdir($dh)))
{
	if ($filename !== "." && $filename !== "..")
	{

		$files['name'] = $filename;

		$files['count'] = count(glob("/home2/sharingi/public_html/subdomains/" . $filename . "/filez/" . "*.mp3"));	

		echo $files['name'];
		echo '<br>';
		echo $files['count'];
		echo '<br>';

	}
}

 

Basically I am trying display a sorted list of the directories and their count.

 

Folder1 10

Folder2 6

Folder3 2

 

Thanks,

ian

Link to comment
https://forums.phpfreaks.com/topic/128107-solved-displaying-array-contents/
Share on other sites

You're not storing the information correctly. The way you're doing it, you're overwriting the same entry every time.

 

Try it this way:

<?php
$dir = "/home2/sharingi/public_html/subdomains";
$dh = opendir($dir);
while (false !== ($filename = readdir($dh)))
{
	if ($filename !== "." && $filename !== "..")
	{
		$files[$filename] = count(glob("/home2/sharingi/public_html/subdomains/" . $filename . "/filez/" . "*.mp3"));	
	}
}
        ksort($files);
        foreach($files as $file => $count)
               echo $file . ': ' . $count . "<br>\n";
?>

 

Ken

After I cleaned up some quotes...

 

<?php
   $dir = "/home2/sharingi/public_html/subdomains";
   $dh = opendir($dir);
   while (false !== ($filename = readdir($dh)))
   {
      if ($filename !== "." && $filename !== "..")
      {
         $files['$filename'] = count(glob("/home2/sharingi/public_html/subdomains/" . $filename . "/filez/" . "*.mp3"));   
      }
   }
        ksort($files);
        
	foreach($files as $file => $count) echo $file . ":"  . $count . "<br>";
?>

 

Returns

 

 

 

$filename:2

 

 

 

Thats all.

?

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.