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 7 of 7
  1. #1
    New Coder
    Join Date
    Jul 2008
    Posts
    57
    Thanks
    0
    Thanked 0 Times in 0 Posts

    copy one folder into another folder using php

    hi,

    i want to copy the contents of one folder into another folder.For that i create the one new folder . I copied the files of already existing folder into new folder.For that i write a code as

    PHP Code:
    <?php
    mkdir
    ("filesscreate",0777);
    copy('admin','filescreate');
    ?>
    But in that new folder 'filescreate' created. and files in the admin folder are not copied.plz tell that what's the problem in my code.

  • #2
    Master Coder
    Join Date
    Jun 2003
    Location
    Cottage Grove, Minnesota
    Posts
    9,502
    Thanks
    8
    Thanked 1,089 Times in 1,080 Posts
    Are you copying (as in having them in two places), or is your
    intention that you want to "move" them to another directory?

  • #3
    New Coder
    Join Date
    Jul 2008
    Posts
    57
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by mlseim View Post
    Are you copying (as in having them in two places), or is your
    intention that you want to "move" them to another directory?
    i want to copy the files

  • #4
    New to the CF scene
    Join Date
    Jul 2008
    Posts
    8
    Thanks
    0
    Thanked 1 Time in 1 Post
    Is it an intention that in the mkdir() function you have the name of file created wit double 's' (filesscreate) while in the copy() function filescreate is with one 's' only?

  • #5
    Master Coder
    Join Date
    Jun 2003
    Location
    Cottage Grove, Minnesota
    Posts
    9,502
    Thanks
    8
    Thanked 1,089 Times in 1,080 Posts
    copy() is for files only, not directories ...

    So, as in the code below, you make a directory (if it does not exist),
    then set permissions so you can copy the files. Loop through the
    files only and copy them.

    Some assumptions:

    The "source" directory has the permission to read from it.

    The "destination" directory (the paths to it) have permission to write.
    example, in the script shown, "filescreate" is to be created in the same
    root directory where "admin" resides. If you wish to put "filescreate"
    inside of another directory, like "includes/filescreate" ... you would have
    to make sure the directory called "includes" has write permission.

    The script is non-recursive, meaning it will copy files in one directory ...
    it won't spider through any sub-directories.

    PHP Code:
    <?php
    copydir
    ("admin","filescreate");
    echo 
    "done";

    function 
    copydir($source,$destination)
    {
    if(!
    is_dir($destination)){
    $oldumask umask(0); 
    mkdir($destination01777); // so you get the sticky bit set 
    umask($oldumask);
    }
    $dir_handle = @opendir($source) or die("Unable to open");
    while (
    $file readdir($dir_handle)) 
    {
    if(
    $file!="." && $file!=".." && !is_dir("$source/$file"))
    copy("$source/$file","$destination/$file");
    }
    closedir($dir_handle);
    }
    ?>

  • #6
    New to the CF scene
    Join Date
    Mar 2012
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by mlseim View Post
    copy() is for files only, not directories ...

    So, as in the code below, you make a directory (if it does not exist),
    then set permissions so you can copy the files. Loop through the
    files only and copy them.

    Some assumptions:

    The "source" directory has the permission to read from it.

    The "destination" directory (the paths to it) have permission to write.
    example, in the script shown, "filescreate" is to be created in the same
    root directory where "admin" resides. If you wish to put "filescreate"
    inside of another directory, like "includes/filescreate" ... you would have
    to make sure the directory called "includes" has write permission.

    The script is non-recursive, meaning it will copy files in one directory ...
    it won't spider through any sub-directories.

    PHP Code:
    <?php
    copydir
    ("admin","filescreate");
    echo 
    "done";

    function 
    copydir($source,$destination)
    {
    if(!
    is_dir($destination)){
    $oldumask umask(0); 
    mkdir($destination01777); // so you get the sticky bit set 
    umask($oldumask);
    }
    $dir_handle = @opendir($source) or die("Unable to open");
    while (
    $file readdir($dir_handle)) 
    {
    if(
    $file!="." && $file!=".." && !is_dir("$source/$file"))
    copy("$source/$file","$destination/$file");
    }
    closedir($dir_handle);
    }
    ?>
    with little code below your code will be usefull for copy folder with subfolder inside
    PHP Code:
    <?php
    copydir
    ("admin","filescreate");
    echo 
    "done";

    function 
    copydir($source,$destination)
    {
    if(!
    is_dir($destination)){
    $oldumask umask(0); 
    mkdir($destination01777); // so you get the sticky bit set 
    umask($oldumask);
    }
    $dir_handle = @opendir($source) or die("Unable to open");
    while (
    $file readdir($dir_handle)) 
    {
    if(
    $file!="." && $file!=".." && !is_dir("$source/$file")) //if it is file
    copy("$source/$file","$destination/$file");
    if(
    $file!="." && $file!=".." && is_dir("$source/$file")) //if it is folder
    copydir("$source/$file","$destination/$file");
    }
    closedir($dir_handle);
    }
    ?>
    i just add two lines
    PHP Code:
    if($file!="." && $file!=".." && is_dir("$source/$file")) //if it is folder
    copydir("$source/$file","$destination/$file"); 
    Thank you anyway your code really help me

  • #7
    Regular Coder
    Join Date
    Jan 2009
    Posts
    204
    Thanks
    29
    Thanked 0 Times in 0 Posts
    Can the following be altered to select a random file...


    Quote Originally Posted by mlseim View Post
    copy() is for files only, not directories ...

    So, as in the code below, you make a directory (if it does not exist),
    then set permissions so you can copy the files. Loop through the
    files only and copy them.

    Some assumptions:

    The "source" directory has the permission to read from it.

    The "destination" directory (the paths to it) have permission to write.
    example, in the script shown, "filescreate" is to be created in the same
    root directory where "admin" resides. If you wish to put "filescreate"
    inside of another directory, like "includes/filescreate" ... you would have
    to make sure the directory called "includes" has write permission.

    The script is non-recursive, meaning it will copy files in one directory ...
    it won't spider through any sub-directories.

    PHP Code:
    <?php
    copydir
    ("admin","filescreate");
    echo 
    "done";

    function 
    copydir($source,$destination)
    {
    if(!
    is_dir($destination)){
    $oldumask umask(0); 
    mkdir($destination01777); // so you get the sticky bit set 
    umask($oldumask);
    }
    $dir_handle = @opendir($source) or die("Unable to open");
    while (
    $file readdir($dir_handle)) 
    {
    if(
    $file!="." && $file!=".." && !is_dir("$source/$file"))
    copy("$source/$file","$destination/$file");
    }
    closedir($dir_handle);
    }
    ?>


  •  

    Posting Permissions

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