0

I am trying to upload a file to BrickFTP using their API with the PHP cURL library. I am able to upload the file but the content disposition headers end up at the top of the uploaded file. (BrickFTP uploads to Amazon S3). My question is very similar to this question but I wasn't able to convert that answer PHP.

// Authenticate with Session
$ch = curl_init();
$postData = json_encode([
   'username' => 'xxx',
   'password' => 'xxx'
]);
curl_setopt($ch, CURLOPT_URL, "https://SUBDOMAIN.brickftp.com/api/rest/v1/sessions.json");
curl_setopt($ch, CURLOPT_HTTPHEADER, ["Content-Type: application/json", "Accept: application/json"]);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postData);
curl_setopt($ch, CURLOPT_VERBOSE, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);

$result = curl_exec($ch);
$error = curl_error($ch);

$result = json_decode($result);
$apiID = $result->id;

// Step 1: Get the upload URI
$remotePath = 'https://SUBDOMAIN.brickftp.com/api/rest/v1/files/' . basename($this->remotePath);
$postData = json_encode([
   'action' => 'put',
]);
curl_setopt($ch, CURLOPT_URL, $remotePath);
curl_setopt($ch, CURLOPT_COOKIE, "BrickAPI={$apiID}");
curl_setopt($ch, CURLOPT_POSTFIELDS, $postData);

$result = curl_exec($ch);
$error = curl_error($ch);

$result = json_decode($result);
$headers = empty($result->headers) ? 0 : $result->headers;

// Step 2: Upload the file
$fileSize = filesize($this->localPath);
$finfo = finfo_open(FILEINFO_MIME_TYPE);
$finfo = finfo_file($finfo, $this->localPath);
$cFile = new CURLFile($this->localPath, $finfo, basename($this->localPath));
$postData = ['file' => $cFile];
curl_setopt($ch, CURLOPT_URL, $result->upload_uri);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postData);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT");
curl_setopt($ch, CURLOPT_HEADER, $headers);
curl_setopt($ch, CURLOPT_HTTPHEADER, ["Content-Length: {$fileSize}", "Content-Type: text/plain"]);
curl_setopt($ch, CURLOPT_INFILESIZE, $fileSize);

$result = curl_exec($ch);
$error = curl_error($ch);

// Step 3: Complete the request
...

The file gets uploaded successfully, but with this added to the top:

--------------------------af8ccf12ca11d3f7
Content-Disposition: form-data; name="0"; filename="my-file.csv"
Content-Type: text/plain

At first I had Content-type set to multipart/form-data but Googling around, I read that some people fixed the issue by setting the Content-type to text/plain; that didn't work for me. I also tried using just @/filepath/my-file.csv as the value for file in $postData but that resulted in an HTTP/1.1 400 Bad Request.

Morgan
  • 1,280
  • 1
  • 14
  • 15

1 Answers1

0

So it looks like I just needed to send in the data as a string like so:

$postData = file_get_contents($this->localPath);

Maybe a duplicate of: PHP Curl post a file without header

Morgan
  • 1,280
  • 1
  • 14
  • 15