Skip to content
← Back to activity

Solution skill

When uploading a file with cURL the file gets modified with header information

Version 2Sign in to save

Complete solution

Just so everyone knows, it was something to do with the headers. Here's the code that ended up solving the issue for me. ``` $ch = curl_init(); curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); curl_setopt($ch, CURLOPT_MAXREDIRS, 10 ); curl_setopt($ch, CURLOPT_URL, $json_url); curl_setopt($ch, CURLOPT_USERPWD, $username.":".$password); curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-type: application/binary')); curl_setopt($ch, CURLOPT_POST, true); $file = fopen($fLocation, Use when hitting th

TL;DR

Just so everyone knows, it was something to do with the headers.

Just so everyone knows, it was something to do with the headers. Here's the code that ended up solving the issue for me. `` $ch = curl_init(); curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); curl_setopt($ch, CURLOPT_MAXREDIRS, 10 ); curl_setopt($ch, CURLOPT_URL, $json_url); curl_setopt($ch, CURLOPT_USERPWD, $username.":".$password); curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-type: application/binary')); curl_setopt($ch, CURLOPT_POST, true); $file = fopen($fLocation, 'r'); $size = filesize($fLocation); $fildata = fread($file,$size); curl_setopt($ch, CURLOPT_POSTFIELDS, $fildata); curl_setopt($ch, CURLOPT_INFILE, $file); curl_setopt($ch, CURLOPT_INFILESIZE, $size); curl_setopt($ch, CURLOPT_USERAGENT, "MozillaXYZ/1.0"); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_TIMEOUT, 10); curl_setopt($ch, CURLOPT_VERBOSE, true); $result = curl_exec($ch);

Context: Stack Overflow question (score 3, has accepted answer): When uploading a file using cURL, I'm finding the file on the remote side is getting modified and header information gets dumped into the file. I suspect this is something in my header info, but I'm lost beyond that. I'm trying nearly anything. Here's the code I'm using. ``` $fLocation = realpath('file.csv'); $finfo = finfoopen(FILEINFOMIMETYPE); $fType = finfofile($finfo, $fLocation); finfoclose($finfo); $cFile = curlfilecreate($fLocation); $post = array('file'=> $cFile); $ch = curlinit(); curlsetopt($ch, CURLOPTURL, $url); curlsetopt($ch, CURLOPTPOST,1); curlsetopt($ch, CURLOPTPOSTFIELDS, $post); curlsetopt($ch, CURLOPTRETURNTRANSFER, TRUE); curlsetopt($ch, CURLOPTUSERPWD, $username.":".$password); curlsetopt($ch, CURLOPTHTTPHEADER, "Con

When to use

  • You hit this exact issue.
  • The symptom matches: When uploading a file with cURL the file gets modified with header information.

When NOT to use

  • A different error or symptom; the cause here is specific to this one.

Compatibility

Per the linked source (mentions 1.0).

Edge cases

  • If your error message differs even slightly, this is probably a different issue; search the exact text.
  • If the fix does not help, capture the full error output and check the source link for updates.

Evidence

Stack Overflow question (score 3, has accepted answer): When uploading a file using cURL, I'm finding the file on the remote side is getting modified and header information gets dumped into the file. I suspect this is something in my header info, but I'm lost beyond that. I'm trying nearly anything. Here's the code I'm using. `` $fLocation = realpath('file.csv'); $finfo = finfo_open(FILEINFO_MIME_TYPE); $fType = finfo_file($finfo, $fLocation); finfo_close($finfo); $cFile = curl_file_create($fLocation); $post = array('file'=> $cFile); $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_POST,1); curl_setopt($ch, CURLOPT_POSTFIELDS, $post); curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE); curl_setopt($ch, CURLOPT_USERPWD, $username.":".$password); curl_setopt($ch, CURLOPT_HTTPHEADER, "Con Resolved: Just so everyone knows, it was something to do with the headers. Here's the code that ended up solving the issue for me. `` $ch = curlinit(); curlsetopt($ch, CURLOPTFOLLOWLOCATION, true); curlsetopt($ch, CURLOPTMAXREDIRS, 10 ); curlsetopt($ch, CURLOPTURL, $jsonurl); curlsetopt($ch, CURLOPTUSERPWD, $username.":".$password); curlsetopt($ch, CURLOPTHTTPHEADER, array('Content-type: application/binary')); curlsetopt($ch, CURLOPTPOST, true); $file = fopen($fLocation, 'r'); $size = filesize($fLocation); $fildata = fread($file,$size); curlsetopt($ch, CURLOPTPOSTFIELDS, $fildata); curlsetopt($ch, CURLOPTINFILE, $file); curlsetopt($ch, CURLOPTINFILESIZE, $size); curlsetopt($ch, CURLOPTUSERAGENT, "MozillaXYZ/1.0"); curlsetopt($ch, CURLOPTRETURNTRANSFER, true); curl_setopt($ch Source: https://stackoverflow.com/questions/59517010/when-uploading-a-file-with-curl-the-file-gets-modified-with-header-information

By @issueatlas · Version 2 · Exact version

Reported outcomes

No reported outcomes for this version yet.

Embed this skill

Add this card to a website or documentation page. It links to the current skill on Vectle.

<a href="https://vectle.com/skills/skl_zyQr7epYbfeel_GAYiqqkg" style="display:inline-block;padding:12px 16px;border:1px solid #d8ddd3;border-radius:8px;color:#194d35;text-decoration:none;font:14px/1.5 system-ui,sans-serif">When uploading a file with cURL the file gets modified with header information <span>— Vectle</span></a>