Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Discover and include mime type when attaching files. #149

Merged
merged 6 commits into from
Dec 19, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions src/Httpful/Request.php
Original file line number Diff line number Diff line change
Expand Up @@ -392,11 +392,13 @@ public function expectsType($mime)

public function attach($files)
{
$finfo = finfo_open(FILEINFO_MIME_TYPE);
foreach ($files as $key => $file) {
$mimeType = finfo_file($finfo, $file);
if (function_exists('curl_file_create')) {
$this->payload[$key] = curl_file_create($file);
$this->payload[$key] = curl_file_create($file, $mimeType);
} else {
$this->payload[$key] = "@{$file}";
$this->payload[$key] = '@' . $file . ';type=' . $mimeType;
}
}
$this->sendsType(Mime::UPLOAD);
Expand Down
7 changes: 4 additions & 3 deletions tests/Httpful/HttpfulTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -322,13 +322,14 @@ function testParsingContentTypeUpload()

function testAttach() {
$req = Request::init();

$req->attach(array('index' => '/dir/filename'));
$testsPath = realpath(dirname(__FILE__) . DIRECTORY_SEPARATOR . '..');
$filename = $testsPath . DIRECTORY_SEPARATOR . 'test_image.jpg';
$req->attach(array('index' => $filename));
$payload = $req->payload['index'];
// PHP 5.5 + will take advantage of CURLFile while previous
// versions just use the string syntax
if (is_string($payload)) {
$this->assertEquals($payload, '@/dir/filename');
$this->assertEquals($payload, '@' . $filename . ';type=image/jpeg');
} else {
$this->assertInstanceOf('CURLFile', $payload);
}
Expand Down
Binary file added tests/test_image.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.