Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Hamelraj's avatar

call to member function move() on a non-object larvel 5.2?

in my controller i try to upload files but when i use this methods same error i will get

isValid();
move($destinationPath);
getClintoriginalNmae();

do i need to install for laravel 5.2 im totally confuse.

Symfony\Component\HttpFoundation\File\UploadedFile;

this is my controller function

public function store(Request $request)
{
    $this->validate($request, $this->rules);
    $input = $request->all();
    $file = $request->file('file');
    $charge = $request->get('charge');
    $date = $request->get('date');
    $job = $request->get('job_id');
    $id = \Auth::id();
    $current_id = DB::table('charges')->max('invoice_no');
    $data = array();
    foreach ($charge as $key=>$value){
        $data[] = [
            'date' => $date,
            'charge'=>$value,
            'job_id'=>$job,
            'user_id'=>$id,
            'amount'=>$input['amount'][$key],
            'category'=>$input['category'][$key],
            'file'=>$value.$file[$key],
            'invoice_no' => $current_id + 1,
        ];
 }
    ;
    dd($data);
    //$des = storage_path().'/assets';
   // $request->file('file')->move($des);
   // DB::table('charges')->insert($data);
    //Redirect::route('charges.index')->with('message', 'Charges created.');
}

this output is

array:2 [▼
  0 => array:8 [▼
    "date" => "2016-01-28"
    "charge" => "ce"
    "job_id" => "2"
    "user_id" => 1
    "amount" => "100"
    "category" => "cat1"
    "file" => "ceH:\xampp\tmp\phpA32A.tmp"
    "invoice_no" => 16
  ]
  1 => array:8 [▼
    "date" => "2016-01-28"
    "charge" => "se"
    "job_id" => "2"
    "user_id" => 1
    "amount" => "200"
    "category" => "120"
    "file" => "seH:\xampp\tmp\phpA32B.tmp"
    "invoice_no" => 16
  ]
]

i have to insert real name of the document and i want display on my view how can i ???? where to use and how ????

getClientOriginalExtension(); 
base_path() . '/assets/',
0 likes
18 replies
edoc's avatar

getclientOriginalName?

typo?

Hamelraj's avatar

@bobbybouwmann @JeffreyWay I published this question but no one will give the proper answer, please consider on this give me idea to move forward im really stuck this is my controller function

public function store(Request $request)
{
    $this->validate($request, $this->rules);
    $input = $request->all();
    $file = $request->file('file');
    $charge = $request->get('charge');
    $date = $request->get('date');
    $job = $request->get('job_id');
    $id = \Auth::id();
    $current_id = DB::table('charges')->max('invoice_no');
    $data = array();
    foreach ($charge as $key=>$value){
        $data[] = [
            'date' => $date,
            'charge'=>$value,
            'job_id'=>$job,
            'user_id'=>$id,
            'amount'=>$input['amount'][$key],
            'category'=>$input['category'][$key],
            'file'=>$value.$file[$key],
            'invoice_no' => $current_id + 1,
        ];
 }
    ;
    dd($data);
    //$des = storage_path().'/assets';
   // $request->file('file')->move($des);
   // DB::table('charges')->insert($data);
    //Redirect::route('charges.index')->with('message', 'Charges created.');
}

this output is

array:2 [▼
  0 => array:8 [▼
    "date" => "2016-01-28"
    "charge" => "ce"
    "job_id" => "2"
    "user_id" => 1
    "amount" => "100"
    "category" => "cat1"
    "file" => "ceH:\xampp\tmp\phpA32A.tmp"
    "invoice_no" => 16
  ]
  1 => array:8 [▼
    "date" => "2016-01-28"
    "charge" => "se"
    "job_id" => "2"
    "user_id" => 1
    "amount" => "200"
    "category" => "120"
    "file" => "seH:\xampp\tmp\phpA32B.tmp"
    "invoice_no" => 16
  ]
]

i have to insert real name of the document and i want display on my view how can i ???? where to use and how ????

getClientOriginalExtension(); 
base_path() . '/public/images/catalog/',
Hamelraj's avatar

@bobbybouwmann you know i did my coding using this tutorials but for me im suffering get Files real name and save to which path i like when i don't knw where to use

 getClintoriginalNmae();
move($destinationPath);
TerrePorter's avatar

Conversation reference for other readers, http://laravel.io/forum/01-24-2016-how-to-store-file-in-mysqldb-and-display-in-view

@Hamelraj Try this,

    // make a filename for the destination it needs to be unique, keep the upload default original extension
    // maybe [job_id]_[user_id]_[invoice_no].[getClientOriginalExtension]
    $destinationFileName = $job.'_'.$user_id.'_'.($current_id+1). '.' . $request->file('image')->getClientOriginalExtension();

    // move the uploaded file from tmp to destination path with the new filename
    $request->file('file')->move(
        base_path() . '/public/images/catalog/', $destinationFileName
    );
   // in the db update, store the destination filename with path
    foreach ($charge as $key=>$value) {
        $data[] = [
            ...
            'file' => base_path() . '/public/images/catalog/' . $destinationFileName, // save the destination path and filename in the db
            ...
        ]
    }
1 like
Hamelraj's avatar

@TerrePorter still im getting error same error call to member funtion getClientOriginalExtension(); on a non-object plz help me

public function store(Request $request)
{
    $this->validate($request, $this->rules);
    $input = $request->all();
    $charge = $request->get('charge');
    $date = $request->get('date');
    $job = $request->get('job_id');
    $id = \Auth::id();
    $current_id = DB::table('charges')->max('invoice_no');
 $destinationFileName = $job.'_'.$id.'_'.($current_id+1). '.' . $request->file('file')->getClientOriginalExtension();
    $request->file('file')->move(
        base_path() . '/assets/files/', $destinationFileName
    $data = array();
    foreach ($charge as $key=>$value){
        $data[] = [
            'date' => $date,
            'charge'=>$value,
            'job_id'=>$job,
            'user_id'=>$id,
            'amount'=>$input['amount'][$key],
            'category'=>$input['category'][$key],
            'file'=>base_path() . '/public/images/catalog/' . $destinationFileName,
            'invoice_no' => $current_id + 1,
        ];
 }
    ;
 
   DB::table('charges')->insert($data);
    Redirect::route('charges.index')->with('message', 'Charges created.');
}
Hamelraj's avatar

@TerrePorter i can add files depend on my requirement using dynamic fileds here i have add two files and this is the return of dd ( $request->file('file') );

array:2 [▼
  0 => UploadedFile {#30 ▼
    -test: false
    -originalName: "CHARGES (2).csv"
    -mimeType: "application/vnd.ms-excel"
    -size: 72
    -error: 0
    path: "H:\xampp\tmp"
    filename: "phpBCA7.tmp"
    basename: "phpBCA7.tmp"
    pathname: "H:\xampp\tmp\phpBCA7.tmp"
    extension: "tmp"
    realPath: "H:\xampp\tmp\phpBCA7.tmp"
    aTime: 2016-01-29 12:32:29
    mTime: 2016-01-29 12:32:29
    cTime: 2016-01-29 12:32:29
    inode: 0
    size: 72
    perms: 0100666
    owner: 0
    group: 0
    type: "file"
    writable: true
    readable: true
    executable: false
    file: true
    dir: false
    link: false
    linkTarget: "H:\xampp\tmp\phpBCA7.tmp"
  }
  1 => UploadedFile {#31 ▼
    -test: false
    -originalName: "invoice (11).pdf"
    -mimeType: "application/pdf"
    -size: 3954
    -error: 0
    path: "H:\xampp\tmp"
    filename: "phpBCA8.tmp"
    basename: "phpBCA8.tmp"
    pathname: "H:\xampp\tmp\phpBCA8.tmp"
    extension: "tmp"
    realPath: "H:\xampp\tmp\phpBCA8.tmp"
    aTime: 2016-01-29 12:32:29
    mTime: 2016-01-29 12:32:29
    cTime: 2016-01-29 12:32:29
    inode: 0
    size: 3954
    perms: 0100666
    owner: 0
    group: 0
    type: "file"
    writable: true
    readable: true
    executable: false
    file: true
    dir: false
    link: false
    linkTarget: "H:\xampp\tmp\phpBCA8.tmp"
  }
]
Hamelraj's avatar

@TerrePorter yes i have

{!! Form::model(new App\Charge, ['route' => ['charges.store'],'files'=>true,'class'=>'form-horizontal','role'=>'form']) !!}
@include('charges/form', ['submit_text' => 'SAVE'])
{!! Form::close() !!}
TerrePorter's avatar

@Hamelraj I see it is an array. A couple of problems, as far as i can tell your db is only set up to have one file reference not multiple.

// load the file array
foreach ($request->file('image')as $filekey=>$fileobj){
   // make file name
   $destinationFileName = $job.'_'.$user_id.'_'.($current_id+1). '.' . $fileobj->getClientOriginalExtension();

    // move the uploaded file from tmp to destination path with the new filename
    $fileobj->move(
        base_path() . '/public/images/catalog/', $destinationFileName
    );
 // save file list
   $dbFilenames[] = base_path() . '/public/images/catalog/', $destinationFileName;
}

// db will need to be updated to support multiple files or can concat them 

'file'=> implode('|', $dbFilenames),  // this makes this - "file1.ext|file2.ext"

Hamelraj's avatar

Thanks @TerrePorter but already i tried this method but file is not required for every charges without file i have to insert charges and amount from this method $request->file('image') is required without file we cant save charges and amount am i correct ? and i tried other way to use foreach( $request->file('file') as $file){ $file->->getClientOriginalExtension(); } ~~ ~ from this can we do something ????

TerrePorter's avatar
Level 12

@Hamelraj Ok, had some sleep, try this

public function store(Request $request)
{
// validate input
    $this->validate($request, $this->rules);

    // set destination path
    $uploadDestinationPath = base_path() . '/assets/files/';

    // get input
    $input = $request->all();
    $charge = $request->get('charge');
    $date = $request->get('date');
    $job = $request->get('job_id');
    $id = \Auth::id();
    $current_id = DB::table('charges')->max('invoice_no');

    // make sure a file has been included
    if ($request->hasFile('file')) {

        // this form uploads multiple files
        foreach ($request->file('file') as $fileKey => $fileObject ){

            // make sure each file is valid
            if ($fileObject->isValid()) {

                // make destination file name
                $destinationFileName = $job . '_' . $id . '_' . ($current_id + 1) . '.' . $fileObject->getClientOriginalExtension();

                // move the file from tmp to the destination path
                $fileObject->move($uploadDestinationPath, $destinationFileName);

                // save the the destination filename
                $dbFilenames[$fileKey] = $uploadDestinationPath . '/' . $destinationFileName;

            }
        }
    } else {
        // no files to include
        //TODO:: handle error for no files uploaded (if not already covered in the $this->validate)
        // set dbFilenames to empty array
        $dbFilenames[] = '';
    }

    // make array for database
    $data = array();
    foreach ($charge as $key=>$value){
        $data[] = [
            'date' => $date,
            'charge'=>$value,
            'job_id'=>$job,
            'user_id'=>$id,
            'amount'=>$input['amount'][$key],
            'category'=>$input['category'][$key],
            'file'=> (isset($dbFilenames[$key])?$dbFilenames[$key]:''), 
               // if file with key does not exist (may not happen, if part of $this->validate), then save it or save empty
            'invoice_no' => $current_id + 1,
        ];
    }
    ;

    // save the data
    DB::table('charges')->insert($data);

    // redirect
    Redirect::route('charges.index')->with('message', 'Charges created.');
}
Hamelraj's avatar

@TerrePorter My lovely friend thnks so mch u saved my life bro else my office will throw out me .... :(

Please or to participate in this conversation.