Laravel 下载数据库中包含两个表的文件
Laravel download files with two tables in database
我希望我已经接近最后阶段了。我将文件上传到 storage/files/
并使用 id_message
为每个上传文件创建 uniq 文件夹没有问题并将文件数据存储在 table files
文件的最终路径是 /storage/files/{id_message}/{file_name}
两个变量 id_message
和 file_name
都在 table files
.
FileController
函数 fileUpload
:
function fileUpload(Request $request)
{
$request->validate([
'id_message' => 'required|min:6'
]);
$idParameter = $request->id_message=$request->id_message;
$result=$request->file('file_path')->store('files/'.$idParameter);
$file = new File;
$file->id_message=$idParameter;
$file->file_path=$result;
$file->file_name=$request->file('file_path')->getClientOriginalName();
$file->save();
上传后 table files
:
id id_message file_name
1 000001 Myfile.zip
/storage/app/files/000001/Myfile.zip
FileController
: getDownload
public function getDownload($id)
{
$resultFile = DB::table('files')->where('id_message',$id)->first();
$attachment = store_path().'/' . $resultFile->id_message . '/' . $resultFile->file_name;
return response()->download($attachment);
}
route
Route::get('download/{id}/{fileName}', 'FileController@getDownload')->name('downloadFile');
view.blade
<td><a href="{{ route ('downloadFile', $resultFile->file_name)}}">Download</a></td>
error
Undefined variable:resultFile
我是否接近于 laravel 最终下载文件?
controller for view
table users
public function postLogin(Request $request)
{
request()->validate([
'id_message' => 'required',
'sms_code' => 'required',
]);
$credentials = $request->only('id_message', 'sms_code');
$request->session()->flash('success','');
if ($user=User::where($credentials)->first()) {
auth()->login($user);
return redirect()->intended('dashboard')->with('success','');
}
return Redirect::to("login")->with(['url_attribute' => $url_attribute,'id_message' => $id_message])->with('error','');
}
public function dashboard()
{
if(Auth::check())
{
return view('dashboard');
}
return Redirect::to("login")->withSuccess('Opps! You do not have access');
}
如果转换为整数,前导零可能会被去除。
尝试在 $id
传递给 getDownload
后添加填充:
str_pad($id, 6, '0', STR_PAD_LEFT);
我希望我已经接近最后阶段了。我将文件上传到 storage/files/
并使用 id_message
为每个上传文件创建 uniq 文件夹没有问题并将文件数据存储在 table files
文件的最终路径是 /storage/files/{id_message}/{file_name}
两个变量 id_message
和 file_name
都在 table files
.
FileController
函数 fileUpload
:
function fileUpload(Request $request)
{
$request->validate([
'id_message' => 'required|min:6'
]);
$idParameter = $request->id_message=$request->id_message;
$result=$request->file('file_path')->store('files/'.$idParameter);
$file = new File;
$file->id_message=$idParameter;
$file->file_path=$result;
$file->file_name=$request->file('file_path')->getClientOriginalName();
$file->save();
上传后 table files
:
id id_message file_name
1 000001 Myfile.zip
/storage/app/files/000001/Myfile.zip
FileController
: getDownload
public function getDownload($id)
{
$resultFile = DB::table('files')->where('id_message',$id)->first();
$attachment = store_path().'/' . $resultFile->id_message . '/' . $resultFile->file_name;
return response()->download($attachment);
}
route
Route::get('download/{id}/{fileName}', 'FileController@getDownload')->name('downloadFile');
view.blade
<td><a href="{{ route ('downloadFile', $resultFile->file_name)}}">Download</a></td>
error
Undefined variable:resultFile
我是否接近于 laravel 最终下载文件?
controller for view
table users
public function postLogin(Request $request)
{
request()->validate([
'id_message' => 'required',
'sms_code' => 'required',
]);
$credentials = $request->only('id_message', 'sms_code');
$request->session()->flash('success','');
if ($user=User::where($credentials)->first()) {
auth()->login($user);
return redirect()->intended('dashboard')->with('success','');
}
return Redirect::to("login")->with(['url_attribute' => $url_attribute,'id_message' => $id_message])->with('error','');
}
public function dashboard()
{
if(Auth::check())
{
return view('dashboard');
}
return Redirect::to("login")->withSuccess('Opps! You do not have access');
}
如果转换为整数,前导零可能会被去除。
尝试在 $id
传递给 getDownload
后添加填充:
str_pad($id, 6, '0', STR_PAD_LEFT);