note 43248 added to ref.ftp
| From: | (kristy@isp7.net) | Date: | Tue, 15 Jun 2004 15:50:09 +0000 |
| Subject: | note 43248 added to ref.ftp | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-71103@lists.php.net to get a copy of this message | ||
Here's a little function that I created to recurse through a local directory and upload the
entire contents to a remote FTP server.
In the example, I'm trying to copy the entire "iwm" directory located at
/home/kristy/scripts/iwm to a remote server's /public_html/test/ via FTP.
The only trouble is that for the line "if (!ftp_chdir($ftpc,$ftproot.$srcrela))", which I
use to check if the directory already exists on the remote server, spits out a warning about being
unable to change to that directory if it doesn't exist.
But an error handler should take care of it.
My thanks to the person who posted the snippet on retrieving the list of files in a directory.
For the version of the script that echo's it's progress as it recurses & uploads, go
to: http://pastebin.com/73784
<?php
// --------------------------------------------------------------------
// THE TRIGGER
// --------------------------------------------------------------------
// set the various variables
$ftproot = "/public_html/test/";
$srcroot = "/home/kristy/scripts/";
$srcrela = "iwm/";
// connect to the destination FTP & enter appropriate directories both locally and remotely
$ftpc = ftp_connect("ftp.mydomain.com");
$ftpr = ftp_login($ftpc,"username","password");
if ((!$ftpc) || (!$ftpr)) { echo "FTP connection not established!"; die(); }
if (!chdir($srcroot)) { echo "Could not enter local source root directory."; die(); }
if (!ftp_chdir($ftpc,$ftproot)) { echo "Could not enter FTP root directory."; die(); }
// start ftp'ing over the directory recursively
ftpRec ($srcrela);
// close the FTP connection
ftp_close($ftpc);
// --------------------------------------------------------------------
// THE ACTUAL FUNCTION
// --------------------------------------------------------------------
function ftpRec ($srcrela)
{
global $srcroot;
global $ftproot;
global $ftpc;
global $ftpr;
// enter the local directory to be recursed through
chdir($srcroot.$srcrela);
// check if the directory exists & change to it on the destination
if (!ftp_chdir($ftpc,$ftproot.$srcrela))
{
// remote directory doesn't exist so create & enter it
ftp_mkdir ($ftpc,$ftproot.$srcrela);
ftp_chdir ($ftpc,$ftproot.$srcrela);
}
if ($handle = opendir("."))
{
while (false !== ($fil = readdir($handle)))
{
if ($fil != "." && $fil != "..")
{
// check if it's a file or directory
if (!is_dir($fil))
{
// it's a file so upload it
ftp_put($ftpc, $ftproot.$srcrela.$fil, $fil, FTP_BINARY);
}
else
{
// it's a directory so recurse through it
if ($fil == "templates")
{
// I want the script to ignore any directories named "templates"
// and therefore, not recurse through them and upload their contents
}
else
{
ftpRec ($srcrela.$fil."/");
chdir ("../");
}
}
}
}
closedir($handle);
}
}
?>
----
Manual Page -- http://www.php.net/manual/en/ref.ftp.php
Edit -- http://master.php.net/manage/user-notes.php?action=edit+43248
Delete -- http://master.php.net/manage/user-notes.php?action=delete+43248&report=yes
Reject -- http://master.php.net/manage/user-notes.php?action=reject+43248&report=yes
Search -- http://master.php.net/manage/user-notes.php