SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Member Zenin Albatros's Avatar
    Join Date
    May 2003
    Location
    California
    Posts
    5
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question Need help on best method for deleting/moving files.

    I have a script that when processed can either delete a huge amount of files from a given path on my server (200+), or transfer a huge amount of files from one directory into another directory, deleting the files from the first directory.

    Both processes use PHP file-system functions, and are of the following functions I have defined:

    PHP Code:
    Function for deleting a directory, and all its files underneath (since rmdir() wont delete directories with files):

    function 
    deldir($dir){
      
    $current_dir opendir($dir);
      while(
    $entryname readdir($current_dir)){
         if(
    is_dir("$dir/$entryname") and ($entryname != "." and $entryname!="..")){
           
    deldir("${dir}/${entryname}");
         }elseif(
    $entryname != "." and $entryname!=".."){
            
    unlink("${dir}/${entryname}");
         }
      }
      
    closedir($current_dir);
      
    rmdir(${dir});

    PHP Code:
    The two recursive functions for moving files to another directory:

    function 
    dotransfer($firstpath$secondpath)
    {
     if(
    is_file($firstpath)){
        
    $perms fileperms($firstpath);
       return 
    rename($firstpath$secondpath) && chmod($secondpath$perms);
     }
      else if(
    is_dir($firstpath)){
        
    dodirtransfer($firstpath$secondpath);
      }
     else{
        die(
    "Cannot copy file: $firstpath (it's neither a file nor a directory)");
      } 
    }
        
    function 
    dodirtransfer($firstpath$secondpath
    {
      
    $dir opendir($firstpath);
      while(
    $file readdir($dir)){
        if(
    $file == "." || $file == ".."){
          continue;
      }
        
    dotransfer("$firstpath/$file""$secondpath/$file");
      }
      
    closedir($dir);

    All three functions when called will perform their task as needed, but the problem is that I need to do these processes with 100+ files, and I've read numerous places that running lengthy scripts like this probably isn't the best method through a browser.

    The other method is using exec() commands, which I have very vague knowledge on how to use. I don't own my own web server, therefore there are a lot of shell commands I'm unfamiliar with how to use.

    From PHP.net I found that the following:
    PHP Code:
    exec("rm -rf /path/to/directory"); 
    Is suppose to delete a directory along with all the contents.

    This would solve my problem for the first function I have that deletes a directory, but I'm left with how to use exec() to transfer files from one location to another, essentially with how I was doing it with rename() and the recursion.

    ----------

    Any thoughts on a possible fix for my issue overall? I'm not sure if the method I'm doing it by with PHP file-system functions is all that worse off.
    Endeavors:
    vBulletin Gallery - "Your personal deviantArt"

  2. #2
    SitePoint Member Zenin Albatros's Avatar
    Join Date
    May 2003
    Location
    California
    Posts
    5
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    *bump*

    Any thoughts? ;D
    Endeavors:
    vBulletin Gallery - "Your personal deviantArt"

  3. #3
    SitePoint Member tahjah's Avatar
    Join Date
    Mar 2003
    Location
    US
    Posts
    22
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Just make sure when passing rm -rf to the system. Becareful if the user is selecting the files/directories. If not using superglobals, then make sure to initialize all your variables before using them.


    As far moving or copying:

    Move:

    mv src dest

    Copy:

    cp src dest

    Copy Dir:

    cp -r src dest

  4. #4
    SitePoint Member Zenin Albatros's Avatar
    Join Date
    May 2003
    Location
    California
    Posts
    5
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks, I'll have to try those commands when time is free.

    I'm guessing than, by your response, that using the exec() method is more efficient than if I had done it via PHP file-system functions?
    Endeavors:
    vBulletin Gallery - "Your personal deviantArt"

  5. #5
    Non-Member
    Join Date
    Jan 2003
    Posts
    5,748
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yes, it is more effecient - though also remember it is more deadly

    There is no going back... so be better to make sure the area of site is password protected, etc.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •