0

サーバーから別のサーバーに POST メソッドでファイルを送信するスクリプトを作成しようとしています。HTML は次のようになります。

<form action="https://anotherserver/receive.php" method="POST" enctype="multipart/form-data">
    <input type="file" name="file" />
    <input type="submit" />
</form>

この部分を使用すると、自分のコンピューターから別のサーバーにファイルを送信できます。ただし、条件が true の場合、スクリプトは Web サーバーからファイルを送信する必要があります。たとえば、Web サイトでファイルを作成し、[OK] をクリックすると、ファイルが自動的に外部 Web サーバーに送信されます。そのような方法は可能ですか?どんな助けでも大歓迎です。

4

1 に答える 1

2

このコードは、ファイルを別のサーバーに投稿します。

<?php
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_HEADER, 0);
    curl_setopt($ch, CURLOPT_VERBOSE, 0);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible;)");
    curl_setopt($ch, CURLOPT_URL, 'https://anotherserver/receive.php');
    curl_setopt($ch, CURLOPT_POST, true);
    $post = array(
        "file"=>"/path/to/myfile.jpg",
    );
    curl_setopt($ch, CURLOPT_POSTFIELDS, $post); 
    $response = curl_exec($ch);
?>

適応元:http://dtbaker.com.au/random-bits/uploading-a-file-using-curl-in-php.html

于 2012-07-04T08:58:23.863 に答える