Tanvir Ahmed
Tanvir Ahmed

Reputation: 1019

how to solve laravel file uploading problem

I tried to upload image on my public folder but i constantl getting error like this

The "C:\xampp\tmp\phpCE7B.tmp" file does not exist or is not readable.

Her is my code that i tried so far

public function create(Register $request)
    {
        //Registering Students
        $student=new Student;
        $student->name = $request->input('name');
        $student->username = $request->input('username');
        $student->email = $request->input('email');
        $student->password = bcrypt($request->input('password'));
        $student->gender = $request->input('gender');
        $student->phone = $request->input('phone');
        if ($request->hasFile('image')) {
            $file=$request->File('image');
            $ext=$student->username. "." .$file->clientExtension();
            $path = public_path(). '/images/';
            $file->move($path,$ext);
            $student->image = $ext;
        }
        $student->save();
}

it saved my info with image in database but after storing gives me the error.Please help me to solve this

"UPDATE"

I dont Know what is happening to me, but now its working autometically

Upvotes: 1

Views: 2198

Answers (3)

Yousef Al-Akour
Yousef Al-Akour

Reputation: 56

I have the same problem when i'm trying to upload image to public path. I solved the problem by defining a file system disk in config (filesystems) like that:

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

then you can upload images to this path by using StoreAs method:

$file = $file->storeAs(YourPathInPublic, YourFileName, [
    'disk' => 'YourDiskName'
]);

Upvotes: 2

Nipun Tharuksha
Nipun Tharuksha

Reputation: 2567

I also had the same issue when I use public_path()

Try something like this rather than using public_path. Actually there is nothing wrong with your code. But this happens most of the time due to permission levels. As others said you can try reinstalling Xampp or just try this.

if ($request->hasFile('image')) {
    $file = $request->File('image');
    $ext = $student->username.".".$file->clientExtension();

    $ext->save('your_public_path/'.$ext);

    //$path = public_path().'/images/';
    //$file->move($path,$ext);
    $student->image = $ext;
}

Example code from one of my project for your info

$image = $request->file('uploadUserAvatar');
$fileName = time().'.'.request()->uploadUserAvatar->getClientOriginalExtension() ;
$image_resize = Image::make($image->getRealPath());
$image_resize->resize(300,300);
$image_resize->save(('assets/img/user_avatars/'.$fileName));

Upvotes: 0

Md Azizur Rahman
Md Azizur Rahman

Reputation: 375

// Get Form Image
             $image = $request->file('image');
            $slug = str_slug($request->name);
            if (isset($image))
            {
                $currentDate = Carbon::now()->toDateString();
                $imagename = $slug.'-'.$currentDate.'-'. uniqid() .'.'. $image->getClientOriginalExtension();
                if (!file_exists('storage/uploads/post'))
                {
                    mkdir('storage/uploads/post',0777,true);
                }
                $image->move('storage/uploads/post',$imagename);
            }else{
                $imagename = "default.png";
            }
//getting coding


 public function index(){

    return view('admin.post.index',compact('posts'));
    }
// show code

<div class="body">
  <img class="img-responsive img-thumbnail" src="{{ asset('storage/uploads/post/'.$post->image) }}" >
    </div>

Upvotes: 0

Related Questions