cURL - 无法将文件从服务器1发送到服务器2

时间:2017-05-26 03:51:17

标签: php apache curl ftp file-transfer

我在aws上有2台服务器。我正在尝试将文件从server1移动到server2。这是我在server1上的代码 -

function moveFileToServer2($filepath, $target_url){
        $cFile = null;
        if (function_exists('curl_file_create')) {
            $cFile = curl_file_create($filepath);
        } else {
            $cFile = '@' . realpath($filepath);
        }
        if($cFile != null){
            $post = array('file_contents'=> $cFile);
            try{
                $ch = curl_init();
                curl_setopt($ch, CURLOPT_URL, $target_url);
                curl_setopt($ch, CURLOPT_HEADER, false);
                curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
                curl_setopt($ch, CURLOPT_POST, true);
                curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
                curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
                curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
                curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
                $result = curl_exec ($ch);
            }catch(Exception $e){
                 //catch exception
            }finally{
                curl_close ($ch);
            }
            return $result;
        }
    }

所有文件路径都是正确的(绝对和相对的一切)。我已经验证了它们。

这是我在server2上的代码(curl_file_receiver.php) -

<?php
    //some other stuff here         
    $output_dir = "../uploads/";

    move_uploaded_file($_FILES["file_contents"]["tmp_name"], $output_dir);
    echo json_encode(1);
    exit;
?>

此代码未将文件移动到服务器2.我尝试使用curl传递其他参数,并且我能够建立连接并传递字符串数据。

这里,当我的CURLOPT_FOLLOWLOCATION选项设置为true时,我在curl结果中收到空数组。

当我的CURLOPT_FOLLOWLOCATION设置为false时,我在结果中收到一条HTML,说明了这一点 -

<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
<html><head>
<title>302 Found</title>
</head><body>
<h1>Found</h1>
<p>The document has moved <a href="https://<server-path>/curl_file_receiver.php?<some-other-params>"here</a>.</p>
<hr>
<address>Apache/2.4.7 (Ubuntu) Server at -server-name- Port 80</address>
</body></html>

无论哪种方式,我都没有在服务器2上接收任何文件。此代码在我的本地计算机上运行正常。 (即,如果我在同一个XAMPP服务器上将文件从一个应用程序发送到另一个应用程序)(每个服务器托管一个PHP项目) 有人可以告诉我出了什么问题吗?我需要在我的aws服务器上启用一些curl / php设置吗?或者我需要设置一些卷曲选项?

另外,我需要能够在生产服务器上的https上发送文件,我已经关闭了CURLOPT_SSL_VERIFYPEER。可以通过https发送文件的解决方案对我有很大帮助。

0 个答案:

没有答案