Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Uploading files #664

Open
manix opened this issue Jan 12, 2022 · 1 comment
Open

Uploading files #664

manix opened this issue Jan 12, 2022 · 1 comment

Comments

@manix
Copy link

manix commented Jan 12, 2022

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 :)

@LukaszJaro
Copy link

LukaszJaro commented Nov 22, 2022

Thank you! I have to POST a file to a external API and was wondering how to do this the WP way, I found this https://core.trac.wordpress.org/ticket/35388

But your solution looks cleaner.

Also another similar solution here to /ticket/35388 https://gist.github.com/sirbrillig/6ab49aa6517d203a6560d75d65e0874a

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants