app/Http/Controllers/ProductFilesController.php
/**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
Store a newly created resource in storage.
Store a newly created resource in storage .
public function store(Request $request)
    {
        if(auth()->user()->isAdmin()){
            $this->validate($request,[
                'prodfile_file'=>'required'
            ]);
            if($request->hasFile('prodfile_file')){
                $fileNameFull = $request->file('prodfile_file')->getClientOriginalName();
                $fileName = pathinfo($fileNameFull, PATHINFO_FILENAME);
                $fileExt = pathinfo($fileNameFull, PATHINFO_EXTENSION);
                $fileNameToStore = $fileName.'_'.time().'.'.$fileExt;
                $file = $request->file('prodfile_file');
                $file->storeAs('productfiles/', $fileNameToStore, 'public_uploads');
                $product_file = new ProductFile;
                if($request->input('prodfile_name') != '')
                    $product_file->name = $request->input('prodfile_name');
                else $product_file->name = $fileName;
                $product_file->file_name = $fileNameToStore;
                $product_file->product_id = $request->input('prodfile_productID');
                if(strtolower($fileExt) == 'jpg' || strtolower($fileExt) == 'jpeg' || strtolower($fileExt) == 'png')
                    $product_file->type = 'image';
                else if(strtolower($fileExt) == 'pdf' || strtolower($fileExt) == 'docx' || strtolower($fileExt) == 'doc' || strtolower($fileExt) == 'pptx' || strtolower($fileExt) == 'xlsx')
                    $product_file->type = 'document';
                else return "Not supported file type. Supported types: PDF, DOCX, DOC, PPTX, XLSX.";
                $product_file->save();
                return redirect('products/'.$product_file->product_id);
            }
            else return "No file";
        }
        else abort(404);
    }
public function store ( Request $ request ) { if ( auth ( ) -> user ( ) -> isAdmin ( ) ) { $ this -> validate ( $ request , [ 'prodfile_file' => 'required' ] ) ; if ( $ request -> hasFile ( 'prodfile_file' ) ) { $ fileNameFull = $ request -> file ( 'prodfile_file' ) -> getClientOriginalName ( ) ; $ fileName = pathinfo ( $ fileNameFull , PATHINFO_FILENAME ) ; $ fileExt = pathinfo ( $ fileNameFull , PATHINFO_EXTENSION ) ; $ fileNameToStore = $ fileName . '_' . time ( ) . '.' . $ fileExt ; $ file = $ request -> file ( 'prodfile_file' ) ; $ file -> storeAs ( 'productfiles/' , $ fileNameToStore , 'public_uploads' ) ; $ product_file = new ProductFile ; if ( $ request -> input ( 'prodfile_name' ) != '' ) $ product_file -> name = $ request -> input ( 'prodfile_name' ) ; else $ product_file -> name = $ fileName ; $ product_file -> file_name = $ fileNameToStore ; $ product_file -> product_id = $ request -> input ( 'prodfile_productID' ) ; if ( strtolower ( $ fileExt ) == 'jpg' || strtolower ( $ fileExt ) == 'jpeg' || strtolower ( $ fileExt ) == 'png' ) $ product_file -> type = 'image' ; else if ( strtolower ( $ fileExt ) == 'pdf' || strtolower ( $ fileExt ) == 'docx' || strtolower ( $ fileExt ) == 'doc' || strtolower ( $ fileExt ) == 'pptx' || strtolower ( $ fileExt ) == 'xlsx' ) $ product_file -> type = 'document' ; else return " Not supported file type. Supported types: PDF, DOCX, DOC, PPTX, XLSX. " ; $ product_file -> save ( ) ; return redirect ( 'products/' . $ product_file -> product_id ) ; } else return " No file " ; } else abort ( 404 ) ; }
Store a newly created resource in storage.
Store a newly created resource in storage .
// Handle file // Get file name // Upload