3

Having quite a bit of trouble PUT-ting a PDF. I've managed to get it working fine in Postman, using the code below (large code block) and appending the PDF via the body as form-data. I'm trying to replicate this in PHP now. I'm having trouble attaching the PDF though.

I've tried numerous techniques trying to attach the PDF via "CURLOPT_INFILE", "CURLOPT_POSTFIELDS" to no avail.

I create the file via:

$pdf = $_SERVER['DOCUMENT_ROOT'] . '/pdf/temp/temp.pdf';
$file = curl_file_create($pdf, 'application/pdf', 'receipt');`

or

$file = new CURLFile($pdf, 'application/pdf', 'receipt');

I've tried using:

$file = fopen($pdf, 'rb');
$file = array('file' => $file);

CURLOPT_POSTFIELDS      => $file,
CURLOPT_INFILESIZE      => $fileSize,
CURLOPT_INFILE          => $file

No luck though.

<?php

$curl = curl_init();

curl_setopt_array($curl, array(
  CURLOPT_URL => "https://staging-tallie.com/v2/enterprise/ENTERPRISEID/MyReceipt/",
  CURLOPT_RETURNTRANSFER => true,
  CURLOPT_ENCODING => "",
  CURLOPT_MAXREDIRS => 10,
  CURLOPT_TIMEOUT => 30,
  CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
  CURLOPT_CUSTOMREQUEST => "PUT",
  CURLOPT_POSTFIELDS => "-----011000010111000001101001\r\nContent-Disposition: form-data; name=\"file\"\r\n\r\n\r\n-----011000010111000001101001--",
  CURLOPT_HTTPHEADER => array(
    "accept: application/json; charset=utf-8",
    "cache-control: no-cache",
    "content-type: multipart/form-data; boundary=---011000010111000001101001",
    "token: TOKEN",
    "upload-filename: receipt.pdf"
  ),
));

$response = curl_exec($curl);
$err = curl_error($curl);

curl_close($curl);

if ($err) {
  echo "cURL Error #:" . $err;
} else {
  echo $response;
}

Error reads:

<?xml version="1.0" encoding="utf-8"?>
<ErrorResponse xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <ResponseCode>400</ResponseCode>
    <Message>Unable to Save the file to the Storage Service.</Message>
</ErrorResponse>
3
  • 2
    Does the PHP process have write access to the destination directory" Commented Nov 20, 2015 at 1:31
  • This may help: stackoverflow.com/questions/6081131/… Commented Nov 20, 2015 at 1:31
  • @DarwinvonCorax Nail on the head! Thank you sir! Commented Nov 20, 2015 at 3:00

2 Answers 2

2

400 is an HTTP response code indicating that the request was impossible to satisfy. That, along with the accompanying message text, suggest that the PHP process does not have write access to the destination directory.

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

Comments

2

This code worked for me in order to upload a file to bluemix Cloud Object Storage. File is uploaded from temporary folder after form submit using PUT method. Don't forget to validate file mime and extension before upload.

if (is_uploaded_file($_FILES['my_file']['tmp_name'])){
    $ch = curl_init();

    $url = IBM_BLUEMIX_BUCKET_END_POINT.$bucket_name."/".$file_name; // give the file a unique name


    curl_setopt($ch,CURLOPT_URL,$url);
    curl_setopt($ch,CURLOPT_PUT, true); //PUT REQUEST                
    curl_setopt($ch, CURLOPT_HTTPHEADER, array(
        'x-amz-acl: public-read', //header required for bluemix 
        'Authorization: Bearer '.$access_token, // authorization for bluemix iam
        'Content-Type: '.$conten_type, //application/pdf or image/jpg
        'Expect: '
    ));

    $image_or_file = fopen($_FILES['my_file']['tmp_name'], "rb");

    curl_setopt($ch, CURLOPT_BINARYTRANSFER, 1);
    curl_setopt($ch, CURLOPT_INFILE, $image_or_file);
    curl_setopt($ch, CURLOPT_INFILESIZE, $_FILES[$fieldName]['size']);

    curl_setopt(
        $ch,
        CURLOPT_POSTFIELDS,
        array(
          'file' =>
              '@'            . $_FILES['my_file']['tmp_name']
              . ';filename=' . $_FILES['my_file']['name']
              . ';type='     . $conten_type //application/pdf or image/jpg
    ));

    curl_setopt($ch,CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch,CURLOPT_CONNECTTIMEOUT ,16);
    curl_setopt($ch,CURLOPT_TIMEOUT, 20);
    curl_setopt($ch, CURLINFO_HEADER_OUT, true); // enable tracking

    $response = curl_exec($ch);
    $headerSent = curl_getinfo($ch ); // request headers from response (check if something wrong)

    curl_close ($ch);
    fclose($image_or_file);

    if(!$response){ // or response
       // do something...
    }
}else{
    //File did not upload, do something ...
}

1 Comment

I have been struggling with Oracle object storage and I solved it with your answer. Thank you

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.