views:

989

answers:

1

Hello Friends,

We are struggling to automatically upload images using php - curl. Please let me know if there is any way to do the same.

+1  A: 

the basic idea

<?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",
        "username"=>"foobar",
        "password"=>"secret",
        "submit"=>"submit"
    );
    curl_setopt($ch, CURLOPT_POSTFIELDS, $post); 
    $response = curl_exec($ch);
?>

you can have more info about curl here.

RageZ
Hello RageZ,Thanks for the answer. However, I am not using an array in my code. I am using as below:$Post = Filefieldname = " "Now I don'e know what to write in those " " so that the image can be uploaded.
Aditya
you should use an array that's how the curl_setopt is made, if you don't use array you have to encode the file on your own. Why are you unable to user array ?
RageZ
Browse fields are not getting automatically populated when I am using array so I used a single line url as $post = "field_name=nameSo I was just wondring if I can replace the question marks above with something that will upload the image.
Aditya
you can pass the field as array I will edit my answer
RageZ