Love Fuel?
Donate
About
Forums
Discussions
Login
FuelPHP Forums
Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
General
problem with fuel curl with file upload
rklinee
August 2016
I cannot get FuelPhp Curl to upload a file with the "CURLFile" class, although it
works with regular Php. I'm running versions of Php at least 5.5.
Here is a test program I wrote:
==========================================
<?php
/**
*
@group
SampleTester
*/
class Test_SampleTester extends TestCase {
private static $SAMPLE_URL
// = '
http://URL/TO/SITE/sample.json'
;
private static $UPLOAD_FILE = "/tmp/newfile.data";
public static function setUpBeforeClass() {
// create a temporary file for upload, put some data into it
$myfile = fopen(self::$UPLOAD_FILE, "w") or die("Unable to open file!");
$data = "SOME DATA IN THE FILE";
fwrite($myfile, $data);
fclose($myfile);
}
public function test_php_curl() {
echo "\n---- test php_curl ----\n\n";
$ch = curl_init();
curl_setopt_array($ch, [
CURLOPT_URL => self::$SAMPLE_URL,
CURLOPT_POST => 1,
CURLOPT_RETURNTRANSFER => true,
]);
$post = [
'file' => new CURLFile(self::$UPLOAD_FILE, 'application/octet-stream'),
];
curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
$result = curl_exec($ch);
if ($result === false) {
echo '**************** Curl error: ' . curl_error($ch);
}
curl_close($ch);
echo $result, "\n";
}
public function test_fuel_curl() {
echo "\n---- test fuel_curl ----\n\n";
$curl = Request::forge(self::$SAMPLE_URL, 'curl');
$curl->set_method('post');
$curl->set_header('enctype', 'multipart/form-data'); // no help with this
$curl->set_params([
'file' => new CURLFile(self::$UPLOAD_FILE, 'application/octet-stream'),
]);
$result = $curl->execute();
$result_body = $result->response()->body;
echo $result_body, "\n";
}
}
==========================================
The target URL is the following REST controller:
==========================================
<?php
class Controller_Sample extends Controller_Rest {
public function post_index() {
try {
$num_uploads = count(Upload::get_files());
return $this->response([
'status' => 'ok',
'message' => "num_uploads: $num_uploads",
]);
}
catch (Exception $ex) {
return $this->response([
'status' => 'error',
'message' => $ex->getMessage(),
]);
}
}
}
==========================================
I run the test program:
php oil t group=SampleTester
And the output shows that the php curl version works, but
the FuelPhp curl version indicates that no files were uploaded.
==========================================
PHPUnit 5.1.3 by Sebastian Bergmann and contributors.
.
---- test php_curl ----
{"status":"ok","message":"num_uploads: 1"}
. 2 / 2 (100%)
---- test fuel_curl ----
{"status":"error","message":"No uploaded files were found. Did you specify \u0022enctype\u0022 in your \u0026lt;form\u0026gt; tag?"}
==========================================
I could live with the Php-curl version, but I'd love to get
the Fuel version working.
Thanks for any help.
Harro
August 2016
Accepted Answer
The upload class expect uploaded files to be in $_FILES, i.e. uploaded via a FILE input element of a HTTP POST. That error message means $_FILES was empty.
Harro
August 2016
Accepted Answer
Found your mistake:
$curl->set_params([
'form-data' =>
[
'file' => new CURLFile(self::$UPLOAD_FILE, 'application/octet-stream'),
],
]);
You need to post the file as form-data, and you don't need to set the enctype header.
Result of my test is now:
---- test fuel_curl ----
{"status":"ok","message":"num_uploads: 1"}
rklinee
September 2016
Works like a charm!
Thanks so much Harro!
Harro
September 2016
Accepted Answer
You're welcome. Please mark this question as answered.
Add a Comment
Howdy, Stranger!
It looks like you're new here. If you want to get involved, click one of these buttons!
Sign In
Apply for Membership
Categories
All Discussions
5,089
General
↳ General
3,364
↳ Job Board
13
↳ Installation & Setup
214
Packages
↳ Oil
213
↳ Orm
700
↳ Auth
261
Development
↳ Tips and Tutorials
126
↳ Code share
145
↳ Applications
52
In this Discussion
Harro
September 2016
rklinee
September 2016