【问题标题】:copy file from one server to another programmatically in php在php中以编程方式将文件从一台服务器复制到另一台服务器
【发布时间】:2015-02-23 17:19:57
【问题描述】:

我已将所有代码文件托管在一台服务器上,其域类似于example.com,并且我位于其中一个 html 页面。我希望 example.com 的一些文件移动到另一个域为 example2.com 的服务器上。我已经通过互联网搜索,但我找不到任何好的解决方案。请告诉我,如果没有 FTP 客户端或没有我们手动上传文件的浏览器,这是否可能。或者有什么方法可以将 HTML 表单中的文件从一台服务器提交到另一台服务器,比如我们会写

<form action="http://example2.com/action_page.php">

任何帮助将不胜感激。

【问题讨论】:

标签: php ftp-client


【解决方案1】:

仍有 2 种可能的方式可用于从另一台服务器复制文件。

-一种是从 example.com 中删除您的 .htaccess 文件或允许访问所有文件(通过修改您的 .htaccess 文件)。 - 通过它们各自的 URL 访问/读取这些文件,并使用 'file_get_contents()' 和 'file_put_contents()' 方法保存这些文件。但是这种方法也会使其他人也可以访问所有文件。

            $fileName       = 'filename.extension';
            $sourceFile     = 'http://example.com/path-to-source-folder/' . $fileName;
            $targetLocation = dirname( __FILE__ ) . 'relative-path-destination-folder/' + $fileName;

            saveFileByUrl($sourceFile, $targetLocation);

            function saveFileByUrl ( $source, $destination ) {
                if (function_exists('curl_version')) {
                    $curl   = curl_init($fileName);
                    $fp     = fopen($destination, 'wb');
                    curl_setopt($ch, CURLOPT_FILE, $fp);
                    curl_setopt($ch, CURLOPT_HEADER, 0);
                    curl_exec($ch);
                    curl_close($ch);
                    fclose($fp);
                } else {
                    file_put_contents($destination, file_get_contents($source));
                }
            }

或者您可以在 example.com 上创建代理/服务,以在验证密码或用户名/密码组合后读取特定文件(根据您的要求)。

            //In myproxy.php
            extract($_REQUEST);
            if (!empty($passkey) && paskey == 'my-secret-key') {
                if (!empty($file) && file_exists($file)) {
                    if (ob_get_length()) {
                        ob_end_clean();
                    }
                    header("Pragma: public");
                    header( "Expires: 0");
                    header( "Cache-Control: must-revalidate, post-check=0, pre-check=0");
                    header( 'Content-Type: ' . mime_content_type($file) );
                    header( "Content-Description: File Transfer");
                    header( 'Content-Disposition: attachment; filename="' . basename( $file ) . '"' );
                    header( "Content-Transfer-Encoding: binary" );
                    header( 'Accept-Ranges: bytes' );
                    header( "Content-Length: " . filesize( $file ) );
                    readfile( $file );
                    exit;
                } else {
                    // File not found 
                }
            } else {
                //  You are not authorised to access this file.
            }

您可以通过 url 'http://example.com/myproxy.php?file=filename.extension&passkey=my-secret-key' 访问该代理/服务。

【讨论】:

    【解决方案2】:

    如果你设置这个文件的内容:example2.com/action_page.php 为:

    <?php
    $tobecopied = 'http://www.example.com/index.html';
    $target = $_SERVER['DOCUMENT_ROOT'] . '/contentsofexample/index.html';
    
    if (copy($tobecopied, $target)) {
        //File copied successfully
    }else{
        //File could not be copied
    }
    ?>
    

    并通过命令行或cron运行它(因为你已经写了一个php相关的问题,但禁止使用浏览器!)它应该将example.com/index.html的内容复制到您站点的目录(域:example2.com),称为 contentsofexample。

    注意如果你想复制整个网站,你应该把它放在一个 for 循环中

    【讨论】:

      【解决方案3】:

      您也可以使用 zip 方法在服务器之间交换文件。 你拥有两台服务器,所以你不应该有安全问题。

      在托管您想要的文件或文件夹的服务器上,创建一个 php 脚本并为其创建一个 cron 作业。示例代码如下:

      <?php
      /**
       * ZIP All content of current folder
      
      /* ZIP File name and path */
      $zip_file = 'myfiles.zip';
      
      /* Exclude Files */
      $exclude_files = array();
      $exclude_files[] = realpath( $zip_file );
      $exclude_files[] = realpath( 'somerandomzip.php' );
      
      /* Path of current folder, need empty or null param for current folder */
      $root_path = realpath( '' ); //or $root_path = realpath( 'folder_name' ); if the file(s) are in a particular folder residing in the root
      
      /* Initialize archive object */
      $zip = new ZipArchive;
      $zip_open = $zip->open( $zip_file, ZipArchive::CREATE );
      
      /* Create recursive files list */
      $files = new RecursiveIteratorIterator(
          new RecursiveDirectoryIterator( $root_path ),
          RecursiveIteratorIterator::LEAVES_ONLY
      );
      
      /* For each files, get each path and add it in zip */
      if( !empty( $files ) ){
      
          foreach( $files as $name => $file ) {
      
              /* get path of the file */
              $file_path = $file->getRealPath();
      
              /* only if it's a file and not directory, and not excluded. */
              if( !is_dir( $file_path ) && !in_array( $file_path, $exclude_files ) ){
      
                  /* get relative path */
                  $file_relative_path = str_replace( $root_path, '', $file_path );
      
                  /* Add file to zip archive */
                  $zip_addfile = $zip->addFile( $file_path, $file_relative_path );
              }
          }
      }
      /* Create ZIP after closing the object. */
      $zip_close = $zip->close();
      ?>
      

      在服务器上创建另一个 php 脚本和 cron 作业以接收如下副本:

      /**
      * Transfer Files Server to Server using PHP Copy
      *
      */ /* Source File URL */
      $remote_file_url = 'url_of_the_zipped';
      
      /* New file name and path for this file */
      $local_file ='myfiles.zip';
      
      /* Copy the file from source url to server */ 
      $copy = copy($remote_file_url, $local_file );
      
      /* Add notice for success/failure */ 
      if( !$copy ) {
      echo "Failed to copy $file...\n"; }
      else{
      echo "Copied $file successfully...\n"; }
      $file = 'myfiles.zip';
      $path = pathinfo( realpath( $file ), PATHINFO_DIRNAME );
      $zip = new ZipArchive;
      $res = $zip->open($file);
      if ($res === TRUE) {
      $zip->extractTo( $path );
      $zip->close();
      echo "$file extracted to $path"; }
      else {
      echo "Couldn't open $file";
      }
      ?>
      

      瞧!

      【讨论】:

        【解决方案4】:
            <?php
        /* Source File URL */
        $remote_file_url = 'http://origin-server-url/files.zip';
          
        /* New file name and path for this new file */
        $local_file = 'files.zip';
          
        /* Copy the file from source url to server */
        $copy = copy( $remote_file_url, $local_file );
          
        /* Add notice for success/failure */
        if( !$copy ) {
            echo "Doh! failed to copy $file...\n";
        }
        else{
            echo "WOOT! Thanks Munshi and OBOYOB! success to copy $file...\n";
        }
        ?>
        

        【讨论】:

          猜你喜欢
          • 2021-06-22
          • 2014-05-17
          • 2012-01-11
          • 1970-01-01
          • 2016-02-26
          • 1970-01-01
          • 2020-06-18
          • 2018-07-08
          • 2011-02-05
          相关资源
          最近更新 更多