faqts : Computers : Programming : Languages : PHP

+ Search
Add Entry AlertManage Folder Edit Entry Add page to http://del.icio.us/
Did You Find This Entry Useful?

5 of 5 people (100%) answered Yes
Recently 5 of 5 people (100%) answered Yes

Entry

how can i archive a set of remote files, about 10 on my own server?

Feb 24th, 2008 19:41
dman, Justin Dalrymple, joachim molund, http://www.ttnr.org


Assuming all of these files are in one directory, you can simply use a 
recurring function to pull the files one by one.
Here is some example code. This script assumes;
1. All files in need of archiving are inside 1 directory.
$basedir = The local directory you'd like to save your files to. This 
must be set inside AND outside the function.
$root = The remote directory to start from.
2. You have FTP access to the server and correct login information has 
been supplied.
$ftp_server = The remote FTP server to connect to.
$ftp_user_name = Your FTP username
$ftp_user_pass = Your FTP password
This script will run through the entire '$root' directory and download 
all files/directory structure to the '$basedir' directory.
<?
/* SET BASEDIR = YOUR LOCAL BASE DIRECTORY
SET ROOT = THE STARTING DIRECTORY OF YOUR FTP DOWNLOAD. LEAVE BLANK TO 
USE DOCUMENT ROOT
BE SURE TO SET FUNCTION-SPECIFIC BASE DIRECTORY BELOW
*/
$ftp_server = "";
$ftp_user_name = "";
$ftp_user_pass = "";
$conn_id = ftp_connect($ftp_server); 
$login_result = ftp_login($conn_id, $ftp_user_name, $ftp_user_pass); 
IF ((!$conn_id) OR (!$login_result))
{ 
echo ("Error Connecting to FTP.");
exit();
}
$basedir = "";
$root = "";
function read_dir($dirPath, $debugging="")
{
// SET FUNCTION-SPECIFIC BASE DIRECTORY
$basedir = "";
include ("ftp_connect.inc");
global $i;
echo "<p><br><div align='center'><b>$dirPath</b></div><br>\n";
$dir = ftp_chdir($conn_id, $dirPath);
$contents = Array();
$contents = ftp_nlist($conn_id, $dirPath);
while (list($arrayid, $file) = each($contents))
{
$fileold = $file;
IF (strcmp($file, ".") && strcmp($file, ".."))
{
IF (!strstr($file, ".") AND !strstr($file, ".."))
{
$directory_location = $basedir . $file;
IF (!is_dir($directory_location))
{
mkdir ($directory_location, 0777);
}
read_dir($file);
}
ELSE
{
$file_location = $basedir . $file;
$grabfile = @ftp_get($conn_id, $file_location, $file, "FTP_ASCII");
IF (!$grabfile)
{
// Do Nothing.
}
echo "$debugging$file<br>\n";
}
}
}
}
$rootdirs = Array();
$base_directory = $basedir . $root;
IF (!is_dir($base_directory))
{
mkdir($base_directory, 0777);
}
read_dir($root);
@ftp_quit($conn_id);
?>