我一直在阅读Laravel中的文件上传,阅读文档并查看最佳实践。在我的应用程序中,我有两个实例:一个文件上传区域和一个文件上传区域。
单数允许用户输入自己的文件名,而多文件上传则不允许。
上传单个文件的方法
/**
* Store a newly created resource in storage.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\Response
*/
public function store(Request $request)
{
// Get every extension that we allowed
$all_ext = implode(',', $this->allExtensions());
$this->validate($request, [
'name'=>'required|unique:file_meta_data|min:3|max:40',
'file' => 'required|file|mimes:' . $all_ext . '|max:2048'
]);
// Grab data from the request to fill the necessary fields
$name = $request->get('name');
$department = $request->get('department');
$category = $request->get('category');
$uploadedFile = $request->file('file');
// Get the extension and then use this to work out the file type
$file->size = $uploadedFile->getSize();
$extension = $uploadedFile->getClientOriginalExtension();
$type = $this->getType($extension);
// Create a new instancce of this model
$file = new FileMetaData();
$file->name = $name;
$file->department = $department;
$file->category = $category;
$file->type = $type;
$file->extension = $extension;
// Upload all the given files to Storage, within a specific directory
if ($department != '') {
$path = $file->storeAs('library/' . $department, $name);
$category = null;
} elseif ($category != '') {
$path = $file->storeAs('library/' . $category, $name);
$department = null;
}
// Grab the filepath so we can store it in the database
$file->filepath = $path;
// Finally, check that the file exists and save the model
if (Storage::exists($path)) {
$file->save();
return redirect('editable/templates-and-tools')->with('success', 'File has been added');
}
}
如您所见,它具有各种属性,然后将文件放入指定目录中的存储中。
多种上传方法
/**
* Process uploading of multiple files
*
* @param Request $request
* @return void
*/
public function bulkUpload(Request $request)
{
// Get every extension that we allowed
$all_ext = implode(',', $this->allExtensions());
// Initialize a file upload counter
$fileCount = 0;
// Get the category and department from the form
$department = $request->get('department');
$category = $request->get('category');
// Loop through each file and add it to storage
foreach ($request->file('file') as $file) {
// Get the meta data for each file
$name = $file->getClientOriginalName();
$extension = $file->getClientOriginalExtension();
$type = $this->getType($extension);
$size = $file->getSize();
// Upload all the given files to Storage, within a specific directory
if ($department != '') {
$path = $file->storeAs('library/' . $department, $name);
$category = null;
} elseif ($category != '') {
$path = $file->storeAs('library/' . $category, $name);
$department = null;
}
// Grab the filepath so we can store it in the database
$file->filepath = $path;
// Create the database entries for the newly uploaded files
FileMetaData::UpdateOrCreate([
'name' => $name,
'department' => $department,
'category' => $category,
'type' => $type,
'extension' => $extension,
'size' => $size,
'filepath' => $path
]);
$fileCount++;
}
return redirect('editable/templates-and-tools')->with('success', $fileCount . ' files have been added');
}
我正在尝试改进这两种方法,以使它们更整洁并具有更好的总体安全性。另外,在使用多文件上传功能时,您只能通过单独的表单通过编辑数据库条目来重命名文件。
我该如何改善这些方法?
也许我也应该使用更好的UI元素,例如Jquery文件上传器?