代码之家  ›  专栏  ›  技术社区  ›  Gatman

用曲线传输图像

  •  4
  • Gatman  · 技术社区  · 16 年前

    我有两台服务器。我想用curl将一个文件从一个服务器传输到另一个服务器。有人能给我举个很好的例子吗?我该给curl什么选择…..

    谢谢。

    3 回复  |  直到 16 年前
        1
  •  1
  •   Centurion    16 年前

    PHP

    $ch = curl_init();
    curl_setopt($ch, CURLOPT_HEADER, 0);
    curl_setopt($ch, CURLOPT_VERBOSE, 1);
    // true to return the transfer as a string of the return value
    // of 'curl_exec' instead of outputting it directly
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 
    
    curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible;)");
    curl_setopt($ch, CURLOPT_URL, 'http://localhost/test/curl/receiver.php');
    curl_setopt($ch, CURLOPT_POST, true);
    $post = array(
      'euro' => '@eurodance.pls',
      'flush' => '@flush_next.png',
      'first_name' => 'Vadim'
    );
    curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
    $response = curl_exec($ch);
    print_r($response);
    

    收件人

    if(isset($_FILES)){
      $temp_file_name = $_FILES['euro']['tmp_name'];
      $original_file_name = $_FILES['euro']['name'];
    
      // Find file extention
      $ext = explode ('.', $original_file_name);
      $ext = $ext [count ($ext) - 1];
    
      // Remove the extention from the original file name
      $file_name = str_replace ($ext, '', $original_file_name);
    
      $new_name = '_'.$file_name . $ext;
    //echo $file_name ." ". $ext;
    
      if (move_uploaded_file ($temp_file_name, $new_name)) {
          echo "success";
       } else {
          echo "error";
        }
    
    }
    if(isset($_FILES)){
      $temp_file_name = $_FILES['flush']['tmp_name'];
      $original_file_name = $_FILES['flush']['name'];
    
      // Find file extention
      $ext = explode ('.', $original_file_name);
      $ext = $ext [count ($ext) - 1];
    
      // Remove the extention from the original file name
      $file_name = str_replace ($ext, '', $original_file_name);
    
      $new_name = '_'.$file_name . $ext;
    //echo $file_name ." ". $ext;
    
      if (move_uploaded_file ($temp_file_name, $new_name)) {
          echo "success";
       } else {
          echo "error";
        }
    
    }
    
        2
  •  2
  •   Jim    16 年前
        3
  •  0
  •   Chris    16 年前

    使用curl似乎使用了错误的工具(即当你有锤子时,每个问题看起来都像钉子),基于你所给的,如果scp/sftp不够灵活,为什么不查看scp/sftp,甚至rsync。如果没有,则必须在源服务器上托管该文件并启动curl以请求该文件(即通过HTTP)。

    推荐文章