2

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

2 Answers 2

7

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.

Sign up to request clarification or add additional context in comments.

4 Comments

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.
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 ?
Browse fields are not getting automatically populated when I am using array so I used a single line url as $post = "field_name=name&password=password&Filefieldname=???&button_name=Continue"; So I was just wondring if I can replace the question marks above with something that will upload the image.
The leading '@' is critical in the abs path to the file.
1
<?php

/*
ini_set('display_errors',1);
error_reporting(E_ALL);
*/
include('_db.php');
include('_session.php');




$business_id = $session->business->id;
$error = "";
$output = "";

if ($_FILES["file"]["type"] == "image/gif" || $_FILES["file"]["type"] == "image/jpeg" || $_FILES["file"]["type"] == "image/pjpeg" || $_FILES["file"]["type"] == "image/png")
{
    if ($_FILES["file"]["error"] > 0)
    {
        $error = $_FILES["file"]["error"];
        echo "{error: '". $error ."', msg: ''}";
    }
    else
    {
        //set POST variables
        $url = 'http://img.mySite.com/';

        $fields = array(
                    //assign filetype the file extension
                    'filetype'=>substr(strrchr($_FILES["file"]["name"], '.'), 1),
                    //give the file id a unique id
                    'fileid'=>$business_id . ":" . date('YmdGisu') .":". $_FILES["file"]["name"],
                    //read image data into a string using file get contents
                    'content'=>file_get_contents($_FILES['file']['tmp_name'])
                );

        //open connection
        $ch = curl_init();

        //set the url, number of POST vars, POST data
        curl_setopt($ch, CURLOPT_HEADER, 0);
        curl_setopt($ch, CURLOPT_VERBOSE, 0);
        curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible;)");
        curl_setopt($ch,CURLOPT_URL,$url);
        curl_setopt($ch,CURLOPT_POST,true);
        curl_setopt($ch,CURLOPT_POSTFIELDS,$fields);
        curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);

        //execute post
        $output = curl_exec($ch);
        if($output == false)
            $error = "Fail.";
        echo "{error: '". $error ."', msg: '" . $output . "'}";

        //close connection
        curl_close($ch);
    }
  }
  else
  {
    $error = "Incorrect File Format.";
    echo "{error: '". $error ."', msg: ''}";
  }
mysql_close($link);

?>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.