3

I had been using the following script without any problems until today. I changed my linux's distro,and I don't know if it is related or not

     $full = realpath($newPath."/".$image);
     $data = array('name' => $name, 'pic' => '@'.$full.";type=image/jpeg");
     $ch = curl_init();
     curl_setopt($ch, CURLOPT_VERBOSE, 1);
     curl_setopt($ch, CURLOPT_POST, 1);
     curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type: multipart/form-data"));
     curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
     curl_setopt($ch, CURLOPT_URL, $url);
     curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
     curl_setopt($ch, CURLOPT_COOKIEFILE, 'cookie.txt');
     curl_setopt($ch, CURLOPT_COOKIEJAR, 'cookie.txt');
     curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
     $contents = curl_exec($ch);

I am posting the info through a php's form, but I cannot submit the image due to that it isn't detected as an image, it is detected as "application/octet-stream"

How can I specify the content-type as an image and submit the text too?

P.D. I checked the similar questions asked before mine, I just found one with the exact purpouse and it hadn't any reply. I am not trying to download any image, I am trying to fill a form with several text fields and I should upload an image through the same form

4
  • No, it isn't a duplicate due to that my purpouse is different from that. I am trying to upload an image plus text and that guy is trying to download @MaxZoom Commented Aug 12, 2016 at 16:49
  • Which versions of PHP did you use? (old one and new one) Commented Aug 12, 2016 at 17:00
  • Uhm, I am using PHP 7.0.9 (cli) (built: Jul 20 2016 17:12:28) ( NTS ) Copyright (c) 1997-2016 The PHP Group Zend Engine v3.0.0, Copyright (c) 1998-2016 Zend Technologies, I know that I tested the script with php v5, may be that the cause of it doesn't work properly... not? @Dekel Commented Aug 12, 2016 at 19:14
  • I am having a similar issue, with nearly identical cURL options, after upgrading from PHP 5.4 to 7.0. The $_FILES superglobal is empty on the receiving end, with no error. Commented Sep 3, 2016 at 0:17

1 Answer 1

4

Okay, so I found the issue. You'll need to use the CurlFile() class, instead of the '@' . $file_name example.

Docs: http://php.net/manual/en/class.curlfile.php

Instead of

$postfields['file_contents'] = '@' . $filename;

You'll want to do this

$postfields['file_contents'] = new CurlFile($filename);

You can look at the docs for specifying mime type and name on the other end in the constructor, but that's the basic gist of it.

Best of luck!

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

2 Comments

I should add that this method should be used for PHP 5.5+ (including PHP 7)!
Thanks for your answer! It helped me! :)

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.