我目前正在尝试使用laravel
上传vue
和base64
中的特定文件格式。即使文件已成功上传并在文件文件夹中显示了文件类型的格式,它们都已损坏并且无法打开。不知道我做错了什么。下面是我的代码。
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);
}
}
}
好像您正在尝试解码错误的内容。
$base = base64_decode($file);
[$file
仍包含完整的数据URI,data:image/png;base64,ABCD…
-您要解码的内容仅是ABCD…
部分。