0

I'm trying to use cURL to upload a file using PUT with a content type of application/octet-stream, but even when I set it using curl_setopt the content type is always 'text/html'. I've tested in firefox and chrome to see if it as a browser quirk or something (it wasn't) so I'm not sure what I can do about this.

         error_reporting(E_ALL);
    ini_set('display_errors', 1);
            $ch = curl_init();
            $localfile = 'upload.txt';
            $fp = fopen($localfile, 'r');
            curl_setopt($ch, CURLOPT_URL, $url.$localfile.'?type=file');
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
            curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT");
            curl_setopt($ch, CURLOPT_HEADER, true);
            curl_setopt($ch, CURLOPT_HTTPHEADER, array("content-type: application/octet-stream"));
            curl_setopt($ch, CURLOPT_UPLOAD, 1);
            curl_setopt($ch, CURLOPT_INFILE, $fp);
            curl_setopt($ch, CURLOPT_INFILESIZE, filesize($localfile));
            curl_setopt($ch, CURLOPT_USERPWD, $un.":".$pw);
            curl_setopt($ch, CURLOPT_VERBOSE, 1);
            curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
            $msg = curl_exec ($ch);
            $error_no = curl_errno($ch);
            $print = curl_getinfo($ch);
            curl_close ($ch);
            if ($error_no == 0) {
                    $error = 'File uploaded succesfully.';
            } else {
                    $error = 'File upload error.';
            }

Thanks for reading.

6
  • Try again with Content-Type instead of content-type. Maybe you need to add Content-Length too, I don't know if cURL will add this automatically.
    – Havenard
    Sep 12, 2013 at 2:06
  • @Havenard What value would I use for content length?
    – kaidash
    Sep 12, 2013 at 2:08
  • The exact length in bytes of the file you are trying to upload.
    – Havenard
    Sep 12, 2013 at 2:09
  • @Havenard I added content-length and tried the content-type capitalization, didn't work.
    – kaidash
    Sep 12, 2013 at 2:13
  • I suggest you try to perform this operation using a browser and then compare in the web server logs what cURL is trying to do and what the browser did after some difference. Perhaps some network debugging tool like Wireshark might help too.
    – Havenard
    Sep 12, 2013 at 2:15

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.