Skip to content

Conversation

Irker
Copy link
Contributor

@Irker Irker commented May 16, 2015

Add possible to upload files from string, not from disc.
New function curl_buffer_file_create, similar for curl_file_create, but accept other arguments:
$buffer - file content, $post_filename and $mime_type.
Also CURLFile constructor now haven't required arguments. So we can fill file object later:

$ch = curl_init('http://localhost:8000/server.php');

$file = generate_some_special_image_file(); // just for example, it may be autogenerated pdf, image or something special in memory

$cfile = new CURLFile();
$cfile->setBuffer($file);
$cfile->setPostFilename('test_name.jpg');

$data = array('test_file' => $cfile);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);

$result = curl_exec($ch);

For BC I save unserialization restriction: if $filename contain string - we cannot unserialize object as before this pull-request. If we have object with buffer unserialization accepted.

If object have $filename we use it and ignore buffer.
$buffer can be empty.

@Kubo2
Copy link
Contributor

Kubo2 commented May 23, 2015

@Irker 👍
Does setters return $this?

@Irker
Copy link
Contributor Author

Irker commented May 25, 2015

Hi!
@Kubo2 No, I didn't change methods behavior. I tried to change as less as possible to force pull-request be applied sooner

@@ -29,35 +29,41 @@

PHP_CURL_API zend_class_entry *curl_CURLFile_class;

static void curlfile_ctor(INTERNAL_FUNCTION_PARAMETERS)
static void curlfile_ctor(char *fname, size_t fname_len, char *mime, size_t mime_len, char *postname, size_t postname_len, char *buffer, size_t buffer_len, INTERNAL_FUNCTION_PARAMETERS)
{
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why you need INTERNAL_FUNCTION_PARAMETERS? You don't seem to use them except for return_value. So maybe just pass return_value?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you, will change it.

if (Z_TYPE_P(prop) == IS_STRING && Z_STRLEN_P(prop) > 0) {
filename = Z_STRVAL_P(prop);
prop = zend_read_property(curl_CURLFile_class, current, "name", sizeof("name")-1, 0, &rv);
if (zval_is_true(prop)) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am not sure I understand why zval_is_true check? If you wanted to check for name being empty/null, then it should be that check. Otherwise you'll get in trouble with "0" etc.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@smalyshev , can it be something like if (!Z_ISNULL_P(prop) && (Z_TYPE_P(prop) != IS_STRING || Z_STRLEN_P(prop) > 0)) { ? Not sure if prop can be ISUNDEF after zend_read_property. Can you correct me, before I will push this change?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure it can be UNDEF, but I'd rather specify explicitly which values we do expect - i.e. if we expect name to be NULL when we use buffer.

Also, whatever check you do here you should also do on __wakeup. In fact, I'd make it a macro or inline function to ensure they are always the same.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we will use NULL as base value for this condition, we must change this code: zend_declare_property_string(curl_CURLFile_class, "name", sizeof("name")-1, "", ZEND_ACC_PUBLIC); to zend_declare_property_null. This is small but BC break =(, because we change default value for property.

Previous variant of pull-request with separate CURLBufferFile seems now less terrible )))

@Irker
Copy link
Contributor Author

Irker commented Jul 1, 2016

Not interested for the community

@Irker Irker closed this Jul 1, 2016
@Irker
Copy link
Contributor Author

Irker commented Dec 31, 2017

@smalyshev is any chance to implement sending files from buffer string?
I can try to create new PR, but all tries broken by current realisation:

  1. If I tried to separate feature to another class we have many duplicates codes
  2. If I tried to add feature to current class we have many logic problems discussed in this PR.
  3. If I tried to create two new immutable classes we have too big BC and need support new and old ways. (Same as previous "@filename" option) .
    This feature require only few strings of code for CURL library, and very usefull for production.

@smalyshev
Copy link
Contributor

@Irker I'll look into it.

@Irker
Copy link
Contributor Author

Irker commented May 14, 2018

@smalyshev I do not want to disturb, but I think it's time to remind.

@Irker Irker deleted the curl_buffer_file branch August 31, 2018 10:36
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants