Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    Regular Coder
    Join Date
    Jun 2002
    Location
    Montreal, Canada
    Posts
    644
    Thanks
    0
    Thanked 0 Times in 0 Posts

    move, rename and copy folders

    Is it possible to move, rename and copy folders using PHP?

  • #2
    Regular Coder
    Join Date
    Feb 2003
    Location
    California
    Posts
    925
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I am not so sure about all that, I have heard that you can copy which means you can create a new name for the file/folder...but I would search for this thread...I read it before...but then again, I don't think you can just move stuff cause that would create some massive problems.


    Jason

  • #3
    Senior Coder missing-score's Avatar
    Join Date
    Jan 2003
    Location
    UK
    Posts
    2,194
    Thanks
    0
    Thanked 0 Times in 0 Posts
    You can copy, but I dont know if you can copy entire folders, I have never tried:

    If not:

    PHP Code:
    <?php

    function copyFF($file,$folder,$dest)
    {

    $error '';

    if(!
    is_dir($dest))
    {
          if(!
    mkdir($dest,777))
          {
              
    $error .= 'Failed to Create Directory<br />'
          }
    }

    for(
    $i 0$i count($file); $i++)
    {

    if(
    copy($folder.'/'.$file[$i],$dest.'/'.$file[$i]))
    {
          
    $error .= 'File '.$file[$i].' copied<br />';
    }
    else
    {
          
    $error .= 'Error copying '.$file[$i].'<br />';
    }
    }
    return 
    $error;
    }

    $filename = array('file1.txt','file2.txt','file3.txt');
    $foldername "folder";
    $destination "anotherfolder";

    $uploads copyFF($filename,$foldername,$destination);

    echo 
    $uploads// Prints any errors
    ?>
    I just quickly wrote this function, so there may be some errors, but that would have the effect of copying a dir and any files within it.

  • #4
    Senior Coder missing-score's Avatar
    Join Date
    Jan 2003
    Location
    UK
    Posts
    2,194
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Just an afterthought, you said you wanted to move the directory completely, you might want to add rmdir() into that function ( if it even works )

  • #5
    Regular Coder
    Join Date
    Jun 2002
    Location
    Montreal, Canada
    Posts
    644
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I was wondering if there was a built in function to do it. I'm guesssing the isn't. Thanks for the script, but I doubt it would be very usefull if there was another directory withing the directory I want to move. I guess I could write a code that maps out the filesystem below the folder I select, creates all the folders withing it, move all the files within them to the new destination (with rename()), then go delete all the folders, one at a time from the lowest point in the branch up. At this point it's not that it would be a very hard script to write, it's that I think it would use too much of the servers ressources. Thanks everyone for the help, but I think I'm just going to forget about this function for now.


  •  

    Posting Permissions

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