[英]laravel 5.3 upload file
我正在使用 laravel 5.3,我想將文件上傳到目錄。 我一直在關注用於上傳文件的 Laravel 文檔,例如以下代碼:
public function store(Request $request)
{
$path = $request->attachment->store('attachment');
}
但它得到一個錯誤
在字符串上調用成員函數 store()
以前在 Laravel 5.2 上,我一直在使用此代碼上傳文件,並且可以正常工作
if ($request->hasFile('attachment')) {
$destination = 'upload/attachment';
$file = $request->file('attachment');
$file->move($destination, $file->getClientOriginalName());
}
$filelocation = $destination. "/" .$file->getClientOriginalName();
但在 laravel 5.3 中它不起作用,我收到一個錯誤
未定義變量:目的地
你能幫我的代碼有什么問題嗎?
public function store(Request $request)
{
$path = $request->attachment->store('attachment');
}
我不認為$request->attachment
做你想做的事情。 將該行更改為:
$request->file('attachment')->store('/your/destination/path')
if($request->hasFile('attachment')){
$destination = 'upload/attachment';
$file = $request->file('attachment');
$file->move($destination, $file->getClientOriginalName());
}
$filelocation = $destination. "/" .$file->getClientOriginalName();
當$request->hasFile('attachment')
為false
, $destination
不會被聲明。 因此,當它到達$filelocation
行時,您會得到一個未定義的變量。
在 Larave 5.3 中,您可以像這樣使用簡單的上傳文件:
$request->file('file')->storePublicly($folderSrc);
這會將您的文件保存到 storage/app/public/you-folder-src
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.