简体   繁体   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.”

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:所以我试图在 update() function 上上传图像,它一直给我“C:\xampp\tmp\php38A9.tmp”文件不存在或不可读。”错误。以下是我的代码:

EditForm.blade.php (Form with the Image Input): 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 : 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): 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'
    ];
}
}

Edit Button (This is the button that the user clicks to get to EditProfile.blade.php):编辑按钮(这是用户单击以访问 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首先 go 到您的 config/filesystems.php 和内部磁盘阵列将本地替换为以下

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

Nad then in controller you can use it like below Nad 然后在 controller 你可以像下面这样使用它

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.如果您使用的是 windows,则可能是因为符号链接问题。 Try this:尝试这个:

php artisan config:cache
php artisan storage:link it doesn't matter if you've already linked it. php artisan storage:link如果您已经链接它也没关系。 These commands and then try re-uploading it again.这些命令,然后尝试重新上传它。 I hope this helps.我希望这有帮助。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 laravel 文件上传会引发错误“文件不存在或不可读”。 - laravel file upload throws an error “file does not exist or is not readable.” 文件名C:\\ xampp \\ tmp \\ php4632.tmp在使用codeigniter到mysql的上传数据excel中不可读 - The filename C:\xampp\tmp\php4632.tmp is not readable in upload data excel with codeigniter to mysql 无法在 Laravel 7.x 中上传图像:“C:\xampp\tmp\phpE0A1.tmp” 此错误显示在我的数据库banner_photo 列中 - Unable to upload image in Laravel 7.x: ”C:\xampp\tmp\phpE0A1.tmp“ this error show in my database banner_photo column 无法上传CSV文件并出现错误C:\\ xampp \\ tmp \\ php9F4F.tmp - cannot upload csv file and get error C:\xampp\tmp\php9F4F.tmp 如何解决此 Laravel 问题 - “/tmp/phpY14gRo”文件不存在或不可读? - How to resolve this Laravel issue - The “/tmp/phpY14gRo” file does not exist or is not readable? PHP文件上传-生产中无法从/ tmp /读取文件,但它们位于localhost - PHP file upload - files not readable from /tmp/ in production but they are in localhost 文件&#39;C:\\ xampp \\ tmp \\ php3226.tmp&#39;无法重命名。 zend中已经存在错误,怎么办? - File 'C:\xampp\tmp\php3226.tmp' could not be renamed. It already exists error in zend, how to? php tmp名称文件未显示在xampp / tmp文件夹中 - php tmp name file not showing in xampp/tmp folder 图像上传/ Laravel将图像存储为.tmp而不是.img文件 - Image Upload / Laravel stores image as a .tmp and not .img file PHP文件上传和tmp文件 - PHP File upload and tmp files
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM