如何发送上传文件到其他网站通过使用curl在PHP?

问题描述:

如何通过在PHP中使用Curl将文件上传到其他网站并获取响应页面?

How does one upload a file to another website by using Curl in PHP and get the response page?

网站: http://www.postto.me

<form action="posttome.php" enctype="multipart/form-data" method="post">
<input type="hidden" value="2097152" name="MAX_FILE_SIZE">
<input type="file" size="30" class="input" name="img">
<input type="submit" class="input" value="Upload" name="upload">
</form>


<?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, "http://www.postto.me/upload.php");
    curl_setopt($ch, CURLOPT_POST, true);
    // same as <input type="file" name="file_box">
    $post = array(
        "img"=>$_FILES["img"]["tmp_name"],
    );
    curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
    $response = curl_exec($ch);
    echo $response;

echo $_FILES["img"]["tmp_name"];
}
?>

it not working [How] ??

it not working [How]??

这是一个如何上传文件的示例。 (curl file upload example php的第一个匹配。)

Here's an example of how to upload a file. (First hit for "curl file upload example php".)

<?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, _VIRUS_SCAN_URL);
    curl_setopt($ch, CURLOPT_POST, true);
    // same as <input type="file" name="file_box">
    $post = array(
        "file_box"=>"@/path/to/myfile.jpg",
    );
    curl_setopt($ch, CURLOPT_POSTFIELDS, $post); 
    $response = curl_exec($ch);
?>