2009-05-29 10 views
16

私は自分のサーバに保存されたディレクトリの内容全体をftp経由で他のサーバにアップロードするスクリプトを書こうとしています。PHPでFTP全体のディレクトリをアップロードする

私はdocumentation on www.php.netで読んできましたが、一度に複数のファイルをアップロードする方法が見つからないようです。

これを行う方法はありますか、またはそのディレクトリのインデックスを作成し、アップロードするファイルの配列を作成するスクリプトはありますか?

ご協力いただきありがとうございます。

答えて

0

複数のファイルを一度にアップロードしたい場合は、スレッドまたはフォークを使用する必要があります。

は、私はPHPでのスレッドimplentationのわからないんだけど、編集、あなたはPHP SPLおよび/またはPEAR

を見てみる必要があります:フランク・ファーマーへおかげで、私はフォークがあったことを知っているようにpcntl_fork()

また、特定のディレクトリのすべてのファイルをアップロードするには、コンテンツディレクトリ全体を再帰的に取得する必要があります。

+1

PHPはスレッドはありませんが、それはフォーク(pcntl_fork()を使用して)ないと、PHPのフォーク機能のいくつかの糸状のラッパーがあります。 –

+0

ありがとう、ヒントについては、私のポストへのリンクを追加しました。 –

+0

フォークの使用はお勧めしません。それは毛むくじゃらのものです。単純に複数の接続を開き、socket_select()を使用して非同期で書き込みを行う方が簡単です。 –

1

は、フォルダ内のすべてのファイルを介し

$servername = $GLOBALS["servername"]; 
    $ftpUser = $GLOBALS["ftpUser"]; 
    $ftpPass = $GLOBALS["ftpPass"]; 
$conn_id = ftp_connect($servername) or die("<p style=\"color:red\">Error connecting to $servername </p>"); 

if(ftp_login($conn_id, $ftpUser, $ftpPass)) 
{ 
    $dir_handle = @opendir($path) or die("Error opening $path"); 

     while ($file = readdir($dir_handle)) { 
      ftp_put($conn_id, PATH_TO_REMOTE_FILE, $file) 
     } 
} 
12

を反復、ループでそれを行うあなたが開いている接続ができたら、シリアルディレクトリの内容をアップロードすることは簡単です:

foreach (glob("/directory/to/upload/*.*") as $filename) 
    ftp_put($ftp_stream, basename($filename) , $filename, FTP_BINARY); 

アップロードすべてのファイルを並行して処理することはより困難になります。

+1

これは私に多くの時間を節約しました。優れたコードスニペット! –

0

私はそれをコード化し、スクリプトはサブフォルダとファイルを使ってフォルダ全体をアップロードします。

私はそれがあなたを助けてくれることを願っています。

<?php 
ob_start(); 
set_time_limit(0); 
//r10.net fatal 

$anadizin="uploadedeceginizdizin"; //this is the folder that you want to upload with all subfolder and files of it. 

$ftpsunucu="domain.com"; //ftp domain name 
$ftpusername="ftpuser"; //ftp user name 
$ftppass="ftppass"; //ftp passowrd 
$ftpdizin="/public_html"; //ftp main folder 





$arkadaslarlabardaydik = ftp_connect($ftpsunucu); 
$ictikguldukeglendik = ftp_login($arkadaslarlabardaydik, $ftpusername, $ftppass); 

if((!$arkadaslarlabardaydik) || (!$ictikguldukeglendik)) 
{ 
    echo "cant connect!"; 
    die(); 
} 


function klasoruoku($dizinadi) 
{ 
    global $nerdeyiz,$fulldizin,$ftpdizin,$arkadaslarlabardaydik,$ftpdizin; 

    chdir($dizinadi."\\"); 
    $dizin = opendir("."); 

    while($bilgi=readdir($dizin)) 
    { 
    if ($bilgi!='.' and $bilgi!='..' and $bilgi!="Thumbs.db") 
    { 
     $tamyol="$dizinadi\\$bilgi"; 

     $lokalkla=str_replace("".$nerdeyiz."\\","",$dizinadi).""; 
     $lokaldosya="$lokalkla\\$bilgi"; 
     $ftpyeyolla=str_replace(array("\\\\","\\"),array("/","/"),"$ftpdizin\\".str_replace("".$fulldizin."","",$dizinadi)."\\$bilgi"); 

     if(!is_dir($bilgi)) 
     { 
      $yükleme = ftp_put($arkadaslarlabardaydik, $ftpyeyolla, $tamyol, FTP_BINARY); 

      if (!$yükleme) 
      { 
       echo "$lokaldosya <font color=red>uploaded</font>"; echo "<br>"; fls(); 
      } 
      else 
      { 
       echo "$lokaldosya <font color=green>not uploaded</font>"; echo "<br>"; fls(); 
      } 
     } 
     else 
     { 
      ftp_mkdir($arkadaslarlabardaydik, $ftpyeyolla); 
      klasoruoku("$dizinadi\\$bilgi"); 
      chdir($dizinadi."\\"); 
      fls(); 
     } 
    } 
    } 
    closedir ($dizin); 
} 

function fls() 
{ 
    ob_end_flush(); 
    ob_flush(); 
    flush(); 
    ob_start(); 
} 

$nerdeyiz=getcwd(); 
$fulldizin=$nerdeyiz."\\$anadizin"; 
klasoruoku($fulldizin); 
ftp_close($arkadaslarlabardaydik); 

?> 
5

私は@ iYETERのコードをとり、それをクラスオブジェクトとしてラップしました。

あなたはこれらの行で、このコードを呼び出すことができます。

$ftp = new FtpNew("hostname"); 

$ftpSession = $ftp->login("username", "password"); 

if (!$ftpSession) die("Failed to connect."); 

$errorList = $ftp->send_recursive_directory("/local/dir/", "/remote/dir/"); 
print_r($errorList); 

$ftp->disconnect(); 

これは、再帰的にローカルディレクトリをクロールして、リモートディレクトリの相対でそれを配置します。エラーが発生した場合は、すべてのファイルとその例外コードの配列階層を作成します(これまでのところ2つしか取り込まれませんでした。別のエラーであれば、現在はデフォルトルートをスローします)

これは、

<?php 
//Thanks for iYETER on http://stackoverflow.com/questions/927341/upload-entire-directory-via-php-ftp 

class FtpNew { 
private $connectionID; 
private $ftpSession = false; 
private $blackList = array('.', '..', 'Thumbs.db'); 
public function __construct($ftpHost = "") { 
    if ($ftpHost != "") $this->connectionID = ftp_connect($ftpHost); 
} 

public function __destruct() { 
    $this->disconnect(); 
} 

public function connect($ftpHost) {  
    $this->disconnect(); 
    $this->connectionID = ftp_connect($ftpHost); 
    return $this->connectionID; 
} 

public function login($ftpUser, $ftpPass) { 
    if (!$this->connectionID) throw new Exception("Connection not established.", -1); 
    $this->ftpSession = ftp_login($this->connectionID, $ftpUser, $ftpPass); 
    return $this->ftpSession; 
} 

public function disconnect() { 
    if (isset($this->connectionID)) { 
     ftp_close($this->connectionID); 
     unset($this->connectionID); 
    } 
} 

public function send_recursive_directory($localPath, $remotePath) { 
    return $this->recurse_directory($localPath, $localPath, $remotePath); 
} 

private function recurse_directory($rootPath, $localPath, $remotePath) { 
    $errorList = array(); 
    if (!is_dir($localPath)) throw new Exception("Invalid directory: $localPath"); 
    chdir($localPath); 
    $directory = opendir("."); 
    while ($file = readdir($directory)) { 
     if (in_array($file, $this->blackList)) continue; 
     if (is_dir($file)) { 
      $errorList["$remotePath/$file"] = $this->make_directory("$remotePath/$file"); 
      $errorList[] = $this->recurse_directory($rootPath, "$localPath/$file", "$remotePath/$file"); 
      chdir($localPath); 
     } else { 
      $errorList["$remotePath/$file"] = $this->put_file("$localPath/$file", "$remotePath/$file"); 
     } 
    } 
    return $errorList; 
} 

public function make_directory($remotePath) { 
    $error = ""; 
    try { 
     ftp_mkdir($this->connectionID, $remotePath); 
    } catch (Exception $e) { 
     if ($e->getCode() == 2) $error = $e->getMessage(); 
    } 
    return $error; 
} 

public function put_file($localPath, $remotePath) { 
    $error = ""; 
    try { 
     ftp_put($this->connectionID, $remotePath, $localPath, FTP_BINARY); 
    } catch (Exception $e) { 
     if ($e->getCode() == 2) $error = $e->getMessage(); 
    } 
    return $error; 
} 
} 
関連する問題