theduncan's avatar

Grammer error with fileupload

I am trying to upload files, It all works, except for writing to the storage space. I guess I am missing something, I just do not know what. Is there a better way of writing the file?

the error I get is:

ErrorException in Grammar.php line 107:
Argument 1 passed to Illuminate\Database\Grammar::columnize() must be of the type array, object given, called in /var/www/laravel/vendor/laravel/framework/src/Illuminate/Database/Query/Grammars/Grammar.php on line 114 and defined

If i remove the line: Storage::disk('local')->put($entry->Hash, File::get($NEW_file)); it works.

My Controller:

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Http\Requests;
use Validator;
use App\File;
use Session;
use Storage;

class FilesController extends Controller
{
    public function index(){
        return view('files.index');
    }

    public function browser(){
        return "file browser";
    }

 public function upload(Request $r){
        $Msg_file_count = 0;                                            // count for flash message array
        $Msg_array = array();                                           // Flash Message Array
        $files = $r->file('images');                                    // getting all of the post data
        $file_count = count($files);                                    // Making counting of uploaded images
        $uploadcount = 0;                                               // start count how many uploaded
        $failcount = 0;
        foreach($files as $NEW_file) {
            $M_reason = '';                             // Blank reason, if successful, it is not needed.
            $M_lvl = 'Info';                                            // Default Level for flash message.
            $rules = array('NEW_file' => 'required|mimes:png,gif,jpeg,jpg');        //'required|mimes:png,gif,jpeg'  !! need to add size limit of 3M
            $validator = Validator::make(array('NEW_file'=> $NEW_file), $rules);
            $File_Name = $NEW_file->getClientOriginalName();                // FileName
            if($validator->passes()){
                $entry = new File;
                $entry  -> Hash = sha1_file($NEW_file);                 //Hash used for storing and retriving file.
                $entry  -> Filename = $File_Name;       
                $entry  -> ext = $NEW_file->guessExtension();               // File Ext
                $entry  -> MimeType = $NEW_file->getMimeType();             // Mime type (used to sort files)
                $entry  -> Size = $NEW_file->getClientSize();               // File Size.
                try{
                    Storage::disk('local')->put($entry->Hash,  File::get($NEW_file));
                    $entry -> save();
                    $uploadcount ++;
                    $M_lvl = 'Success';
                }catch(\Illuminate\Database\QueryException $e){
                    $failcount ++;
                    $M_lvl = 'Failure';
                    $M_reason = 'Duplicate file';
                }
            }else{
                $failcount++;
                $M_lvl = 'Failure';
                $M_reason = 'Not an image';
            }
            $Msg_file_count++;
            $Msg_array[$Msg_file_count] = array(
                'title'=>'file',
                'lvl'=>$M_lvl,
                'file'=>$File_Name,
                'reason'=>$M_reason
            );

        }
        array_unshift($Msg_array, array(
            'title' => 'info',
            'Total' => $file_count,
            'Successful' => $uploadcount,
            'Failure' => $failcount,
            'lvl' => 'info'
        ));
        Session::flash('files', $Msg_array);
        return back();
    }
}

0 likes
0 replies

Please or to participate in this conversation.