drs
drs

Reputation: 1255

amazon s3 - image downloading instead of displaying in browser

This is driving me mad. I am uploading images to S3 using the php SDK. Whenever I browse to the image URL, the browser downloads the image opposed to displaying it.

I think its something to do with content type.

        // Prepare to upload the file to S3 bucket.
        $s3->create_object($bucket, $file_name, array(
                'contentType' => 'binary/octet-stream',
                'acl' => AmazonS3::ACL_PUBLIC
        ));

Can you help?

thanks

Upvotes: 37

Views: 54297

Answers (4)

hakki
hakki

Reputation: 6521

If you are working URL images use

'ContentType'  => mime_content_type($absolutePathToImage),

instead of

$_FILES['image']['type']

Upvotes: 5

drs
drs

Reputation: 1255

            $s3->create_object($bucket, $file_name, array(
                    'fileUpload' => $resized_image,
                    'contentType' => $_FILES['image']['type'],
                    'acl' => AmazonS3::ACL_PUBLIC
            ));

Upvotes: 26

MrEyes
MrEyes

Reputation: 13690

Working on your own, seemingly valid, assumption that it is the content type:

You need to set the correct content type for the image being upload, the following list contains all the most common types

* image/gif: GIF image
* image/jpeg: JPEG JFIF image
* image/png: Portable Network Graphics
* image/svg+xml: SVG vector image
* image/tiff: Tag Image File Format
* image/vnd.microsoft.icon: ICO image

So a rework of your sample code for a png upload:

// Prepare to upload the file to S3 bucket.
$s3->create_object($bucket, $file_name, array(
            'contentType' => 'image/png',
            'acl' => AmazonS3::ACL_PUBLIC
));

Upvotes: 3

jschorr
jschorr

Reputation: 3054

Your content type is wrong indeed. It needs to be image/jpeg for JPGs, for instance. See this site for a list: http://en.wikipedia.org/wiki/Internet_media_type

Upvotes: 21

Related Questions