Codice PHP:
<?
$vet_text = array("txt", "asp", "html", "php", "htm", "xml");
if(!isset($_POST['ftp_server'])){
?>
<html>
<head>
<title></title>
</head>
<body>
<CENTER>
<font color=white>TRASFERIMENTO SITO</font>
<form method="post" action="trasf.php" >
<font color=white> FTP SERVER:</font> <input type="text" name="ftp_server" size=40>
<font color=white> FTP USERNAME:</font> <input type="text" name="ftp_username" size=35>
<font color=white> FTP PASSWORD:</font> <input type="password" name="ftp_pass" size=35>
<input type="submit" value="TRASFERISCI">
</form>
</CENTER>
</body>
</html>
<?
}
else {
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Frameset//EN" "http://www.w3.org/TR/html4/frameset.dtd">
<html>
<head>
<title></title>
<link href="style.css" rel="stylesheet" type="text/css" />
</head>
<body >
<?php
// VARIABILI //
$path = "../cartella";
$ftp_server=$_POST['ftp_server'];
$ftp_username=$_POST['ftp_username'];
$ftp_password=$_POST['ftp_pass'];
$destinazione="www".substr($ftp_server, 3)."/ftp_test1/";
//echo $destinazione;
//funzione stampa buffer
function echo_flush($txt){
//inizializzazione del buffer per l'output
if (ob_get_level() == 0) ob_start();
echo $txt;
//per Chrome e Safari si deve aggiungere questa istruzione
print str_pad('',4096)."\n";
//invia il contenuto al buffer
ob_flush();
flush();
}
// legge i file e le directory di una cartella!!!
function scanFileNameRecursivly($path = '', &$name = array() )
{
$path = $path == ''? dirname(__FILE__) : $path;
$lists = @scandir($path);
if(!empty($lists))
{
foreach($lists as $f)
{
if(is_dir($path.DIRECTORY_SEPARATOR.$f) && $f != ".." && $f != ".")
{
scanFileNameRecursivly($path.DIRECTORY_SEPARATOR.$f, &$name);
}
else
{ if($f != "..")
$name[] = $path.DIRECTORY_SEPARATOR.$f;
}
}
}
return $name;
}
echo_flush ("<script type=\"text/javascript\">
parent.visualizza();
</script>");
$file_names = scanFileNameRecursivly($path);
unset( $file_names[0] ); //RIMUOVO PRIMO ELEMENTO!!
//CALCOLO PER PROGRESS-BAR
$tot = count($file_names);
$pe = $tot / 10;
$ww= intval($pe);
$incr=$ww;
include('ftp_class.php');
$ftp = new FTPCon($ftp_server, $ftp_username, $ftp_password);
if ($ftp->connect()) {
$p=1;$q=1;
foreach( $file_names as $key => $value )
{
$val = str_replace("\\", "/", $value);
$val_ok = substr($val,13);
if(substr($value, -1)=="."){
//$dir = $ftp->mkdir($destinazione.$val_ok);
if(!$ftp->mkdir($destinazione.$val_ok)) echo("CREAZIONE ".$destinazione.$val_ok." FALLITA!!!");
$dir2 = $ftp->chmod($destinazione.$val_ok, 0777);
}
else{
//$upload = $ftp->upload($value, $destinazione.$val_ok); //Returns true/false
$trova_punto = explode(".", $val_ok);
$estensione = $trova_punto[count($trova_punto) - 1];
$estensione = strtolower($estensione);
echo $estensione."
";
if (in_array($estensione, $vet_text)){
if(!$ftp->upload($value, $destinazione.$val_ok,FTP_ASCII)) echo("TRASFERIMENTO ".$destinazione.$val_ok." FALLITO!!!");
}
else {
if(!$ftp->upload($value, $destinazione.$val_ok,FTP_BINARY)) echo("TRASFERIMENTO ".$destinazione.$val_ok." FALLITO!!!");
}
}
if($p == $ww) {
$te=$q*10;
if ($te==100)$te=98;
echo_flush ("<script type=\"text/javascript\">
parent.clikke(".$te.");
</script>");
$ww = $ww + $incr;
$q++;
}
$p++;
}
$ftp->logout();
}
$te=100;
echo_flush ("<script type=\"text/javascript\">
parent.clikke(".$te.");
</script>");
?>
</body>
</html>
<?
}
?>
ftp_class:
Codice PHP:
<?php
class FTPCon {
var $host = '';
var $username = '';
var $password = '';
var $port = 21;
var $ftpcon = null;
function __construct($host, $username, $password, $port = 21) {
$this->host = $host;
$this->username = $username;
$this->password = $password;
$this->port = $port;
}
function connect() {
if (isset($this->host, $this->username, $this->password, $this->port)) {
$this->ftpcon = ftp_connect($this->host, $this->port);
if (!$this->ftpcon) {
return false;
}
if (!ftp_login($this->ftpcon, $this->username, $this->password)) {
return false;
}
ftp_pasv ($this->ftpcon, true);
return true;
}
}
function ls($dir = '.') {
if (isset($this->ftpcon)) {
$ls = ftp_nlist($this->ftpcon, $dir);
return $ls;
}
}
function upload($file, $path, $mode = FTP_ASCII) {
if (isset($file, $path)) {
if (file_exists($file)) {
$upload = ftp_put($this->ftpcon, $path, $file, FTP_ASCII);
return $upload;
}
}
return false;
}
function download($file, $path, $mode = FTP_BINARY) {
if (isset($file)) {
if (ftp_get($this->ftpcon, $path, $file, FTP_BINARY)) {
return true;
}
}
return false;
}
function chmod($file, $permissions = 0644) {
if (isset($file)) {
if (ftp_chmod($this->ftpcon, $permissions, $file) !== false) {
return true;
} else {
return false;
}
}
}
function mkdir($dirname) {
if (isset($dirname)) {
return ftp_mkdir($this->ftpcon, $dirname);
}
}
function delete($file) {
if (isset($file)) {
return ftp_delete($this->ftpcon, $file);
}
}
function logout() {
if (isset($this->ftpcon)) {
ftp_close($this->ftpcon);
unset($this->ftpcon);
}
}
function __destruct() {
$this->logout();
}
}
?>