Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

PHP Forum


You are currently viewing our PHP Forum as a guest. Please register to participate.
Login



Freelance Jobs

Reply
how to get all file names/location from a dir tree...
Old 12-09-2007, 03:24 PM how to get all file names/location from a dir tree...
dansgalaxy's Avatar
Defies a Status

Posts: 6,521
Name: Dan
Location: Swindon
Trades: 0
okay, this may sound weird but how could i write a script which will add every file and its path from the root into a database.

so i will have a entry for each file like?

Thanks,
Dan
__________________
Discounted Web Hosting With XDnet!
>> Get 25% of hosting~ Promo: Webmaster-talk <<

Please login or register to view this content. Registration is FREE
dansgalaxy is offline
Reply With Quote
View Public Profile Visit dansgalaxy's homepage!
 
 
Register now for full access!
Old 12-09-2007, 05:39 PM Re: how to get all file names/location from a dir tree...
lizciz's Avatar
Super Spam Talker

Posts: 807
Name: Mattias Nordahl
Location: Sweden
Trades: 0
I've written a function which kind of does that. It was some time ago I wrote it and I can't guarantee it's working exactly as it should, as it was part of an never finished project :P Here's the code

PHP Code:
    public function getImagesInDir($dir '')
    
// Returns an array with the names of all images in the directory $dir,
    // and also all directives within it.
    
{
        
$errors = array();
                
        
// Hämtar alla bilder från databasen som stämmer in på sökvägen
        
$dbImagePaths $this->getImagePathsFromDb($dir);
        
array_merge($errors$this->errors);
        
        
// Håller reda på endast sökvägen för alla filer som gås igenom (ej kataloger)
        
$imagePaths = array();
    
        
$allowedExtensions = array('jpg''gif''png');
        
$allImages = array();
        if(
preg_match("/^\//"$dir)) {
            
$dir substr($dir1strlen($dir)-1);
        }
        
$realDir REAL_ROOT.IMAGE_PATH.$dir;
        if(
is_dir($realDir)) {
            if(
$dh opendir($realDir)) {
                while((
$file readdir($dh)) !== false) {
                    if(
$file != "." && $file != "..") {
                        if(
filetype($file) == 'dir') {
                            
$allImages[] = array('filetype' => 'dir''dirName' => $file'fullPath' => $dir.$file,
                                                 
'content' => $this->getImagesInDir($dir.$file));
                        } elseif(
in_array($this->getExtension($file), $allowedExtensions)) {
                            
$allImages[] = $this->getImageSpec($dir.$file);
                            
$imagePaths[basename($dir.$file)] = '1';
                        } 
// if
                    
// if
                
// while
            
} else {
                
$errors[] = "Kunde inte öppna direktivet <span class=\"italic\">$dir</span>";
            } 
// if
        
} else {
            
$errors[] = "<span class=\"italic\">$dir</span> &auml;r inget giltligt direktiv";
        } 
// if
        
        // Nu jämförs alla filer hämtade från databasen med de som fanns "på riktigt" för att
        // se så att databasen inte refererar till bilder som inte finns eller det finns bilder
        // som inte är registrerade i databasen.
        
        
$notInDb = array();
        
$notInDir = array();
        foreach(
$dbImagePaths as $key => $path) {
            if(
array_key_exists($path$imagePaths)) {
                unset(
$dbImagePaths[$key], $imagePaths[$path]);
            } else {
                
$notInDir[] = $path;
            }
        }
        if(!empty(
$imagePaths)) {
            foreach(
$imagePaths as $path => $val) {
                
$notInDb[] = $path;
            }
        }

        return 
$allImages;
    } 
Please observe that most explaining texts are in Swedish (and some aren't correct evan), if you absolutely can't understand anything just tell me and I'll make a translation. What the function does is finds all images in the specified directive, and also all other directives in it and calls itself with that directive. It is all saved and return as an array.

It also gets the equialent inforamtion from a database to compare it, to check that no images exists but aren't in the database or that some that doesn't exists is. You can probably take that part out.

Oh yeah, it may be a bit confusing with the dir variables. I made it so that virtually the 'root' would be some folder like 'images' while it acctually was something like '/the_real_root/some_folder/images/'. So instead of writing the complete path, lets say '/the_real_root/some_folder/images/christmas/pic01.jpg', you would write 'christmas/pic01.jpg'. The short dirs is the ones beeing returned in the array while the function is working with the real paths.

And as you can see it is a function which is part of a class, so there is probably some things you may want to change.

Hope you find it useful!
lizciz is offline
Reply With Quote
View Public Profile Visit lizciz's homepage!
 
Old 12-10-2007, 02:09 AM Re: how to get all file names/location from a dir tree...
NullPointer's Avatar
Will Code for Food

Posts: 2,815
Name: Matt
Location: Irvine, CA
Trades: 0
I can't give you the code off the top of my head, but I can bust out some psudocode. You'll want to use recursion rather than an explicit loop (in my opinion).

Code:
function visitAll($directory)
{
     //base case
     if there are no more subdirectories
     {
           for each file in this directory
           {
                  add path to database;
                  return;
           }
     }
     else
     {
            for each subdirectory
            {
                   visitAll($subdirectory);
                   for each file in this directory
                   {
                          add path to database;
                   }
                    return;
           }
     }
}

That's just how I would approach the problem, but then again I'm a Java programmer and the way I look at things is a bit different from what may be the PHP norm. Hope that helps.

btw. That's more or less a generic depth first tree traversal.
__________________

Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
NullPointer is online now
Reply With Quote
View Public Profile Visit NullPointer's homepage!
 
Old 12-10-2007, 12:31 PM Re: how to get all file names/location from a dir tree...
dansgalaxy's Avatar
Defies a Status

Posts: 6,521
Name: Dan
Location: Swindon
Trades: 0
:s ok, that sounds like its basically what i want, a script which goes throguh and if files not in db add them.

i will try to explain a bit more what i want it for, okay so i basically would like to get a kind of file check script, which just runs through and shows status of all files, and this would be the crawler part, which goes through all the files, checks the ones in db exist checks are readble, checks write permissions, gets file size, basically a whole load of info and then i can have a file check which shows all files, all the info about them and then can show missing or errors with major files, and incorrect write permissions.

from what i get from your desc your script is almost what i need, all it would need is allow all extensions and not jsut the ones in the array.

but im stupid so im confused on how to go about this :s
__________________
Discounted Web Hosting With XDnet!
>> Get 25% of hosting~ Promo: Webmaster-talk <<

Please login or register to view this content. Registration is FREE
dansgalaxy is offline
Reply With Quote
View Public Profile Visit dansgalaxy's homepage!
 
Old 12-12-2007, 06:05 PM Re: how to get all file names/location from a dir tree...
lizciz's Avatar
Super Spam Talker

Posts: 807
Name: Mattias Nordahl
Location: Sweden
Trades: 0
I've modified the script a bit and made some translations. You need to write the functions getFilePathsFromFB() and getFileSpec(). I also noticed that it doesn't do anything with the variables $notInDB and $notInDir. You can return them together with $allFiles in an array or save them as a class variable, or any other way you want.

To make it find all files instead of just images of certain type I simply replaced the part with elseif("is of the right file extension") with else.

PHP Code:
    public function getFilesInDir($dir '')
    
// Returns an array with the names of all images in the directory $dir,
    // and also all directives within it.
    
{
        
$errors = array();
                
        
// Get all images from database which is in the specified directory
        
$dbFilePaths $this->getFilePathsFromDb($dir);
        
array_merge($errors$this->errors);
        
        
// Holds the path for all images (not directories)
        
$filePaths = array();
    
        
// The array to be returned. Holds all files and dirctories
        
$allFiles = array();

        if(
preg_match("/^\//"$dir)) {
            
$dir substr($dir1strlen($dir)-1);
        }
        
$realDir REAL_ROOT.IMAGE_PATH.$dir;
        if(
is_dir($realDir)) {
            if(
$dh opendir($realDir)) {
                while((
$file readdir($dh)) !== false) {
                    if(
$file != "." && $file != "..") {
                        if(
filetype($file) == 'dir') {
                            
$allFiles[] = array('filetype' => 'dir''dirName' => $file'fullPath' => $dir.$file,
                                                 
'content' => $this->getFilesInDir($dir.$file));
                        } else {
                            
$allFiles[] = $this->getFileSpec($dir.$file);
                            
$filePaths[basename($dir.$file)] = '1';
                        } 
// if
                    
// if
                
// while
            
} else {
                
$errors[] = "Could nopt open the directive <span class=\"italic\">$dir</span>";
            } 
// if
        
} else {
            
$errors[] = "<span class=\"italic\">$dir</span> is not a valid directive";
        } 
// if
        
        // Compare all files from the database with the real ones in order
        // to check if any files from the database acctually doesn't exists
        // or if some existsing ones is not in the database
        
        
$notInDb = array();
        
$notInDir = array();
        foreach(
$dbFilePaths as $key => $path) {
            if(
array_key_exists($path$filePaths)) {
                unset(
$dbFilePaths[$key], $filePaths[$path]);
            } else {
                
$notInDir[] = $path;
            }
        }
        if(!empty(
$filePaths)) {
            foreach(
$filePaths as $path => $val) {
                
$notInDb[] = $path;
            }
        }

        return 
$allFiles;
    } 
lizciz is offline
Reply With Quote
View Public Profile Visit lizciz's homepage!
 
Old 12-14-2007, 12:11 PM Re: how to get all file names/location from a dir tree...
dansgalaxy's Avatar
Defies a Status

Posts: 6,521
Name: Dan
Location: Swindon
Trades: 0
Okay sorry i really am being thick here, i tried this and im getting Fatal error: Using $this when not in object context

what else do i need to do to get this working? i have never got my head around classes
__________________
Discounted Web Hosting With XDnet!
>> Get 25% of hosting~ Promo: Webmaster-talk <<

Please login or register to view this content. Registration is FREE
dansgalaxy is offline
Reply With Quote
View Public Profile Visit dansgalaxy's homepage!
 
Old 12-14-2007, 01:47 PM Re: how to get all file names/location from a dir tree...
Extreme Talker

Posts: 238
Location: United States
Trades: 0
Quote:
Originally Posted by dansgalaxy View Post
Okay sorry i really am being thick here, i tried this and im getting Fatal error: Using $this when not in object context

what else do i need to do to get this working? i have never got my head around classes
$this is used to reference methods and attributes in the same class. If you aren't using classes, then you can just remove the $this-> part.
__________________
The interlocking pieces of web development: usability, performance, accessibility, and standards.
frost is offline
Reply With Quote
View Public Profile
 
Reply     « Reply to how to get all file names/location from a dir tree...
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

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

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





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.19856 seconds with 12 queries