Laravel S3文件上传MimeType问题 [英] Laravel S3 File Upload MimeType Issue

查看:242
本文介绍了Laravel S3文件上传MimeType问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个问题,一个CSS文件正在从S3通过CDN的错误mimetype提供服务。

I'm having a problem with a CSS file being served under the wrong mimetype from S3 via a CDN.

我已经注意到了。如果我通过我的网站上传文件,CSS显示在CDN网址,但不会被网站使用(不呈现布局),因为它来了:

Heres what I've noticed. If I upload the file via my website, the CSS shows at the CDN url, but will not be used by the website (doesn't render the layout) because its coming up as:

资源解释为样式表,但以MIME类型text / x-asm传输

这是我在Laravel中的上传代码:

This is my upload code in Laravel:

public function upload(Request $request)
{
    $files = $request->file('asset');

    $directoryname = Auth::user()->assetdirectory;
    $userId = Auth::user()->id;
    $userAssets = Auth::user()->assets;
    $file_count = count($files);
    $uploadcount = 0;       
    foreach($files as $file)
    {
        $data = Input::all();
        $rules = array 
        (
            'asset' => 'required'       
            );
        $messages = array
        (
            'asset.mimes' => 'That file type is not allowed.  Please try a .css or .js file type.',
            'asset.required' => 'A file is required.'
            );
        $validator = Validator::make($data, $rules, $messages);
        if($validator->passes())
        {   
            if ($file == null)
            {
                return redirect('upload')->with('errornotice', 'A file is required.');
            }       

            $filename = $file->getClientOriginalName();
            $fileext = $file->getClientOriginalExtension();
            $filesize = $file->getClientSize();
            $mime = $file->getMimeType();
            foreach($userAssets as $asset)
            { 
                if($asset->name == $filename)
                { 
                    return redirect('upload')->with('errornotice', 'A file with that name already exist');
                } 
            }           
            if($fileext != "css" && $fileext != "js")
            {
                return redirect('upload')->with('errornotice', 'That file type is not allowed.  Please try a .css or .js file type.');
            }   
            $contents = file_get_contents($file->getRealPath());
            Storage::put($directoryname . "/" . $filename, $contents);
            $asset = new Asset;
            $asset->name = $filename;
            $asset->user_id = $userId;
            $asset->size = $filesize;
            $asset->extension = $fileext;
            if($fileext == "css")
            {
                $asset->url   = ('<link rel="stylesheet" href="' . 'https://cdn.xxx.io/' . $directoryname . "/" . $filename . '">');
            } else if($fileext == "js")
            {
                $asset->url   = ('<script src="' . 'https://cdn.xxx.io/' . $directoryname . "/" . $filename . '">' . '</script>');
            }
            $asset->save();
            $uploadcount ++;
        }       
    }
    if($uploadcount == $file_count){
        return redirect('dashboard')->with('notice', 'Your files have been added.');
    } 
    else {
        return Redirect::to('upload')->withInput()->withErrors($validator);
    }       

}

我可以得到mimetype $ mime = $ file-> getMimeType(); 关闭文件,但我没有线索如何发送它与上传到s3 Storage :: put($ directoryname。/。$ filename,$ contents);

I can get the mimetype $mime = $file->getMimeType(); off the file, but I don't have a clue how to send it along with the upload to s3 Storage::put($directoryname . "/" . $filename, $contents);

推荐答案

p>您可以通过可选的 $ config 参数来告诉S3您的文件的MIME类型:

You can tell S3 the MIME type of your file by the optional $config parameter:

Storage::put($directoryname . "/" . $filename, $contents, ['mimetype' => $mime]);

css文件的mime类型是 text / css

The mime type of css files is text/css.

这篇关于Laravel S3文件上传MimeType问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆