如何使用php中的curl将上传文件发送到其他网站?

时间:2010-09-14 05:29:11

标签: php curl

如何使用PHP中的Curl将文件上传到另一个网站并获取响应页面?

网站: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"];
}
?>

它没有工作[如何] ??

2 个答案:

答案 0 :(得分:2)

这是一个example of how to upload a file。 (首先点击“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);
?>

答案 1 :(得分:1)

除了Paul Schreiber的回答:

根据how to upload file using curl with php,从php 5.5开始,你需要使用curl_file_create($path)而不是"@$path"

经过测试:它确实有效。

使用@方式无法上传文件。