Love Fuel?    Donate

FuelPHP Forums

Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
Can i use Fuel Request (Curl) for uploading a file to a remote server?
  • Hi,

    i whould ask if it is possible to use the Fuel Request Class for uploading files to a remote server.

    I mean i have

    $curl = Request::forge(App::$api_url.'updateProfile?access_key='.App::$api_access_key, 'curl');
    $curl->set_method('POST');

    ...

    $curl->set_params($params);
    $curl->execute();


    and on the remote script

    echo "<pre>";
    print_r($_FILES);

    bit $_FILES is empty

    any ideas?

    greetings

    sorry for my bad english :|
  • Yes, that is possible, but you need to specify the correct params, which depend on the PHP version.

    If you are on PHP 5.3 or 5.4, you need to use the "@" prefix:

    $params = array(
        'filename' => '@/tmp/file/to/upload.txt',
    );

    if you are on PHP 5.5 or newer, you need to use a CurlFile object:


    $params = array(
        'filename' => new CURLFile('/tmp/file/to/upload.txt','plain/text','testfile'),
    );

  • Dont know this does not work

    $params['filename'] = new CurlFile(DOCROOT.'files/9fd96aaa611e6fd0581b63267ab6a39e.jpg', 'image/jpeg',  'file');
            

    but $_files (as i said) is empty

    But it is in the $_POST Array

     [filename] => Array
    (
    [name] => files/9fd96aaa611e6fd0581b63267ab6a39e.jpg
    [mime] => image/jpeg
    [postname] => 9fd96aaa611e6fd0581b63267ab6a39e.jpg
    )
  • It looks like you need to set the option "RETURNTRANSFER" to 1 before this will work.
  • what?

    your request curl class set CURLOPT_RETURNTRANSFER automaticly to true!?!

    if ( ! isset($this->options[CURLOPT_RETURNTRANSFER]))
    {
    $this->options[CURLOPT_RETURNTRANSFER] = true;
    }

    but same result if i set this. $_FILES is empty :(

    can it be that there is an error with encoding?

    when i debug php://input i get


    &filename%5Bname%5D=E%3A%5Cxampp%5Chtdocs%5Cpaychat%5Cpublic%5Cfiles%2F9fd96aaa611e6fd0581b63267ab6a39e.jpg&filename%5Bmime%5D=image%2Fjpeg&filename%5Bpostname%5D=9fd96aaa611e6fd0581b63267ab6a39e.jpg
  • Ah, ok. I didn't write the code, and I've never used it to upload a file, so I am trying to figure out what to do based on docs I can find.

    I'll have to see when I can find the time to setup a small test.

    As far as I can see, the curl class doesn't do anything special other than encapsulating the curl methods, so I can't immediately see why it would behave differently from plain curl code.

    btw, have you tried that, ueing something like http://php.net/manual/en/class.curlfile.php#115569 ?
  • yes but now changes, :(

    ok i have found the issue

    first i must use

    $curl->set_option(CURLOPT_HTTPHEADER, array(
            'Content-Type: multipart/form-data'
        ));

    than CURLOPT_POSTFIELDS must be an array not a string

    can you change this in a new version of fuelphp?
  • There is nothing to change.

    The option is optional, as the name implies. And parameters must always be passed as an array, see http://docs.fuelphp.com/classes/request/curl.html#/method_set_params. We can't force it to be an array, because the params are used for all HTTP methods, and sometimes, even with POST, it should not be an array.

    curl isn't my thing, otherwise I would have spotted the fact you didn't pass it as an array earlier...

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

In this Discussion