5

According to the docs, contentType is optional and it will attempt to determine the correct mime-type based on the file extension. However, it never seemed to guess the mime-type and always defaults to application/octet-stream

Here's my code:

$s3 = new AmazonS3();

$opt = array(   'fileUpload'    => $_FILES['file']['tmp_name'],
                'storage'       => Amazons3::STORAGE_REDUCED);

$r = $s3->create_object('mybucket', $_FILES['file']['name'], $opt);

Here's a screenshot of my AWS console:

enter image description here

How do you actually automatically set the proper Content Type without setting contentType option, or do you really have to set it manually?

Additional info: If I download a file from the console (that I originally uploaded through SDK) then upload it again using the console, the proper Content-Type is set (ex: image/gif for GIF files instead of application/octet-stream)

IMB
  • 15,163
  • 19
  • 82
  • 140

2 Answers2

6

Try to add 'contentType' key to your options array when creating object and use 'body' instead of 'fileUpload'. For example:

$s3->create_object('MY_BUCKET', 'xml_file.xml', array(
                                        'body' => '<xml>Valid xml content</xml>',
                                        'contentType' => 'text/xml'
                                    ));

Successfully creates file with text/xml content type. As I know Content-Type isn't set automatically when uploading through SDK. But where's the problem to derminate mime type from PHP side and set the 'contentType' property?

vitalikaz
  • 568
  • 4
  • 13
  • 1
    Yes I can always set contentType but the thing is, the documentation say it is done automatically: *If a file is being uploaded via fileUpload as a file system path, it will attempt to determine the correct mime-type based on the file extension.* but that is not happening so there's probably a bug. – IMB Feb 10 '12 at 11:40
  • 1
    `ContentType` with capital `C`. Otherwise it doesn't work for me. – Brais Gabin Oct 08 '13 at 16:07
  • The above answer worked for me until I moved to the new SDK. I can't get it working anymore: http://stackoverflow.com/questions/21892352/specify-content-type-in-aws-phps-upload-function – Nathan H Feb 20 '14 at 09:04
1

I have just run into this situation where, when uploading some files, the Content-Type was not automatically detected. I couldn't figure out what the cause was until I stepped through the code watching the SDK execution. I discovered that the CFMimeTypes::get_mimetype() function, which is used to automatically determine the Content-Type, uses a case sensitive comparison to determine the MIME type. Because of this only lowercase extensions will match. If your file extension is upper or mixed case, then it will not match and fallback to the 'application/octet-stream' MIME type.

To fix change the line:

'fileUpload'    => $_FILES['file']['tmp_name']

to

'fileUpload'    => strtolower( $_FILES['file']['tmp_name'] )

I am using the SDK v 1.5.8.2, but there doesn't look to be any changes to this detection in the 1.5.9 or 1.5.10 releases that have both come out in the past week. I consider this to be a bug and will be filing it as such.

Brady
  • 212
  • 1
  • 8