【问题标题】:PHP - ftp_put + Copy Entire Folder StructurePHP - ftp_put + 复制整个文件夹结构
【发布时间】:2012-01-07 23:01:41
【问题描述】:

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

我一直在测试以下代码(已更改用户/密码/域),但连接没有失败并且似乎正在工作。

   $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';
    

我相信模式是正确的。

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

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

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

任何帮助都会很棒。

谢谢

【问题讨论】:

  • 如果您可以使用其他协议,请考虑使用 rsync-over-SSH 方法而不是 FTP。它更安全、具有确定性、对许多常见故障具有弹性,并且在大多数情况下默认是安全的。

标签: php ftp


【解决方案1】:

它不工作,因为它需要一个文件,而不是一个目录。 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();
}

【讨论】:

    【解决方案2】:
    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')

    【讨论】:

      【解决方案3】:

      对于那些仍在寻找答案的人:我做了一个要点: 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";
      

      【讨论】:

      • @AntonMenshov 完成,我是新手,所以我不太习惯这些规则。谢谢
      猜你喜欢
      • 1970-01-01
      • 2013-07-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-03-11
      • 1970-01-01
      • 2011-07-04
      • 2011-08-24
      相关资源
      最近更新 更多