Skip to content

Uploading files #664

@manix

Description

@manix

As I see file uploads are still not possible, even though the fix was suggested some 4 years ago.

Currently I needed to post files and I wasn't in the mood for rewriting stuff in my app, so this is what I came up with:

$hooks = new Requests_Hooks();
$hooks->register('curl.before_send', function ($ch) {
  if (function_exists('curl_file_create')) { // php 5.5+
    $cFile = curl_file_create($_FILES['uploadFile']['tmp_name'], '', $_FILES['uploadFile']['name']);
  } else { // 
    $cFile = '@' . realpath($_FILES['uploadFile']['tmp_name']);
  }
  $_POST['uploadFile'] = $cFile;
  
  curl_setopt($ch, CURLOPT_POSTFIELDS, $_POST);
});
$resp = Requests::post($url, $headers, [], $options);

Just sharing in case it helps someone.

Close when #313 is ready :)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions