laravel-admin文件上传及处理
- file表单通过move指定上传目录
- saving保存回调设置文件名及文件校验参数
- saved保存回调实现后期处理,如文件重命名等
$form->hidden('file_name', '更新文件名');
$form->file('file_path', '更新文件')->move(function ($file_path) use ($form) {
if (!empty($form->app_id)) {
Log::info('Move App ID: '. $form->app_id);
$app = AndroidApp::where('id', $form->app_id)->get(['name'])->first();
Session::put('app', $app);
} else {
$app = Session::get('app');
}
return 'files/' . $app->name;
});
$form->hidden('size', 'Size');
$form->hidden('md5', 'Md5');
$form->hidden('sha1', 'Sha1');
$form->saving(function ($form) {
$file = $form->file_path;
if ($file != null && $file->isValid()) {
Log::info('App Id: ' . $form->app_id);
Log::info('Temp File: ' . $file);
Log::info('Original Name: ' . $file->getClientOriginalName());
$file_name = $file->getClientOriginalName();
$form->file_name = $file_name;
$form->size = filesize($file);
$form->md5 = md5_file($file);
$form->sha1 = sha1_file($file);
}
});
$form->saved(function ($form) {
$app_version = $form->model();
//$app = AndroidApp::where('id', $app_version->app_id)->get(['name'])->first();
$app = $app_version->app;
Session::put('app', $app);
if ($form->file_path != null && $form->file_path->isValid()) {
$extension = substr($form->file_name, strrpos($form->file_name, '.') + 1);
if ($extension === 'apk' || $extension === 'patch' || $extension === 'tpatch') {
$extension = 'zip';
}
$file_name = substr($form->file_name, 0, strrpos($form->file_name, '.') + 1) . $extension;
$old_file = public_path('upload/') . $app_version->file_path;
$new_file = public_path('upload/files/') . $app->name . '/' . $file_name;
Log::info('Old File: ' . $old_file);
Log::info('New File: ' . $new_file);
//上传同名文件会导致md5自动命名,后期重命名回来
if (file_exists($old_file)) {
if (rename($old_file, $new_file)) {
Log::info('Rename Success');
$app_version['file_path'] = 'files/' . $app->name . '/' . $file_name;
$app_version->saveOrFail();
} else {
Log::info('Rename Failure');
}
}
}
});
版权声明:
作者:Joe.Ye
链接:https://www.appblog.cn/index.php/2023/02/26/upload-and-process-of-laravel-admin-files/
来源:APP全栈技术分享
文章版权归作者所有,未经允许请勿转载。
THE END
0
二维码
打赏
海报
laravel-admin文件上传及处理
file表单通过move指定上传目录
saving保存回调设置文件名及文件校验参数
saved保存回调实现后期处理,如文件重命名等
$form->hidden('file_name……
文章目录
关闭
共有 0 条评论