samedi 11 janvier 2020

how can i fix " The file was not uploaded due to an unknown error. "

hi i have recently received this error .. when my function want to upload a pic about product and add it to database

The file "2018-06-12-product.jpg" was not uploaded due to an unknown error.

my function:

public function store(Request $request)
{
    $this->validate($request,[
       'title'=>'required',
       'brand_id'=>'required',
       'category_id'=>'required',
       'body'=>'required',
       'price'=>'required|numeric',
       'discount'=>'numeric|max:2',
       'count'=>'required|numeric',
        'image'=>'required|image|mimes:jpeg,png,jpg|max:3024',
    ],[
        'title.required'=>'نامی برای محصول خود انتخاب کنید',
        'brand_id.required'=>'برند محصول خود را انتخاب کنید',
        'category_id.required'=>'دسته بندی محصول خود را مشخص کنید',
        'body.required'=>'توضیحی برای محصول خود بنویسید',
        'price.required'=>'مبلغی برای محصول خود معین کنید',
        'price.numeric'=>'لطفا در وروردی مربوط به قیمت از عدد استفاده کنید',
        'discount.numeric'=>'لطفا در وروردی مربوط به تخفیف از عدد استفاده کنید',
        'discount.max'=>'تخفیف مد نظر شما مجاز نیست. نمی توانید از دو رقم بیشتر وارد کنید ',
        'count.required'=>'مقدار موجود در انبار را مشخص کنید ',
        'count.numeric'=>'مقدار موجود در انبار را با عدد مشخص کنید',
        'image.required'=>'لطفا عکسی برای محصول خود انتخاب کنید',
        'image.image'=>'فایل مورد نظر باید یکی از فرمت های تصویری باشد (jpeg,png,jpg)',
        'image.mimes'=>'فایل مورد نظر باید یکی از فرمت های تصویری باشد (jpeg,png,jpg)',
    ]);

    $file=$request['image'];
    $imgname=$file->getClientOriginalName();
    $this->ImageProductUploader($file,'/upload/product/large/');
    $this->ImageProductThumbUploader($file,'/upload/product/thumb/');
    $product=product::create([
       'title'=>$request->title,
       'brand_id'=>$request->brand,
       'category_id'=>$request->category,
       'body'=>$request->body,
       'price'=>$request->price,
       'discount'=>$request->discount,
       'count'=>$request->count,
        'image'=>$imgname,
        'image_thumb'=>$imgname,
    ]);

    $product->category()->sync($request->input('category'));

    session()->flash('add_product','محصول جدید با موفقیت اضافه شد');
    return back();
}

my file uploader function :

 public function ImageProductUploader($file,$paths){

    $filename=$file->getClientOriginalName();
    $path=public_path($paths);
    $files=$file->move($path,$filename);
    $img=Image::make($files->getRealPath());
    $img->resize(675,675,null, function ($constraint) {
        $constraint->aspectRatio();
    });
    $img->save($path.$filename);
    return $paths.$filename;
}
public function ImageProductThumbUploader($file,$paths){

    $filename=$file->getClientOriginalName();
    $path=public_path($paths);
    $files=$file->move($path,$filename);
    $img=Image::make($files->getRealPath());
    $img->resize(175,175,null, function ($constraint) {
        $constraint->aspectRatio();
    });
    $img->save($path.$filename);
    return $paths.$filename;
}

and my form:

 <form class="form-horizontal" action="" method="post" enctype="multipart/form-data">
            

  </form> 

i always use from this codes for uploading the photo but i can't understand why i get this error



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire