Laravel S3文件上传MimeType问题

时间:2015-07-01 12:40:05

标签: php css laravel amazon-s3 laravel-5

我有一个问题,一个CSS文件是通过CDN在S3的错误mimetype下提供的。

这是我注意到的。如果我通过我的网站上传文件,CSS会显示在CDN网址上,但不会被网站使用(不会渲染布局),因为它会显示为:

Resource interpreted as Stylesheet but transferred with MIME type text/x-asm

如果我直接进入S3存储桶并直接上传到该文件夹​​,该文件将按原样呈现CSS,并且网站布局应该如此。

这是我在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);

1 个答案:

答案 0 :(得分:1)

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

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

mss类型的css文件是text/css