[英]laravel file upload throws an error “file does not exist or is not readable.”
[英]Unable to upload image in Laravel: “The ”C:\xampp\tmp\php38A9.tmp“ file does not exist or is not readable.”
所以我試圖在 update() function 上上傳圖像,它一直給我“C:\xampp\tmp\php38A9.tmp”文件不存在或不可讀。”錯誤。以下是我的代碼:
EditForm.blade.php (帶有圖像輸入的表單):
{!! Form::model(Auth::user(),array('route'=>['profile.update',Auth::user()->id],'method'=>'PUT','files'=>'true')) !!}
<div class="form-group form-row">
<div class="col">
{!! Form::text('fname',null,['class'=>'form-control','placeholder'=>'Enter First Name']) !!}
</div>
<div class="col-5">
<div class="custom-file">
{!! Form::file('img',['class'=>'custom-file-input']) !!}
{!! Form::label('Choose Avatar',null,['class'=>'custom-file-label']) !!}
</div>
</div>
</div>
<div class="form-group">
{!! Form::text('lname',null,['class'=>'form-control','placeholder'=>'Enter Last Name']) !!}
</div>
<div class="form-group">
{!! Form::email('email',null,['class'=>'form-control','placeholder'=>'Enter Email']) !!}
</div>
<div class="form-group">
{!! Form::password('password',['class'=>'form-control','placeholder'=>'Enter Student Password']) !!}
</div>
<div class="form-group">
{!! Form::text('name',null,['class'=>'form-control','placeholder'=>'Enter Student Username']) !!}
</div>
<div class="form-group">
{!! Form::number('rollno',null,['class'=>'form-control','placeholder'=>'Enter Roll Number']) !!}
</div>
<div class="form-group">
{!! Form::select('class', [
'1st' => '1st',
'2nd' => '2nd',
'3rd' => '3rd',
'4th' => '4th',
'5th' => '5th',
'6th' => '6th',
'7th' => '7th',
'8th' => '8th',
'9th' => '9th',
'10th' => '10th',],
null, ['class'=>'custom-select','placeholder' => 'Choose Student Class']); !!}
</div>
<div class="form-group py-4">
{!! Form::submit('Create',['type'=>'submit','class'=>'btn btn-danger btn-block']) !!}
</div>
{!! Form::close() !!}
ProfileController.php :
class ProfileController extends Controller
{
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function index()
{
$users = User::all();
return view('myprofile',compact('users'));
}
/**
* Show the form for creating a new resource.
*
* @return \Illuminate\Http\Response
*/
public function create()
{
//
}
/**
* Store a newly created resource in storage.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\Response
*/
public function store(Request $request)
{
//
}
/**
* Display the specified resource.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function show($id)
{
//
}
/**
* Show the form for editing the specified resource.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function edit(User $user)
{
$user = User::all();
return view('editprofile',compact('user'));
}
/**
* Update the specified resource in storage.
*
* @param \Illuminate\Http\Request $request
* @param int $id
* @return \Illuminate\Http\Response
*/
public function update(User $user, FileRequest $request)
{
if($request->hasfile('img')){
//getting the file from view
$image = $request->file('img');
$image_size = $image->getClientSize();
//getting the extension of the file
$image_ext = $image->getClientOriginalExtension();
//changing the name of the file
$new_image_name = rand(123456,999999).".".$image_ext;
$destination_path = public_path('/images');
$image->move($destination_path,$new_image_name);
//saving file in database
$user->image_name = $new_image_name;
$user->image_size = $image_size;
$user->save();
}
$user = Auth::user()->update($request->only(
'fname',
'lname',
'name',
'email',
'password',
'rollno',
'class',));
return redirect()->route('profile.index');
}
/**
* Remove the specified resource from storage.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function destroy($id)
{
//
}
}
FileRequest.php (請求驗證文件類型):
class FileRequest extends FormRequest
{
/**
* Determine if the user is authorized to make this request.
*
* @return bool
*/
public function authorize()
{
return true;
}
/**
* Get the validation rules that apply to the request.
*
* @return array
*/
public function rules()
{
return [
'img' => 'mimes:jpeg,gif,png'
];
}
}
編輯按鈕(這是用戶單擊以訪問 EditProfile.blade.php 的按鈕):
{{ link_to_route('profile.edit','Edit Profile',[Auth::user()->id],['class'=>'btn btn-danger btn-block']) }}
所以,當我上傳圖片並單擊編輯時,它只會給我錯誤(我附上了錯誤圖片供大家查看)。 請讓我知道我在這里做錯了什么。 如果需要,請隨時要求我顯示更多代碼。
我最近遇到了這個問題,為了解決這個問題,我使用了下面的方法。 首先 go 到您的 config/filesystems.php 和內部磁盤陣列將本地替換為以下
'local' => [
'driver' => 'local',
'root' => public_path(),
],
Nad 然后在 controller 你可以像下面這樣使用它
if ($request->img) {
$file = $request->File('img');
$ext = $user->username . "." . $file->clientExtension();
$file->storeAs('images/', $ext);
$user->image_name = $ext;
}
我已經面臨這個問題一段時間了,出於某種原因,下面的修復對我有幫助。 如果您使用的是 windows,則可能是因為符號鏈接問題。 嘗試這個:
php artisan config:cache
php artisan storage:link
如果您已經鏈接它也沒關系。 這些命令,然后嘗試重新上傳它。 我希望這有幫助。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.