Skip to content

Commit

Permalink
Merge pull request #149 from bpedro/feature/attach-with-mime-type
Browse files Browse the repository at this point in the history
Discover and include mime type when attaching files.
  • Loading branch information
Nate Good committed Dec 19, 2014
2 parents 6c0d64a + 9832395 commit 72a808b
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 5 deletions.
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.

0 comments on commit 72a808b

Please sign in to comment.