1

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

The website: 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]??

monkey_boys
  • 7,108
  • 22
  • 58
  • 82
  • 3
    Have you tried reading the [cURL manual](http://www.php.net/manual/en/book.curl.php) on PHP.net? – amphetamachine Sep 14 '10 at 05:35
  • i don't know how and what, but this worked for me :) The only difference was that i dont upload a file to my server, instead i send a default file to the site – Saurabh Dec 04 '12 at 13:14

2 Answers2

4

On top of Paul Schreiber's answer:

According to how to upload file using curl with php, starting from php 5.5 you need to use curl_file_create($path) instead of "@$path".

Tested: it does work.

With the @ way no file gets uploaded.

Community
  • 1
  • 1
a.l.e
  • 792
  • 8
  • 16
1

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);
?>
Paul Schreiber
  • 12,531
  • 4
  • 41
  • 63