繁体   English   中英

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

[英]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.

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