Laravel File Upload

By | October 12th 2018 04:07:10 PM | viewed 18 times

Create a service and write following code:


<?php namespace App\Services\Web;


use DB;
use Illuminate\Routing\UrlGenerator;
use Illuminate\Support\Facades\URL;
use Symfony\Component\HttpFoundation\File\UploadedFile;
use Folklore\Image\Facades\Image;
use Illuminate\Support\Facades\File;
use Illuminate\Http\Request;
use Carbon\Carbon;

class WebServices{
    
     
      public static $fileName= null;
      public static $fileExt= null;
      public static $fileMineType= null;
      public static $fileSize= null;
      public static $uploadPath= null;
 
      
    public static function fileValivation(Array $files,Array $fileExt,$size){
        
         if(is_array($files)){
             
               foreach($filesArray as $file){
             
                    self::$fileName = $file->getClientOriginalName();
                    self::$fileExt = $file->getClientOriginalExtension();
                    self::$fileMineType = $file->getMimeType();
                    self::$fileSize = $file->getSize();

                    if(! array_search(self::$fileExt, self::$fileExt)){
                         return 3;
                         break;
                    }

                    if( ! self::$fileSize <= $size){
                         return 4;
                         break;
                    }
             
               }
               
               return 1;
             
         }else{
             
             return 2;
         }         
         
                                
          
      }
      
      public static function fileUpload(UploadedFile $file,$fileRename,$location){
                                          
          if($file){
                                        
                    self::$fileName = $file->getClientOriginalName();
                    self::$fileExt = $file->getClientOriginalExtension();
                                                            
                       if($fileRename){
                            self::$fileName = md5(self::$fileName. microtime()).'.'.self::$fileExt;
                           //$fileName =uniqid('l_', true).'.'.$fileExt;
                       }else{
                          self::$fileName = self::$fileName; 
                       }

                    self::$uploadPath = $location;

                    if($file->move(self::$uploadPath,self::$fileName)){

                       return self::$fileName; 
                
                    }else{
                        return 6;
                    }                                                 
             
              
         }else{             
             return 5;
         } 
         
         return self::$fileName; 
                                                   
      }
      
      public static function fileUploadMedium(UploadedFile $file,$fileRename,$location,$weight,$height){
                                              
          if($file){
                                        
                    self::$fileName = $file->getClientOriginalName();
                    self::$fileExt = $file->getClientOriginalExtension();
                                                            
                       if($fileRename){
                           self::$fileName = md5(self::$fileName. microtime()).'.'.self::$fileExt;
                           //$fileName =uniqid('m_', true).'.'.$fileExt;
                       }else{
                          self::$fileName = self::$fileName; 
                       }

                      self::$uploadPath = $location.self::$fileName;
                                         
                       if(Image::make($file->getRealPath(),array(
                                    'width' => $weight,
                                    'height' => $height,
                                    'crop' => true,
                                    'grayscale' => false
                                 ))->save(self::$uploadPath)){

                       return self::$fileName; 
                
                    }else{
                        return 6;
                    }                                                 
             
              
         }else{             
             return 5;
         } 
         
         return self::$fileName; 
                                                   
      }
      
      
      public static function fileUploadThumb(UploadedFile $file,$fileRename,$location,$weight,$height){
                                             
          if($file){
                                        
                    self::$fileName = $file->getClientOriginalName();
                    self::$fileExt = $file->getClientOriginalExtension();
                                                            
                       if($fileRename){
                           self::$fileName = md5(self::$fileName. microtime()).'.'.self::$fileExt;
                           //$fileName =uniqid('t_', true).'.'.$fileExt;
                       }else{
                          self::$fileName = self::$fileName; 
                       }

                    
                       self::$uploadPath = $location.self::$fileName;
                                         
                       if(Image::make($file->getRealPath(),array(
                                    'width' => $weight,
                                    'height' => $height,
                                    'crop' => true,
                                    'grayscale' => false
                                 ))->save(self::$uploadPath)){

                       return self::$fileName; 
                
                    }else{
                        return 6;
                    }                                                 
             
              
         }else{             
             return 5;
         } 
         
         return self::$fileName; 
                                                   
      }
      
            
    
}

?>
Create a method in a controller and add following code:
public function pimage(Request $request)
	{
            $insert = null;         
             
                  
            if($request->input('updateId')){
                $ecProductImage = EcProductImage::where('prod_amt_price_meta_name',$request->route()->parameter('mn'))->where('bus_name_meta_name',Auth::user()->business_name_meta_name)->first(); 
            }else{
              $ecProductImage = new EcProductImage();   
            }
                   
                 
                   
                    
                     $location = base_path()."/public/images/upload/product/";
                     
                     
                     
                      if($request->input('updateId')){
                          
                          $thumb_file_1 = $location.@$ecProductImage->thumb_file_1; 
                          $medium_file_1 = $location.@$ecProductImage->medium_file_1; 
                          $large_file_1 = $location.@$ecProductImage->large_file_1; 
                          
                          $thumb_file_2 = $location.@$ecProductImage->thumb_file_2; 
                          $medium_file_2 = $location.@$ecProductImage->medium_file_2; 
                          $large_file_2 = $location.@$ecProductImage->large_file_2; 
                          
                          $thumb_file_3 = $location.@$ecProductImage->thumb_file_3; 
                          $medium_file_3 = $location.@$ecProductImage->medium_file_3; 
                          $large_file_3 = $location.@$ecProductImage->large_file_3; 
                          
                          $thumb_file_4 = $location.@$ecProductImage->thumb_file_4; 
                          $medium_file_4 = $location.@$ecProductImage->medium_file_4; 
                          $large_file_4 = $location.@$ecProductImage->large_file_4; 
                          
                          
                         
                          
                               if($request->file('ukmFile_1') && (File::exists($thumb_file_1) || File::exists($medium_file_1) || File::exists($large_file_1))) {
                                                                                                  
                                    File::delete($thumb_file_1);
                                    File::delete($medium_file_1);
                                    File::delete($large_file_1);
                                }
                                
                                if($request->file('ukmFile_2') && (File::exists($thumb_file_2) || File::exists($medium_file_2) || File::exists($large_file_2))) {
                                                                                                      
                                    File::delete($thumb_file_2);
                                    File::delete($medium_file_2);
                                    File::delete($large_file_2);
                                }
                                
                                 if($request->file('ukmFile_3') && (File::exists($thumb_file_3) || File::exists($medium_file_3) || File::exists($large_file_3))) {
                                                                                                      
                                    File::delete($thumb_file_3);
                                    File::delete($medium_file_3);
                                    File::delete($large_file_3);
                                }
                                
                                if($request->file('ukmFile_4') && (File::exists($thumb_file_4) || File::exists($medium_file_4) || File::exists($large_file_4))) {
                                                                                                      
                                    File::delete($thumb_file_4);
                                    File::delete($medium_file_4);
                                    File::delete($large_file_4);
                                }
                          
                      }
                          
                        
                        if(!File::exists($location)) {
                             File::makeDirectory($location, 0775, true);
                         }
                          
                     
                      if($request->file('ukmFile_1')){
                          
                          $this->validate($request, [
                                'ukmFile_1'=> ['required','mimes:jpeg,jpg,bmp,png,gif','max:600'],   //  1024 KB = 1 MB                          
                           ]);
                          
                          $ecProductImage->thumb_file_1 = WebServices::fileUploadThumb($request->file('ukmFile_1'),true,$location,100,125); 
                          $ecProductImage->medium_file_1 = WebServices::fileUploadMedium($request->file('ukmFile_1'),true,$location,245,306); 
                          $ecProductImage->large_file_1 = WebServices::fileUpload($request->file('ukmFile_1'),true,$location); // max:1000*1250 (100:125)
    
                      }
                      
                      if($request->file('ukmFile_2')){
                          
                          $this->validate($request, [
                                'ukmFile_2'=> ['required','mimes:jpeg,jpg,bmp,png,gif','max:600'],   //  1024 KB = 1 MB                          
                           ]);
                          
                          $ecProductImage->thumb_file_2 = WebServices::fileUploadThumb($request->file('ukmFile_2'),true,$location,100,125); 
                          $ecProductImage->medium_file_2 = WebServices::fileUploadMedium($request->file('ukmFile_2'),true,$location,245,306); 
                          $ecProductImage->large_file_2 = WebServices::fileUpload($request->file('ukmFile_2'),true,$location); // max:1000*1250 (100:125)
    
                      }
                      
                      if($request->file('ukmFile_3')){
                          
                          $this->validate($request, [
                                'ukmFile_3'=> ['required','mimes:jpeg,jpg,bmp,png,gif','max:600'],   //  1024 KB = 1 MB                          
                           ]);
                          
                          $ecProductImage->thumb_file_3 = WebServices::fileUploadThumb($request->file('ukmFile_3'),true,$location,100,125); 
                          $ecProductImage->medium_file_3 = WebServices::fileUploadMedium($request->file('ukmFile_3'),true,$location,245,306); 
                          $ecProductImage->large_file_3 = WebServices::fileUpload($request->file('ukmFile_3'),true,$location); // max:1000*1250 (100:125)
    
                      }
                      
                       if($request->file('ukmFile_4')){
                           
                           $this->validate($request, [
                                'ukmFile_4'=> ['required','mimes:jpeg,jpg,bmp,png,gif','max:600'],   //  1024 KB = 1 MB                          
                           ]);
                          
                          $ecProductImage->thumb_file_4 = WebServices::fileUploadThumb($request->file('ukmFile_4'),true,$location,100,125); 
                          $ecProductImage->medium_file_4 = WebServices::fileUploadMedium($request->file('ukmFile_4'),true,$location,245,306); 
                          $ecProductImage->large_file_4 = WebServices::fileUpload($request->file('ukmFile_4'),true,$location); // max:1000*1250 (100:125)
    
                      }
                      
                     
                     

                    if($request->input('updateId')){
                        
                       $ecProductImage->update_by = Auth::user()->id;
                       $ecProductImage->update_date = Carbon::now(); 
                    
                    }else{
                        $ecProductImage->create_by = Auth::user()->id;
                        $ecProductImage->create_date = Carbon::now();
                    }
           
                   
                    
                   $insert = $ecProductImage->save();
               
                    if($insert){

                       return redirect()->route('ownrProd/update',['mn'=>$request->route()->parameter('mn'),'ia'=>1])->with('success','Data insert successfull');

                    }else{

                       $request->session()->flash('fail','Data insert fail');
                       return redirect()->route('ownrProd/update',['mn'=>$request->route()->parameter('mn'),'ia'=>1]);  
                   }
             
	}
bONEandALL