开发者

amazon s3 - image downloading instead of displaying in browser

开发者 https://www.devze.com 2023-02-05 14:44 出处:网络
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.

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 开发者_StackOverflow社区you help?

thanks


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


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


If you are working URL images use

'ContentType'  => mime_content_type($absolutePathToImage),

instead of

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


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
));
0

精彩评论

暂无评论...
验证码 换一张
取 消