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

Add ability to pass in a file pointer #34

Open
sidchilling opened this issue Nov 30, 2012 · 14 comments · May be fixed by #190
Open

Add ability to pass in a file pointer #34

sidchilling opened this issue Nov 30, 2012 · 14 comments · May be fixed by #190

Comments

@sidchilling
Copy link

How to post file data / upload file using requests? I can;t find any documentation on this.

Like in HttpRequest, we have a method called addPostFile()

@rmccue
Copy link
Collaborator

rmccue commented Dec 1, 2012

There's no native way to pass files in, you'll need to pass a string in via the data parameter. See the making a POST request section of the docs.

@rmccue rmccue closed this as completed Dec 1, 2012
@cviebrock
Copy link

I'll just add (while writing code that needs to post 20 meg CSV files to a remote server), that it would be nice if the data parameter accepted a file pointer resource -- or maybe better, an SPLFileObject -- and then used the contents of that for the request body.

Otherwise, you need to load the whole file into memory in order to pass it as the request.

@rmccue rmccue reopened this Mar 12, 2013
@rmccue
Copy link
Collaborator

rmccue commented Mar 12, 2013

Reopened, as that is a good point. Whether that's technically possible in the transports, I'm not sure.

In terms of API design, I think ideally:

  1. If data is a string, we simply pass the string as file data.
  2. If data is a file resource, we use fread and family
  3. If data is a SplFileObject, we use $data->current() in a loop

I'm not sure, but I feel like we should be able to handle cases 2 and 3 in the same way. It doesn't seem like fread accepts an SplFileObject unfortunately, so we probably have to go in the opposite direction. I'd prefer to work with bytes, but it looks like SplFileObject works line-by-line.

@rmccue
Copy link
Collaborator

rmccue commented Mar 12, 2013

Based on PHP bug #36289, it looks like SplFileObject is unsuitable for this. I'd prefer to write a file-like interface and wrap file resources suitably instead.

@cviebrock
Copy link

Hrm, I never noticed that SplFileObject wasn't useful for binary files .. good to know.

As for the actual upload, would it not be something like this pseudocoe (at least as far as the cURL transport is concerned):

if ( get_resource_type($data) == 'stream' ) {
    curl_setopt($ch, CURLOPT_INFILE, $data);
    // kludge to get size of file when only given the resource
    fseek ( $data, 0, SEEK_END );
    $size = ftell($data);
    curl_setopt($ch, CURLOPT_INFILESIZE, $size );
    rewind($data);
}

@gabrielkoerich
Copy link

Anything new about this? I need to upload a file via post as well.

@motdotla
Copy link

motdotla commented Nov 1, 2013

I'm also interested in this.

@sascha-egerer
Copy link

Any progress here?

@masiorama
Copy link

Not yet available in the last build. I'm interested in it too.

@captn3m0
Copy link

captn3m0 commented Sep 9, 2015

PHP 5.5 now has CurlFile support: https://secure.php.net/manual/en/class.curlfile.php

@rmccue rmccue mentioned this issue Oct 2, 2015
This was referenced Oct 26, 2015
@jmcbee
Copy link

jmcbee commented Mar 5, 2016

Anyone have a fork making this work?

@captn3m0
Copy link

captn3m0 commented Mar 5, 2016

I just shifted to guzzle, which is doing really well in terms of development.

@jmcbee
Copy link

jmcbee commented Mar 6, 2016

Yeah, but the main selling point of this library was 5.2 compatibility (not my decision!) which Guzzle doesn't offer.

@soulseekah
Copy link
Contributor

Related #289

@jrfnl jrfnl modified the milestones: 1.8.x Next, 2.0.0 Apr 27, 2021
@jrfnl jrfnl removed this from the 2.0.0 milestone Nov 5, 2021
@jrfnl jrfnl added this to the 2.1.0 milestone Nov 5, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.