什么会破坏我使用base64上传的所有文件?

问题描述 投票:0回答:1

我目前正在尝试使用laravel上传vuebase64中的特定文件格式。即使文件已成功上传并在文件文件夹中显示了文件类型的格式,它们都已损坏并且无法打开。不知道我做错了什么。下面是我的代码。

public function store(Request $request)
    {
        //
        //$this->validateRequest();



        if($request->file){
            $file = $request->file;
            $pos  = strpos($file, ';');
            $file_type = explode(':', substr($file, 0, $pos))[1]; //returns the file type
            $allowed_file_types = ['image/png', 'image/jpeg','image/jpg','application/pdf'];

            //dd(getfilesize($request->file));

           if(in_array($file_type, $allowed_file_types))
            {
                $base = base64_decode($file);
                $file_size = strlen(base64_decode($file));

                if($file_size > 2097152 ){ //2mb
                    return [ 'message' => 'File size too large'];
                }
                $ready_file = time().'.' . explode('/', explode(':', substr($file, 0, strpos($file, ';')))[1])[1];
                $destinationPath = public_path() . "/uploads/user_files/" . $ready_file;             
                file_put_contents($destinationPath, $base);

            }


        }
    }
php laravel vue.js base64
1个回答
0
投票

好像您正在尝试解码错误的内容。

$base = base64_decode($file);

[$file仍包含完整的数据URI,data:image/png;base64,ABCD…-您要解码的内容仅是ABCD…部分。

© www.soinside.com 2019 - 2024. All rights reserved.