...

View Full Version : copy one folder into another folder using php



swethak
08-13-2008, 01:42 PM
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
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.

mlseim
08-13-2008, 01:56 PM
Are you copying (as in having them in two places), or is your
intention that you want to "move" them to another directory?

swethak
08-13-2008, 02:06 PM
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

alphapatrol
08-13-2008, 02:34 PM
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?

mlseim
08-13-2008, 03:06 PM
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
copydir("admin","filescreate");
echo "done";

function copydir($source,$destination)
{
if(!is_dir($destination)){
$oldumask = umask(0);
mkdir($destination, 01777); // 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);
}
?>

faza
03-30-2012, 08:55 AM
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
copydir("admin","filescreate");
echo "done";

function copydir($source,$destination)
{
if(!is_dir($destination)){
$oldumask = umask(0);
mkdir($destination, 01777); // 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
copydir("admin","filescreate");
echo "done";

function copydir($source,$destination)
{
if(!is_dir($destination)){
$oldumask = umask(0);
mkdir($destination, 01777); // 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


if($file!="." && $file!=".." && is_dir("$source/$file")) //if it is folder
copydir("$source/$file","$destination/$file");


Thank you anyway your code really help me :thumbsup:

Feckie
06-08-2014, 05:09 PM
Can the following be altered to select a random file...



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
copydir("admin","filescreate");
echo "done";

function copydir($source,$destination)
{
if(!is_dir($destination)){
$oldumask = umask(0);
mkdir($destination, 01777); // 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);
}
?>



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum