简体   繁体   中英

Unable to upload image in Laravel: “The ”C:\xampp\tmp\php38A9.tmp“ file does not exist or is not readable.”

So I'm trying to upload an image on update() function and it keeps giving me "C:\xampp\tmp\php38A9.tmp" file does not exist or is not readable." error. Following is my code:

EditForm.blade.php (Form with the Image Input):

{!! 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 (Request to validate file types):

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'
    ];
}
}

Edit Button (This is the button that the user clicks to get to EditProfile.blade.php):

 {{ link_to_route('profile.edit','Edit Profile',[Auth::user()->id],['class'=>'btn btn-danger btn-block']) }}

So, when I upload the image and click Edit, it just gives me the error (I've attached a pictue of the error for everyone to see). Please let me know what I'm doing wrong here. Feel free to ask me to show more code if required.

在此处输入图像描述

I recently faced this problem and to fix this i used below method. First go to your config/filesystems.php and inside disks array replace the local with below

'local' => [
            'driver' => 'local',
            'root' => public_path(),
        ], 

Nad then in controller you can use it like below

if ($request->img) {
            $file = $request->File('img');
            $ext  = $user->username . "." . $file->clientExtension();
            $file->storeAs('images/', $ext);
            $user->image_name = $ext;
        }

I've been facing this problem for a while, for some reason, the below fix helps me. If you're on windows, it could be because of symlink problem. Try this:

php artisan config:cache
php artisan storage:link it doesn't matter if you've already linked it. These commands and then try re-uploading it again. I hope this helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM