PHP - ftp_put +复制整个文件夹结构

时间:2012-01-07 23:01:41

标签: php ftp

我正在尝试创建一个FTP脚本,将本地文件夹结构复制到FTP点。基本上是为了更新网站。

我一直在测试以下代码(已更改用户/ pass / domain)但连接没有失败并且似乎正在运行。

   $server = 'ftp.domainname.co';
   $ftp_user_name = 'user';
   $ftp_user_pass = 'pass';
   $dest = '.';
   $source = '.';
   $mode = 'FTP_ASCII';


   $connection = ftp_connect($server);

   $login = ftp_login($connection, $ftp_user_name, $ftp_user_pass);

   if (!$connection || !$login) { die('Connection attempt failed!'); }

   $upload = ftp_put($connection, $dest, $source, $mode);

   if (!$upload) { echo 'FTP upload failed!'; }

   ftp_close($connection); 

我确信破解的重点是ftp_put行。 我的问题是:

  1. ftp_put可以上传整个目录结构中的文件等,或者这只是一次上传一个文件?我应该使用不同的命令吗?

  2. 我认为这些变量有问题:

        $dest = '.';
        $source = '.';
        $mode = 'FTP_ASCII';
    
  3. 我相信模式是正确的。

    $ dest - 这只是ftp服务器的根目录是ftp.domainname.co - 我应该把ftp服务器名称放在这里。

    $ source - 这是当前的本地路径 - 我也尝试了完整的C:\ etc路径。

    我收到此错误: 警告:ftp_put()期望参数4为长

    任何帮助都会很棒。

    THX

3 个答案:

答案 0 :(得分:12)

它无法正常工作,因为它需要一个文件,而不是一个目录。 PHP manual for ftp_put有一些代码示例,用于评论者发布的递归文件上传。

以下是其中之一(请注意,它需要完整路径):

function ftp_putAll($conn_id, $src_dir, $dst_dir) {
    $d = dir($src_dir);
    while($file = $d->read()) { // do this for each file in the directory
        if ($file != "." && $file != "..") { // to prevent an infinite loop
            if (is_dir($src_dir."/".$file)) { // do the following if it is a directory
                if (!@ftp_chdir($conn_id, $dst_dir."/".$file)) {
                    ftp_mkdir($conn_id, $dst_dir."/".$file); // create directories that do not yet exist
                }
                ftp_putAll($conn_id, $src_dir."/".$file, $dst_dir."/".$file); // recursive part
            } else {
                $upload = ftp_put($conn_id, $dst_dir."/".$file, $src_dir."/".$file, FTP_BINARY); // put the files
            }
        }
    }
    $d->close();
}

答案 1 :(得分:0)

Warning: ftp_put() expects parameter 4 to be long

嗯,对我而言,似乎很明显:参数4需要很长时间' (或:一个数字)。在这种情况下,它也可以是表示该数字的CONSTANT,例如ftp_put(x, y, z, FTP_ASCII)。没有引号('),就像你做的那样:ftp_put(x, y, z, 'FTP_ASCII')

答案 2 :(得分:0)

对于那些仍在寻找答案的人:我做了一个要旨: FTPRecursiveFolderUpload.php将会更新,但是下面是主要代码:

<?php
error_reporting(E_ERROR | E_PARSE);
//Define vars
$ftp_server = getenv('server'); // As ftp.server.com
$ftp_user_name = getenv('user'); // As user
$ftp_password = getenv('password'); //As password
$remoteDir = getenv('remoteDir'); // As /home/user/ftp/ WITH the last slash!!
$dir = getenv('dir'); // As folder/download WITHOUT the last slash!!
function make_directory($ftp_stream, $dir){ //Create FTP directory if not exists
    // if directory already exists or can be immediately created return true
    if (ftp_chdir ($ftp_stream, $dir) || @ftp_mkdir($ftp_stream, $dir)) return true;
    // otherwise recursively try to make the directory
    if (!make_directory($ftp_stream, dirname($dir))) return false;
    // final step to create the directory
    return ftp_mkdir($ftp_stream, $dir);
}
if (boolval(getenv('ssl')) == true){ // Is it and SSL Connection
    $conn_id = ftp_ssl_connect($ftp_server, intval(getenv('port'))); // Create FTP Secure Connection
}else{
    $conn_id = ftp_connect($ftp_server, intval((getenv('port')? getenv('port') : 21))); // Create FTP  Connection
}
$login_result = ftp_login($conn_id, $ftp_user_name, $ftp_password); //Login in with credentials
if ((!$conn_id) || (!$login_result)) { // If login fails
    echo "FTP Connection failed to server $ftp_server for user $ftp_user_name <br>\r\n";
    exit;
} else {
    echo "Connected to Server $ftp_server, for user $ftp_user_name <br>\n";
}
ftp_pasv($conn_id, true); // Set Passive mode
$recursiveFileResearch = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($dir)); // Get all files in folder and subfolder in the selected directory
$files = array();
foreach ($recursiveFileResearch as $file) {
    if ($file->isDir()){
        continue;
    }
    $files[] = str_replace($dir . "/", "", str_replace('\\', '/', $file->getPathname())); // Store the file without backslashes (Windows..) and without the root directory
}
if (count($files) > 0) {
    foreach ($files as $file) {
        make_directory($conn_id, $remoteDir . dirname($file)); // Create directory if not exists
        ftp_chdir ($conn_id, $remoteDir . dirname($file)); // Go to that FTP directory
        echo "Current directory : " . ftp_pwd($conn_id) . " for file : " . basename($file)
            . " that could be found locally : " . $dir . "/" . $file . "<br>\n"; // Some logs to chekc the process
        ftp_put($conn_id, basename($file), $dir . "/"  . $file, FTP_BINARY); //Upload the file to current FTP directory
        echo "Uploaded " . basename($file) . "<br>\n"; // Some logs to chekc the process
    }
} else {
    echo "Didn't found any folder/files to send in directory : " . $dir . "<br>\n";
}
ftp_close($conn_id); // Close FTP Connection
echo "Finished <br>\n";