1

I have a form with a file upload field which POSTs to an endpoint of mine. From there, I'd like to POST the file to an API I use.

What's the best way of achieving this?

Do I have to move the file from temporary storage in order to POST it to the API? Or maybe I have to add cURL options?

HTML:

<form method="post" action="/my_endpoint">
    <div class="form-group">
        <label for="resume">Resume*</label>
        <input type="file" class="form-control" id="resume" name="resume" placeholder="Resume" required />
    </div>
    <button type="submit" class="btn btn-default">Submit</button>
</form>

PHP:

$f3->route('POST /my_endpoint',
    function($f3) {
        $url = API_ENDPOINT;
        $post_params = $f3->get('POST');
        $files_params = $f3->get('FILES');

        $fields = array(
            'id' => $post_params['id'],
            'email' => $post_params['email'],
            'resume' => '@'.$files_params['resume']['tmp_name']
        );

        foreach($fields as $key=>$value) { $fields_string .= $key.'='.$value.'&'; }
        rtrim($fields_string, '&');

        $ch = curl_init();
        curl_setopt($ch,CURLOPT_URL, $url);
        curl_setopt($ch,CURLOPT_HTTPHEADER, array("Content-Type:multipart/form-data"));
        curl_setopt($ch,CURLOPT_POST, count($fields));
        curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
        curl_setopt($ch,CURLOPT_POSTFIELDS, $fields_string);
        $result = curl_exec($ch);
        curl_close($ch);
    }
);
conbask
  • 9,741
  • 16
  • 57
  • 94
  • no. you only move the file out of its temp directory if you awnt to keep it - php will leave it alone for the lifetime of the script handling the upload. if you don't take steps to preserve the file (e.g. move it), then php will auto-delete it when the script exits. that means for your upload, uploading from the temp directory is "fine". – Marc B Feb 05 '16 at 21:32
  • Duplicate: http://stackoverflow.com/questions/15200632/how-to-upload-file-using-curl-with-php – peixotorms Feb 05 '16 at 21:32
  • 1
    BTW, the `@filename` method of posting files is deprecated. You should use http://php.net/manual/en/class.curlfile.php now. – Barmar Feb 05 '16 at 22:47

1 Answers1

0

I took @Barmar's advice and created a curlfile.

$f3->route('POST /my_endpoint',
    function($f3) {
        $url = API_ENDPOINT;
        $post_params = $f3->get('POST');
        $files_params = $f3->get('FILES');

        $resume_file = curl_file_create(realpath($files_params['resume']['tmp_name']),$files_params['resume']['type'],$files_params['resume']['name']);

        $fields = array(
            'id' => $post_params['id'],
            'email' => $post_params['email'],
            'resume' => $resume_file
        );

        $ch = curl_init();
        curl_setopt($ch,CURLOPT_URL, $url);
        curl_setopt($ch,CURLOPT_HTTPHEADER, array("Content-Type:multipart/form-data"));
        curl_setopt($ch,CURLOPT_POST, count($fields));
        curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
        curl_setopt($ch,CURLOPT_POSTFIELDS, $fields);
        $result = curl_exec($ch);
        curl_close($ch);
    }
);

And then it worked.

conbask
  • 9,741
  • 16
  • 57
  • 94
  • FYI, it should be possible to achieve the same with the `Web` class coming with the framework. See [this example](http://fatfreeframework.com/web#PUT). – xfra35 Feb 06 '16 at 13:33
  • Thanks. I wanted to POST other data within the same call. Can the web class be used to do that as well? – conbask Feb 07 '16 at 09:43
  • You're right, this is not possible at the moment. At least, not easily. But that could be a nice addition to the framework. – xfra35 Feb 08 '16 at 10:57