Jump to content

Ftp Help


Danny620

Recommended Posts

Hi, I'm thinking of using this ftp class http://www.shayanderson.com/php/simple-ftp-class-for-php.htm

I just wanted to know how I would go about transferring a folder with contents onto a remote server i.e

 

Server Local A

 

Folder – Blog

Inside folder

Index.php

Config.php

Images – folder

Css – folder

 

To remote server B

 

So I just want to copy the blog folder on server A to remote server B can you provide me with some code using class above?

 

Folder – Blog

Inside folder

Index.php

Config.php

Images – folder

Css - folder

Link to comment
Share on other sites

<?php

$root = 'blog'; // starting folder (relative to where this script is at)

// find the folder structure and the files
$folders = array();
$files = array();
$it = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($root));
while($it->valid()){
if (!$it->isDot()){
	$folders[] = $root .'/'. $it->getSubPath(); // get all folders
	$files[] = $it->key(); // get all files
}
$it->next();
}
$folders = array_unique($folders); // remove duplicates

include "SFTP.php";
$ftp = new SFTP("ftp host name", "ftp user name", "ftp password");
if(!$ftp->connect()) {
// connection failed, display last error
echo "Connection failed: {$ftp->error}<br />";
} else {
echo "Connection successful<br />";
// make the folder structure
foreach($folders as $folder){
	if($ftp->mkdir($folder)){
		echo "Made folder: $folder<br />";
	} else {
		echo "Error: {$ftp->error}<br />";
	}
}

// copy all the files
foreach($files as $file){
	if($ftp->put($file, $file)){
		echo "Copied: $file<br />";
	} else {
		echo "Error: {$ftp->error}<br />";
	}
}
}

Edited by PFMaBiSmAd
editor removed white-space in code, had to redo
Link to comment
Share on other sites

A) The directory structure you actually have is not what you stated.

 

B) Since A) wasn't true and since most FTP servers cannot create multiple folder levels at one time, the script would need to be changed to create folders one level at a time.

 

C) If this is a one time operation, you should just use a FTP client to make a copy onto your PC, then FTP it onto the new server.

 

D) If you need to use a php script to do this, you should probably hire a programmer.

Edited by PFMaBiSmAd
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.