Skip to content

Commit 72a808b

Browse files
author
Nate Good
committed
Merge pull request #149 from bpedro/feature/attach-with-mime-type
Discover and include mime type when attaching files.
2 parents 6c0d64a + 9832395 commit 72a808b

File tree

3 files changed

+8
-5
lines changed

3 files changed

+8
-5
lines changed

src/Httpful/Request.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -392,11 +392,13 @@ public function expectsType($mime)
392392

393393
public function attach($files)
394394
{
395+
$finfo = finfo_open(FILEINFO_MIME_TYPE);
395396
foreach ($files as $key => $file) {
397+
$mimeType = finfo_file($finfo, $file);
396398
if (function_exists('curl_file_create')) {
397-
$this->payload[$key] = curl_file_create($file);
399+
$this->payload[$key] = curl_file_create($file, $mimeType);
398400
} else {
399-
$this->payload[$key] = "@{$file}";
401+
$this->payload[$key] = '@' . $file . ';type=' . $mimeType;
400402
}
401403
}
402404
$this->sendsType(Mime::UPLOAD);

tests/Httpful/HttpfulTest.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -322,13 +322,14 @@ function testParsingContentTypeUpload()
322322

323323
function testAttach() {
324324
$req = Request::init();
325-
326-
$req->attach(array('index' => '/dir/filename'));
325+
$testsPath = realpath(dirname(__FILE__) . DIRECTORY_SEPARATOR . '..');
326+
$filename = $testsPath . DIRECTORY_SEPARATOR . 'test_image.jpg';
327+
$req->attach(array('index' => $filename));
327328
$payload = $req->payload['index'];
328329
// PHP 5.5 + will take advantage of CURLFile while previous
329330
// versions just use the string syntax
330331
if (is_string($payload)) {
331-
$this->assertEquals($payload, '@/dir/filename');
332+
$this->assertEquals($payload, '@' . $filename . ';type=image/jpeg');
332333
} else {
333334
$this->assertInstanceOf('CURLFile', $payload);
334335
}

tests/test_image.jpg

51.4 KB
Loading

0 commit comments

Comments
 (0)