2

How to achieve an image upload via cURL?

I was told to use curl_file_create but it's not working.

This is the array from post when I manually (using Chrome) upload a file and see the raw structure:

Array
(
    [image] => Array
        (
            [0] => 377529090726537.jpg
        )

    [thumbnail_digest] => Array
        (
            [0] => EMPTYDIGEST
        )

    [digest_present] => Array
        (
            [0] => 0
        )

    [image_rotation] => Array
        (
            [0] => 0
        )
)

Inside my cURL I tried:

$cfile = curl_file_create('IMG_33071.jpg','image/jpeg','test_name');
$post_data = array(
'image' => $cfile,
);

But after running the script, I receive this error:

Catchable fatal error: Object of class CURLFile could not be converted to string in C:\Path\to\my\server\curl.php on line 144

The image is on the same folder from curl.php file. My php version is 5.5.

I also tried this:

$cfile = curl_file_create('http://www.siteexample.com/path/to/my/file.jpg','image/jpeg','test_name');

But no success :(

My structure:

//These are the post data username and password
$login_data = 'email='.$email.'&password='.$password.'&entrar=Entrar';

//Create a curl object
$login = curl_init();

//Set the useragent
$agent = $_SERVER["HTTP_USER_AGENT"];
//$agent = 'Mozilla/5.0 (Windows NT 6.2; WOW64; rv:17.0) Gecko/20100101 Firefox/17.0';
curl_setopt($login, CURLOPT_USERAGENT, $agent);

curl_setopt($login, CURLOPT_SSL_VERIFYPEER, false);

//Set the URL
curl_setopt($login, CURLOPT_URL, $login_url );

//This is a POST query
curl_setopt($login, CURLOPT_POST, 1 );

//Set the post data
curl_setopt($login, CURLOPT_POSTFIELDS, $login_data);

//We want the content after the query
curl_setopt($login, CURLOPT_RETURNTRANSFER, 1);

//Follow Location redirects
curl_setopt($login, CURLOPT_FOLLOWLOCATION, 1);

/*
Set the cookie storing files
Cookie files are necessary since we are logging and session data needs to be saved
*/

curl_setopt($login, CURLOPT_COOKIEJAR, 'cookie.txt');
curl_setopt($login, CURLOPT_COOKIEFILE, 'cookie.txt');

//Execute the action to login
$LoginResult = curl_exec($login);
echo $LoginResult;



    //create array of data to be posted
$post_data = array(
'name' => 'Name',
'email' => $email,
'email_confirm' => $email,
'phone' => '9999999999',
'passwd' => $password,
'passwd_ver' => $password,
'create' => '',
);


//traverse array and prepare data for posting (key1=value1)
foreach ( $post_data as $key => $value) {
    $post_items[] = $key . '=' . $value;
}


//create the final string to be posted using implode()
$post_string = implode ('&', $post_items);

var_dump($post_string);

curl_setopt($login, CURLOPT_URL, 'http://urltocurl.com/verify');
curl_setopt($login, CURLOPT_POST, false);
curl_setopt($login, CURLOPT_POSTFIELDS, $post_string);
curl_setopt($login, CURLOPT_TIMEOUT, 0);
$answer = curl_exec($login);
echo $answer;


if (curl_error($login)) {
    echo curl_error($login);
}

//close the connection
curl_close($login);

Now take a look at the array at the beggining.

Array
    (
        [image] => Array
            (
                [0] => 377529090726537.jpg
            )

It is an array inside another one. How to achieve that? Maybe curl_file_create is working but it's sending the image to a wrong location of array.

5
  • see this:58bits.com/blog/2014/06/14/how-upload-file-using-php-curl Commented Oct 29, 2015 at 13:41
  • That's the place where I got some info. Even using its function, I get the same error =( Commented Oct 29, 2015 at 13:42
  • try with test_name.jpg instead of test_name may be extension is the issue. Commented Oct 29, 2015 at 13:45
  • see this:blog.derakkilgo.com/2009/06/07/… Commented Oct 29, 2015 at 14:02
  • Maybe I'm doind something wrong... The [image] is another array.... so it's an array inside another one. I'll update my post, take a look at the structure I'm using to send data. Commented Oct 29, 2015 at 14:12

1 Answer 1

2

Look at my example it should work for you:

$ch = curl_init();
$data = array('name' => 'Foo', 'file' => '@/path/to/image.jpeg');
curl_setopt($ch, CURLOPT_URL, 'http://example.com/upload.php');
curl_setopt($ch, CURLOPT_POST, 1);

curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_exec($ch);
Sign up to request clarification or add additional context in comments.

3 Comments

No success... Maybe the structure is wrong? Please, take a look at the array structure in my question. I don't know if maybe that's why isn't working
Catchable fatal error: Object of class CURLFile could not be converted to string in C:\Path\to\my\server\curl.php on line 144
Updated. Take a look, please :)

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.