TalkPHP
 
 
Account Login
Latest Articles
» The basic usage of PHPTAL, a XML/XHTML template library for PHP
» Vulnerable methods and the areas they are commonly trusted in.
» Simple way to protect a form from bot
» The Basics On: How Session Stealing Works
» How to keep your forms from double posting data
IRC Channel
IRC Speech Bubble Join the friendly bunch on IRC...
(#TalkPHP on Freenode)

...Also available via a web interface.

See this thread for information on the TalkPHP Free Hugs Initiative™. Subject to availability.
Associates
Associates
CSS Tutorials
Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old 10-09-2010, 12:20 PM   #1 (permalink)
The Wanderer
 
Join Date: Oct 2010
Posts: 9
Thanks: 2
medvirus is on a distinguished road
Default folder scan

Hello
i have to scan file in folder and other file in subfolder also , list in one page. All file display in one page.
e.g
folderfiles
1.jpg
2.jpg
etc
And
folder/subfolder file
12.jpg
13.jpg
etc
so can anyone please guide me.
medvirus is offline  
Reply With Quote
Old 10-09-2010, 07:31 PM   #2 (permalink)
The Wanderer
 
Join Date: Oct 2010
Posts: 9
Thanks: 2
medvirus is on a distinguished road
Default

hi
i have this script
can you tell me why it not list all file just list one folder and one file.

Script is here
PHP Code:
function dirs($dir){
  
// set filenames invisible if you want
    
$invisibleFileNames = array(".""..""items.txt""index.php");
    
// run through content of root directory
    
$sfile scandir($dir);
    foreach(
$sfile as $key) {
        
// filter all files not accessible
        //$path = $rootDir.'/'.$content;
        
if(!in_array($key$invisibleFileNames)) {
            
// if content is file & readable, add to array
            
if(is_file($key) && is_readable($key)) {
                
// save file name with path
                
$allData['file'] = $key;

            
// if content is a directory and readable, add path and name
            
}elseif(is_dir($key) && is_readable($key)) {
                
// recursive callback to open new directory
                
$allData['dir'] = $key;

         }
        }
    }
    return 
$allData;


}

$my2=dirs('.');


foreach (
$my2 as $value) {
   echo 
$value;

   } 
i want to list seperate directory and seperate file
like
Directory Name:
new folder
newfolder2
FIle Name :
1.jph
2.jpg
Hope SOmeBody will help
medvirus is offline  
Reply With Quote
Old 10-11-2010, 02:19 AM   #3 (permalink)
Only Human
 
Join Date: Oct 2010
Posts: 5
Thanks: 0
GSmith is on a distinguished road
Default

SPL is typically the way to go for new development, but that's not where your problem is. I will present both solutions.

Here are the problem areas:
PHP Code:
$allData['file'] = $key;
...
$allData['dir'] = $key
Your code is not pushing new values onto the $allData['file'] and $allData['dir'] arrays, and is instead overwriting previously stored values on each iteration.

Here's a solution using your method.. excuse me for renaming a couple of variables I believe were inappropriately named.
PHP Code:
function dirs($path)
{
    
$nodes = array('file' => array(), 'dir' => array());
    
$hidden = array('.''..''items.txt''index.php');
    foreach (
scandir($path) as $file)
    {
        if (
in_array($file$hidden))
            continue;
        if (
is_file($file) && is_readable($file))
            
array_push($nodes['file'], $file);
        elseif (
is_dir($file) && is_readable($file))
            
array_push($nodes['dir'], $file);
    }

    return 
$nodes;

Here's a solution using SPL's DirectoryIterator. There are so many similarities it's really up to you - I've found that if you don't particularly need the extra functionality of SPL objects, it is often quicker to instead use functions found in the first solution.
PHP Code:
function dirs($path)
{
    
$nodes = array('file' => array(), 'dir' => array());
    
$hidden = array('.''..''items.txt''index.php');
    foreach (new 
DirectoryIterator($path) as $node)
    {
        
$filename $node->getFilename();
        if (
in_array($filename$hidden))
            continue;
        if (
$node->isFile() && $node->isReadable())
            
array_push($nodes['file'], $filename);
        elseif (
$node->isDir() && $node->isReadable())
            
array_push($nodes['dir'], $filename);
    }

    return 
$nodes;

See: SPL, DirectoryIterator

Also, your output loop must be designed to handle your 2-dimensional array. Here's an example, although concise and particularly obfuscated.
PHP Code:
$nodes dirs('.');
echo 
'Files:'PHP_EOL'  'implode(PHP_EOL '  '$nodes['file']), PHP_EOLPHP_EOL;
echo 
'Directories:'PHP_EOL'  'implode(PHP_EOL '  '$nodes['dir']), PHP_EOL
Output:
Code:
Files:
  prog.php

Directories:
Good luck! Cheers.
GSmith is offline  
Reply With Quote
The Following 2 Users Say Thank You to GSmith For This Useful Post:
medvirus (10-11-2010), Salathe (10-11-2010)
Old 10-11-2010, 03:49 AM   #4 (permalink)
The Wanderer
 
Join Date: Oct 2010
Posts: 9
Thanks: 2
medvirus is on a distinguished road
Default

nice and thanx for reply bro
but plz tell me how what change i have to do for sub folder file listing also in your script
you know what i really want to is
e.g
Folder
Files:
Directory:
Subfolder1
Files:
Directory:
Subfolder2
Files:
Directory:
Subfolder3
Files:
Directory:


SOmewhat recruit directory listing
but in i wan to seperate file and directory
all file and folder should list in with their files in one page.
medvirus is offline  
Reply With Quote
Old 10-11-2010, 07:57 PM   #5 (permalink)
Only Human
 
Join Date: Oct 2010
Posts: 5
Thanks: 0
GSmith is on a distinguished road
Default

I'm not entirely sure what you mean, but here's a quick mock up of a modified function using SPL which will recurse through subdirectories, build and return an output string with padding for recursion depth. I'm at work right now, so cut me some slack if it produces unexpected results!
PHP Code:
function dirs($path)
{
    
settype($path'string');
    if (!
is_dir($path) || !is_readable($path))
        return 
FALSE;

    
$output $path PHP_EOL;
    
$padding 2;

    
$it = new RecursiveIteratorIterator(
        new 
RecursiveDirectoryIterator($path),
        
RecursiveIteratorIterator::SELF_FIRST);
    foreach (
$it as $node)
    {
        if (
$it->isDot())
            continue;
        if (!
$node->isReadable())
            continue;
        if (!
in_array($node->getType(), array('dir''file')))
            continue;

        
/*
         * Define padding depending on recursion depth and
         * strip out $path and any leading directory separators
         * from the pathname, appending to $output.
         */
        
$output .= sprintf('% ' . ($it->getDepth() + 1) * $padding 's''') .
            
ltrim(substr_replace($node''0strlen($path)),
                
DIRECTORY_SEPARATOR);

        if (
$node->isDir())
            
$output .= DIRECTORY_SEPARATOR;
        
$output .= PHP_EOL;
    }

    return 
$output;
}

echo 
dirs('.'); 
Output
Code:
.
  directoryiterator.php
  baz/
    baz/baq/
      baz/baq/baz
      baz/baq/bar
      baz/baq/foo
  bar
  foo
  baq/
The filesystem returns files in the order they are stored on disk, so an array is probably more appropriate because we can easily sort it; I'll try and provide an update later today.

RecursiveIteratorIterator and RecursiveDirectoryIterator are the workhorses here. There's also a bit of string manipulation in order to produce that particular output, and you can also define the amount of padding per recursion depth by modifying the $padding variable. The constants PHP_EOL (new line) and DIRECTORY_SEPARATOR (/ or \) are platform-independent and defined by PHP at run-time.

If this is a web application you'll have to insert HTML line breaks by each PHP_EOL and replace leading spaces with   to see the formatting using an text/html MIME type. For example:
PHP Code:
echo nl2br(str_replace(' '' 'dirs('.'))); 
I hope I haven't misunderstood your question.

Cheers.
GSmith is offline  
Reply With Quote
The Following User Says Thank You to GSmith For This Useful Post:
medvirus (10-11-2010)
Old 10-11-2010, 08:35 PM   #6 (permalink)
The Wanderer
 
Join Date: Oct 2010
Posts: 9
Thanks: 2
medvirus is on a distinguished road
Default

Nice work and thanks ++
bro i understand my query 100% and solve my problem
actually what i want to make form your script is like this , which i make from below script
PHP Code:
<?php
function dirlist($dir$bool "dirs"){
   
$truedir $dir;
   
$dir scandir($dir);
   if(
$bool == "files"){ // dynamic function based on second pram
      
$direct 'is_dir';
   }elseif(
$bool == "dirs"){
      
$direct 'is_file';
   }
   foreach(
$dir as $k => $v){
      if((
$direct($truedir.$dir[$k])) || $dir[$k] == '.' || $dir[$k] == '..' || $dir[$k] == 'my.php' ){
         unset(
$dir[$k]);
      }
   }
   
$dir array_values($dir);
   return 
$dir;
}
?>
<table align=center width=100%>

<?php
echo "<tr bgcolor=#990000><td>";
$dop="D:\/family_picture/4dham/2010/";
echo 
"<b><font face=\"BN Machine\" color=\"#FFFFFF\">File OF $dop</font></b></td></tr>";
//main dir file
$filem=dirlist("$dop""files");

while(list(
$kf,$vf)=each($filem))
                                {
$kf=$kf+1;
                               echo 
"<tr bgcolor=#FFCC66><td align=center><b><font color=\"#000066\"> $kf:$vf</font></b></td></tr> " ;
                                }




//main dir
$dirl=dirlist("$dop");
while(list(
$k,$v)=each($dirl))
                                { 
$k=$k+1;
                                  echo 
"<tr bgcolor=#FF6600><td align=right><b><font face=\"Martina\"color=\"#000000\" ><h1>Folder OF $dop</h1></font></b></td></tr> " ;
                                  echo 
"<tr bgcolor=#006699><td align=center><b><font color=\"#000000\"> <h1><i>$k</i>->$v</h1></font></b></td></tr> " ;


                                  
//Sub Dir Files
                                
$fils=dirlist("$dop/$v/""files");
                                 while(list(
$k1,$v1)=each($fils))
                                {
$k1=$k1+1;
                                 echo 
"<tr bgcolor=#FFCC66><td align=center><b><font color=\"#CC0000\"><i> $k1</i>->$v1</font></b></td></tr> " ;

                                }
                                 
//Subsub Dir
                                 
$fild=dirlist("$dop/$v/");
                                 while(list(
$k11,$v11)=each($fild))
                                {  
$k11=$k11+1;
                                
//echo "<tr bgcolor=#00FF99><td align=left><b><font color=\"#000000\">Folder of $k:$v</font></b></td></tr> " ;
                                   
echo "<tr bgcolor=#FF99CC><td align=left><b><font face=\"Autumn\" color=\"#000000\"> <h2><i>$k11</i>->$v11 th $v</h2></font></b></td></tr> " ;

                                 
//SUbSUb dirfiles
                                
$fils1=dirlist("$dop/$v/$v11/""files");
                                 while(list(
$k111,$v111)=each($fils1))
                                {  
$k111=$k111+1;
                                  echo 
"<tr bgcolor=#660000><td align=center><b><font color=\"#FFFFFF\"> <i>$k111</i>->$v111</font></b></td></tr> " ;


                                }

                                }

                                }

?>
</table>
just check and tell me that how i use your script instead of this becouse this script has one drawback is
i have to manually add while loop in it , and it make script big,
i wan to short this script as much as possible...

My main aim is , i have to scan file from all folder and move them to one folder.
medvirus is offline  
Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Similar Threads
Thread Thread Starter Forum Replies Last Post
How to create a gallery class Tanax Advanced PHP Programming 25 02-19-2013 04:25 AM
Determine if this folder is the last level within that folder? Peuplarchie General 1 03-01-2010 04:27 AM
Check if specific folder contains files, folders, both or none ? Peuplarchie General 1 02-27-2010 04:36 AM
Redirecting site to temporary folder Jmz General 6 10-09-2009 07:49 AM
Scan dir, seperate file by name convension... Peuplarchie Advanced PHP Programming 5 06-09-2009 07:38 PM


All times are GMT. The time now is 12:22 PM.

 
     

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Inactive Reminders By Icora Web Design