mercredi 27 janvier 2016

Laravel 5 uploading multiple files

I have a problem with uploading multiple file using laravel5, it only upload one file, what is wrong with my code ? by the way, I've tried the foreach loop but still get the same problem.

Here's my code:

$files = Input::file('files');
$file_count = count($files);

for($a=0;$a < $file_count;$a++){
//$files2 = Input::file('files');
//$i++;
$entry++;
$extension = $files[$a]->getClientOriginalExtension();
if(empty($entry)){
    $filename = $newName.$a.'.'.$extension;
    DB::table('files')->insert(
        ['user_id' => Auth::user()->id, 'new_name' => $filename, 'old_name' => $files[$a]->getClientOriginalName(), 'date' => $dateNow, 'folder_id' => Input::get('filefolder'), 'type' => $extension]
    );
    $upload_success = $files[$a]->move($destinationPath, $filename);
    if($upload_success){
        return Redirect::back()->withErrors([
            'success' => 'Files success to upload.'.count($files),
        ]);
    }else{
        return Redirect::back()->withErrors([
            'failed' => 'Failed to upload files. Please try again.',
        ]);   
    }
}else{
    $filename = $newName.$entry.'.'.$extension;
    DB::table('files')->insert(
        ['user_id' => Auth::user()->id, 'new_name' => $filename, 'old_name' => $files[$a]->getClientOriginalName(), 'date' => $dateNow, 'folder_id' => Input::get('filefolder'), 'type' => $extension]
    );
    $upload_success = $files[$a]->move($destinationPath, $filename);
    if($upload_success){
        return Redirect::back()->withErrors([
            'success' => 'Files success to upload.'.$file_count.$a,
        ]);
    }else{
        return Redirect::back()->withErrors([
            'failed' => 'Failed to upload files. Please try again.',
        ]);   
    }
}

}



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire