~~~
$files = request()->file();
if(empty($files['file'])){
return json(array("code"=>400,"msg"=>"上传失败,文件为空"));
}
try {
$savename = \think\facade\Filesystem::disk('public')->putFile('/other/', $files['file']);
$result = [
'code' => 200,
'msg' => "上传成功",
'url' => '/storage/' . $savename,
];
} catch (\Exception $e) {
$result = [
'code' => 400,
'msg' => $e->getMessage()
];
}
if ($result['code'] == 200){
$url:上传txt文件返回的路径
$url = root_path() .'/public'.$result['url'];
if (file_exists($url)){
打开文件
$handle = fopen($url,"rb");
获取文件内容
$content = fread($handle,filesize($url));
利用base64转视频编码
$str = base64_decode(str_replace("data:application/octet-stream;base64,","",$content));
$videoUrl:拼接生成文件路径
$videoUrl = 'storage/other/'.date('Ymd').'/'.date('YmdHis').randStr(10,'ALL').'.mp4';
生成MP4文件
file_put_contents($videoUrl,$str);
删除txt文件
unlink($url);
return json(['data'=>$videoUrl]);
}
}