Status
Not open for further replies.

Porsche_maniak

Active Member
283
2009
0
40
I am not good with php and will ask for help...

* The folder is named 'fav'
* There are subfolders in 'fav'
* Each subfolder may contain a .txt file

Needed -->
* Check for identical .txt file names in all subfolders
* Sort 10 most repeating identicals,show(echo) their contents and show(echo) times repeated (in brackets such as this one :D )

And.. that's it !
I hope you'll code it easily for me..

THANKS !
 
30 comments
Well this would have to be done recursively.

PHP:
$storage = array();
function ScanForTextFiles($folder)
{
    global $storage;
    $resource = opendir($folder);
    while(false != ($file = readdir($resource)))
    {
         $current = $folder . $file;
         if($file != '.' || $file != '..' && is_dir( $current )) //Changed to file from current
         {
               ScanForTextFiles($current);
         }elseif(is_file($current) && substr($current,-3) == 'txt') //Updated to check format
         {
            if(!isset($storage[$file]))
            {
                 $storage[$file] = 0; //Start with 0 counts
            }else
            {
               $storage[$file]++; //Add 1 more count
            }
        }
    }
}
ScanForTextFiles('path/to/fav/'); //WITH LAST SLASH
//Then just sort $storage to print how you want it..

Code is untested.
 
I tried everything and gives errors.
Also i saw that it finds files in fav folder which are outside it (one level down).

Btw the folder is just 'fav/' and i want to be sorted in numeric and show the most 10..

opendir(fav/..login.php) [function.opendir]: failed to open dir: No such file or directory in
 
error without last slash

Code:
Warning: opendir(fav....................................................................................................................................................................................................................................New Text Document.txt) [function.opendir]: failed to open dir: No such file or directory in C:\xampp\htdocs\downloads\stats.php on line 219

Warning: readdir() expects parameter 1 to be resource, boolean given in C:\xampp\htdocs\downloads\stats.php on line 220

Warning: opendir(fav.......................................................................................................................................................................................................................................) [function.opendir]: failed to open dir: No such file or directory in C:\xampp\htdocs\downloads\stats.php on line 219

Error with last slash
Code:
Warning: opendir(fav/..options_cgi.php) [function.opendir]: failed to open dir: No such file or directory in C:\xampp\htdocs\downloads\stats.php on line 219

Warning: readdir() expects parameter 1 to be resource, boolean given in C:\xampp\htdocs\downloads\stats.php on line 220

Warning: opendir(fav/..partner1.html) [function.opendir]: failed to open dir: No such file or directory in C:\xampp\htdocs\downloads\stats.php on line 219

Warning: readdir() expects parameter 1 to be resource, boolean given in C:\xampp\htdocs\downloads\stats.php on line 220

Warning: opendir(fav/..people.html) [function.opendir]: failed to open dir: No such file or directory in C:\xampp\htdocs\downloads\stats.php on line 219
- This one shows files which are outside the folder...
 
PHP:
<?php
function RecurseFolderLister($d)
{
	$BasePath = realpath($d);
	$Storage = array();
	
	if(is_dir($BasePath))
	{
		$resource = opendir($BasePath);
		while(false !== ($directory = readdir($resource)))
		{
			if(is_dir($BasePath . '/' . $directory) && !in_array($directory,array('.','..'))) //DirCheck the TypeCheck
			{
				$Storage = array_merge($Storage,RecurseFolderLister($BasePath . '/' . $directory));
			}else
			{
				if(is_file($BasePath . '/' . $directory) && substr($directory,-3) == 'txt')
				{
					//We have text file
					$key = basename($BasePath . '/' . $directory);
					
					if(!isset($Storage[$key]))
					{
						$Storage[$key] = 0;
					}
					$Storage[$key]++;
				}
			}
		}
	}
	return $Storage;
}
$Storage = RecurseFolderLister('fav');
asort($Storage); //Sort them
var_dump($Storage);
?>

Sorted!
 
So far so good...
I used :
Code:
$entry_arrayz[ 'entry' ]  .= $Storage = RecurseFolderLister('fav'); asort($Storage); var_dump($Storage);

where $entry_arrayz[ 'entry' ] is the place where sorted $storage will show,but i get :

Code:
array(1) { ["entry100620-034822.txt"]=> int(1) } 

Array

How to print that thing ?

Also in the following case i've put entry100620-034822.txt in 2 different folders (which means entry100620-034822.txt is repeating 2 times) and a.txt (just 1)

Code:
array(2) { ["a.txt"]=> int(1) ["entry100620-034822.txt"]=> int(1) }

Array

So another question - Is there "* Sort 10 most repeating identicals,show(echo) their contents and show(echo) times repeated (in brackets such as this one )" in your code ?

I am really bad in PHP so sorry that i am asking such a stupid questions.
 
hmm,

Just do

PHP:
$entry_arrayz[ 'entry' ] = RecurseFolderLister('fav');

var_dump($entry_arrayz['entry']);

Tell me you get and what you SHOULD get :/

And this code will do the following

scann all directories within the directory you specify, load all files that are .txt files into an array, and count how many times it came accross that file

The output would be like so

Code:
Array(
    'Name' > Count
    'Name' > Count
    'Name' > Count
    'Name' > Count
)

Like
Code:
Array(
    'readme.txt' > 12
    'about.txt' > 3
    'test.txt' > 1
    'readmeAgain.txt' > 1
   
)
 
I've tried
Code:
$entry_arrayz[ 'entry' ] = RecurseFolderLister('fav');

var_dump($entry_arrayz['entry']);

but it's the same...

Btw read my prev post if haven't (i edited it)..
 
PHP:
<?php
function RecurseFolderLister($d)
{
    $BasePath = realpath($d);
    $Storage = array();
    
    if(is_dir($BasePath))
    {
        $resource = opendir($BasePath);
        while(false !== ($directory = readdir($resource)))
        {
            if(is_dir($BasePath . '/' . $directory) && !in_array($directory,array('.','..'))) //DirCheck the TypeCheck
            {
                $Storage = array_merge($Storage,RecurseFolderLister($BasePath . '/' . $directory));
            }else
            {
                if(is_file($BasePath . '/' . $directory) && substr($directory,-3) == 'txt')
                {
                    //We have text file
                    $key = $BasePath . '/' . $directory);
                    
                    if(!isset($Storage[$key]))
                    {
                        $Storage[$key] = 0;
                    }
                    $Storage[$key]++;
                }
            }
        }
    }
    return $Storage;
}
$Storage = RecurseFolderLister('fav');
asort($Storage);
$i = 0;
foreach( array_reverse($Storage) as $filename => $total )
{
     echo file_get_contents($filename) . '('.$total.')'
     $i++;
     if($i == 10) break;
}
?>

Try something like that
 
How should i add

Code:
$Storage = RecurseFolderLister('fav'); 
asort($Storage); 
$i = 0; 
foreach( array_reverse($Storage) as $filename => $total ) 
{ 
     echo file_get_contents($filename) . '('.$total.')' 
     $i++; 
     if($i == 10) break; 
}

in $entry_arrayz[ 'entry' ] ?
 
Look Porch, im trying to help but i gave you bulk of the code, try figure it out first...

i aint got a clue what $entry_arrayz[ 'entry' ] is for or where its used or what its sctructure table is like.

i cant just know the answers :(
 
$entry_arrayz[ 'entry' ] is the field where everything will print.

I am using now :
PHP:
		  function RecurseFolderLister($d) 
{ 
    $BasePath = realpath($d); 
    $Storage = array(); 
     
    if(is_dir($BasePath)) 
    { 
        $resource = opendir($BasePath); 
        while(false !== ($directory = readdir($resource))) 
        { 
            if(is_dir($BasePath . '/' . $directory) && !in_array($directory,array('.','..'))) //DirCheck the TypeCheck 
            { 
                $Storage = array_merge($Storage,RecurseFolderLister($BasePath . '/' . $directory)); 
            }else 
            { 
                if(is_file($BasePath . '/' . $directory) && substr($directory,-3) == 'txt') 
                { 
                    //We have text file 
                    $key = ($BasePath . '/' . $directory); 
                     
                    if(!isset($Storage[$key])) 
                    { 
                        $Storage[$key] = 0; 
                    } 
                    $Storage[$key]++; 
                } 
            } 
        } 
    } 
    return $Storage; 
} 
$Storage = RecurseFolderLister('fav'); 
asort($Storage); 
$i = 0; 
foreach( array_reverse($Storage) as $filename => $total ) 
{ 
   $entry_arrayz[ 'entry' ]  .= file_get_contents($filename) . '('.$total.')' ;
     $i++; 
     if($i == 10) break; 
}

But if you check the page you will see TEST(1)TEST2(1)TEST(1)

I must get TEST(2) TEST2(1)

And really thanks for your help so far !
 
The txt files was empty so sorry for that..

But if i change to $filename it proving my above post...

C:\xampp\htdocs\downloads\fav\Administrator/entry100620-034822.txt(1)C:\xampp\htdocs\downloads\fav\New Folder/a.txt(1)C:\xampp\htdocs\downloads\fav\New Folder/entry100620-034822.txt(1)

The bolded ones are indentical,but it counts them as different.. must getting the contents only to one and adding (2) because they are repeating..
 
why don't you just use preg_match to count the duplicates?

i know that this is slower but as the above one doesn't work, you could try this. ;)
 
Status
Not open for further replies.
Back
Top