无法在Laravel中上传图片:““ C:\ xampp \ tmp \ php38A9.tmp”文件不存在或不可读。

时间:2019-11-22 17:17:21

标签: php laravel image

所以我试图在update()函数上上传图像,并且它一直给我“ 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']) }}

因此,当我上传图像并单击“编辑”时,它只是给我错误(我已附上错误图片,以供所有人查看)。请让我知道我在做什么错。如果需要,请随时让我显示更多代码。

enter image description here

2 个答案:

答案 0 :(得分:1)

我最近遇到了这个问题,并通过以下方法解决了这个问题。 首先转到config / filesystems.php,然后在磁盘阵列内部将 local 替换为下面的

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

如果没有,则可以在控制器中按如下所示使用它

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

答案 1 :(得分:0)

由于某些原因,我已经遇到此问题已有一段时间了,下面的修复可以帮助我。如果您在Windows上,则可能是因为symlink问题。试试这个:

php artisan config:cache
php artisan storage:link无关紧要。这些命令,然后尝试再次重新上传。我希望这会有所帮助。