note 44540 added to function.copy
| From: | m dot soto at digitalsenses dot net | Date: | Mon, 09 Aug 2004 12:39:30 +0000 |
| Subject: | note 44540 added to function.copy | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-74318@lists.php.net to get a copy of this message | ||
Seeing all the problems that arise when you copy using the standard PHP function, (wich were
described in my previous note), I dediced to approach the problem from a different angle.
The following set of functions will copy a <source_directory>, to a <new_directory>
inside an existing <dest_directory>.
The way it works is by making an FTP to localhost (an FTP to itself), and transfer all files in
binary mode, while creating the neccessary directories. By doing this in the described way, the
files and directories are created with the proper owners and permissions, like when you upload files
to the site by FTP.
Credits go also to www at w8c.com who posted the copys function wich I used as a base to ftp_copys
:)
Good luck and may the force be with you all...
<?
$foarray=posix_getpwuid(getmyuid());
$user=$foarray['name'];
ftp_copys($user, 'password', '/public_html', '/source_directory',
'/dest_directory/new_directory');
function ftp_copys($username, $password, $ini_dir, $source, $dest)
{
$connection = ftp_connect('127.0.0.1'); // connection
$result = ftp_login($connection, $username, $password);
if ((!$connection) || (!$result)) {
echo 'Connection Failed!!!<BR>';
return false;
} else {
echo 'Connected sucessfully!!!<BR>';
ftp_chdir($connection, $ini_dir); // go to destination
ftp_doit($connection, $username, $ini_dir.$source, $ini_dir.$dest);
ftp_close($connection);
return true;
}
}
function ftp_doit($connection, $username, $source, $dest)
{
$absource='/home/'.$username.$source;
$abdest='/home/'.$username.$dest;
if (!is_dir($absource)) {
echo $absource, ' is not a directory\n';
return 0;
}
if (!is_dir($abdest))
{
echo 'Making directory ', $dest, '<BR>';
if (ftp_mkdir($connection, $dest))
{
echo "successfully created $dest<BR>";
} else {
echo "There was a problem while creating $dest<br>";
}
}
$h=@dir($absource);
while (@($entry=$h->read()) !== false)
{
if (($entry!=".")&&($entry!=".."))
{
if (is_dir("$absource/$entry")&&$abdest!=="$absource/$entry")
{
ftp_doit($connection, $username,
"$source/$entry","$dest/$entry");
}
else
{
if(ftp_put($connection, "$dest/$entry", "$absource/$entry",
FTP_BINARY))
{
echo "Successfully uploaded $dest/$entry<BR>";
} else {
echo "There was a problem while uploading $dest/$entry<BR>";
}
}
}
}
$h->close();
return true;
}
?>
----
Manual Page -- http://www.php.net/manual/en/function.copy.php
Edit -- http://master.php.net/manage/user-notes.php?action=edit+44540
Delete -- http://master.php.net/manage/user-notes.php?action=delete+44540&report=yes
Reject -- http://master.php.net/manage/user-notes.php?action=reject+44540&report=yes
Search -- http://master.php.net/manage/user-notes.php