laravel 5.4 rasmni yuklash

Men ushbu kodni 5.4-rasmdagi faylni yuklash uchun yaratdim

 if ($request->hasFile('input_img')) {
        if($request->file('input_img')->isValid()) {
            try {
                $file = $request->file('input_img');
                $name = rand(11111, 99999) . '.' . $file->getClientOriginalExtension();

                $request->file('input_img')->move("fotoupload", $name);
            } catch (Illuminate\Filesystem\FileNotFoundException $e) {

            }
        }
    }

tasvir muvaffaqiyatli yuklandi, lekin kod bir istisno qo'ydi:

MimeTypeGuesser.php layn 123 da FileNotFoundException:   Fayl

Mening kodamda biron bir xatolik bor yoki u 5,4 laraveldagi bir xatomi, kimdir menga bu muammolarni yechishga yordam berishi mumkinmi?

va bu mening kodim

<form enctype="multipart/form-data" method="post" action="{{url('admin/post/insert')}}">
        {{ csrf_field() }}
         <div class="form-group">
            
            <input data-preview="#preview" name="input_img" type="file" id="imageInput">
            
            
Example block-level help text here. </div> <div class="form-group"> <input class="form-control" type="submit"> </div></form>
13
Ko'rishni ko'rsating?
qo'shib qo'ydi muallif C2486, manba
@Niklesh ok bro i endi ko'rinishni qo'shdi
qo'shib qo'ydi muallif Sawung Himawan, manba
Faylni ko'chirib o'tkazganingizdan so'ng() saqlash kerak.
qo'shib qo'ydi muallif Melannie, manba

9 javoblar

Buni tekshirish vositasida store usuli orqali osonlikcha ishlatishingiz mumkin

Quyidagi kabi

Birinchidan, biz faylni yuklash uchun faylni kiritish bilan form yaratishimiz kerak.

{{Form::open(['route' => 'user.store', 'files' => true])}}

{{Form::label('user_photo', 'User Photo',['class' => 'control-label'])}}
{{Form::file('user_photo')}}
{{Form::submit('Save', ['class' => 'btn btn-success'])}}

{{Form::close()}}

Bizning tekshirgichimizdagi faylni qanday qilib ko'rib chiqishimiz mumkin.

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

class UserController extends Controller
{

  public function store(Request $request)
  {

 //get current time and append the upload file extension to it,
 //then put that name to $photoName variable.
  $photoName = time().'.'.$request->user_photo->getClientOriginalExtension();

  /*
  talk the select file and move it public directory and make avatars
  folder if doesn't exsit then give it that unique name.
  */
  $request->user_photo->move(public_path('avatars'), $photoName);

  }
}

Bo'ldi shu. Endi $ photoName faylini ma'lumotlar bazasiga user_photo maydon qiymati sifatida saqlashingiz mumkin. Siz o'zingizning ko'rinishda aktiv ('avatars') funktsiyasidan foydalanishingiz va rasmlarni ko'rishingiz mumkin.

17
qo'shib qo'ydi

Ushbu kodni sinab ko'ring. Bu sizning muammoingizni hal qiladi.

public function fileUpload(Request $request) {
    $this->validate($request, [
        'input_img' => 'required|image|mimes:jpeg,png,jpg,gif,svg|max:2048',
    ]);

    if ($request->hasFile('input_img')) {
        $image = $request->file('input_img');
        $name = time().'.'.$image->getClientOriginalExtension();
        $destinationPath = public_path('/images');
        $image->move($destinationPath, $name);
        $this->save();

        return back()->with('success','Image Upload successfully');
    }
}
11
qo'shib qo'ydi

Quyidagi kodni ishlating:

$imageName = time().'.'.$request->input_img->getClientOriginalExtension();
$request->input_img->move(public_path('fotoupload'), $imageName);
3
qo'shib qo'ydi
Kod bilan birga izoh berishga harakat qiling.
qo'shib qo'ydi muallif Stuti Rastogi, manba

Laravel 5.4 da guessClientExtension dan foydalanishingiz mumkin

1
qo'shib qo'ydi
public function store()
{
    $this->validate(request(), [
        'title' => 'required',
        'slug' => 'required',
        'file' => 'required|image|mimes:jpg,jpeg,png,gif'
    ]);

    $fileName = null;
    if (request()->hasFile('file')) {
        $file = request()->file('file');
        $fileName = md5($file->getClientOriginalName() . time()) . "." . $file->getClientOriginalExtension();
        $file->move('./uploads/categories/', $fileName);    
    }

    Category::create([
        'title' => request()->get('title'),
        'slug' => str_slug(request()->get('slug')),
        'description' => request()->get('description'),
        'category_img' => $fileName,
        'category_status' => 'DEACTIVE'
    ]);

    return redirect()->to('/admin/category');
}
1
qo'shib qo'ydi
if ($request->hasFile('input_img')) {
    if($request->file('input_img')->isValid()) {
        try {
            $file = $request->file('input_img');
            $name = time() . '.' . $file->getClientOriginalExtension();

            $request->file('input_img')->move("fotoupload", $name);
        } catch (Illuminate\Filesystem\FileNotFoundException $e) {

        }
    } 
}

or follow
https://laracasts.com/discuss/channels/laravel/image-upload-file-does-not-working
or
https://laracasts.com/series/whats-new-in-laravel-5-3/episodes/12

0
qo'shib qo'ydi

Sizning arizangiz uchun yaxshi mantiq shunday bo'lishi mumkin:

public function uploadGalery(Request $request){

  $this->validate($request, [
    'file' => 'required|image|mimes:jpeg,png,jpg,gif,svg|max:2048',
  ]);

  if ($request->hasFile('file')) {
    $image = $request->file('file');
    $name = time().'.'.$image->getClientOriginalExtension();
    $destinationPath = public_path('/storage/galeryImages/');
    $image->move($destinationPath, $name);

    $this->save();

    return back()->with('success','Image Upload successfully');
  }

}
0
qo'shib qo'ydi

Buning uchun yaxshiroq deb o'ylayman

    if ( $request->hasFile('file')){
        if ($request->file('file')->isValid()){
            $file = $request->file('file');
            $name = $file->getClientOriginalName();
            $file->move('images' , $name);
            $inputs = $request->all();
            $inputs['path'] = $name;
        }
    }
    Post::create($inputs);

aslida images - faylni avtomatik ravishda yaratadigan va fayl - bu nomning nomi va bu erda jadvalning ustunidagi ustunimizdagi rasmning nomini saqlab qo'yamiz va rasmni omma oldida saqlaymiz/Rasmlar katalogida

0
qo'shib qo'ydi
// get image from upload-image page 
public function postUplodeImage(Request $request)
{
    $this->validate($request, [
 //check validtion for image or file
        'uplode_image_file' => 'required|image|mimes:jpg,png,jpeg,gif,svg|max:2048',
    ]);
// rename image name or file name 

    $getimageName = time().'.'.$request->uplode_image_file->getClientOriginalExtension();
    $request->uplode_image_file->move(public_path('images'), $getimageName);
    return back()
        ->with('success','images Has been You uploaded successfully.')
        ->with('image',$getimageName);
}
0
qo'shib qo'ydi
Laravel Uzbek Community
Laravel Uzbek Community
413 ishtirokchilar

PhP |BotsUz
PhP |BotsUz
93 ishtirokchilar

Phpni o'rganishni Hohlasangiz https://t.me/joinchat/AAAAAE-KRc5dd5tPMmGmWA A'zo bo'lin