Req #74868 [Com]: CURLFile does not allow to create an object from a stream
Edit report at https://bugs.php.net/bug.php?id=74868&edit=1
ID: 74868
Comment by: hanskrentel at yahoo dot de
Reported by: vladshpilberg at gmail dot com
Summary: CURLFile does not allow to create an object from a
stream
Status: Open
Type: Feature/Change Request
Package: cURL related
PHP Version: 7.0.21
Block user comment: N
Private report: N
New Comment:
The example given is a bit mood as @ didn't support stream resources as well.
However most stream resources do have a file-path already, so this can be wrapped - for example:
/**
* Class CURLResource
*
* CURLFile of a stream resource
*/
class CURLResource extends CURLFile
{
public function __construct(
$resource,
string $mimetype = null,
string $postname = null)
{
if (!(is_resource($resource) && 'stream' ===
get_resource_type($resource))) {
throw new InvalidArgumentException('Not a stream resource');
}
if (null === $filename = stream_get_meta_data($resource)['uri'] ?? null) {
throw new InvalidArgumentException('Incompatible stream resource, "uri"
metadata missing');
}
parent::__construct($filename, $mimetype, $postname);
}
}
Externalized it is just:
new CURLFile(stream_get_meta_data($resource)['uri']);
This might already suffice for your example. If the resource closed automatically (e.g. one from
tmpfile()), the resource handle has to be kept until the file has been send.
You can also post directly from a stream with curl, just not AFAIK for post form data encoded file
uploads, but for the whole request body, see CURLOPT_INFILE,
For more options, just properly wrap . It should bring the (imagined) headaches to a minimum.
But with all the issues of hidden state a stream resource can come with, I wonder if it is really
useful to have support for that in the extension itself. Handling of temporary files is much more
straight forward, I wonder bit what the headaches are. Just my 2 cents.
Previous Comments:
------------------------------------------------------------------------
[2017-07-06 18:59:51] vladshpilberg at gmail dot com
Description:
------------
---
From manual page: http://www.php.net/class.curlfile
---
CURLFile does not allow to create an object from a stream, it always requires file to be saved on a
server first, which is not very convenient. It definitely reads file to a stream somewhere under
hood, why not allow to create it from a stream?
I, for example, have a task of reading file from AWS S3 and passing it to some API. I can do it
'old way' using @, but it's deprecated now and I have to save it to temp location on
my server first, which gives me an unnecessary headache of storage management.
------------------------------------------------------------------------
--
Edit this bug report at https://bugs.php.net/bug.php?id=74868&edit=1
Thread (5 messages)