2016-11-01 7 views
1

Ich habe versucht, mehrere Bilder Funktion mit Laravel zu machen, aber ich habeMehrere Bilder mit Laravel hochgeladen

'Invalid argument supplied for foreach()'

empfangen Dies ist die Funktion in der Steuerung

public function uploadSubmit() { 

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

    foreach($files as $file) { 

     $gallery = new Gallery;  
     $rules = array('file' => 'required'); 
     $validator = Validator::make(array('file'=> $file), $rules); 

     if($validator->passes()){ 

     $filename = str_random(20); 
     $file->move(public_path() . '/uploads', $filename . '.' . $file->getClientOriginalExtension()); 
     $imagePath = '/uploads/' . $filename . '.' . $file->getClientOriginalExtension(); 

     $image = Image::make(public_path() . $imagePath); 

      $image->save(); 
    $gallery ++; 
     $gallery->image = $imagePath; 
     $gallery->save(); 
     } 
    } 
    if($gallery == $file_count){ 
     return Redirect::to('/admin/upload')->with('message', 'image added.'); 
    } 
    else 
    { 
     return Redirect::to('/admin/upload')->withInput()->withErrors($validator); 
    }  

} 

Als ich var_dump($files); es NULL zurückgibt.

Die Form ist

{{ Form::open() }} 
    {{ Form::file('image[]', array('multiple'=>true)) }} 
     <hr /> 
     <button type="submit" class="btn btn-primary">upload</button>   
{{ Form::close() }} 

Meine Route:

Route::get ('/admin/upload', ['uses' => '[email protected]', 'before' => 'admin']); 
Route::post('/admin/upload', ['uses' => '[email protected]', 'before' => 'csrf|admin']); 

Antwort

2

make Datei dann, wenn Sie Ihr Formular

{!! Form::open(array('route' => 'image.upload', 'method' => 'POST', 'files' => true)) !!}

{!! Form::open(array('route' => 'image.upload', 'method' => 'POST', 'files' => true)) !!} 
    {{ Form::file('image[]', array('multiple'=>true)) }} 
     <hr /> 
     <button type="submit" class="btn btn-primary">upload</button>   
{{ Form::close() }} 

Ihre Funktion

erstellen
public function uploadSubmit() { 
    $files = Input::file('image'); 
    $file_count = count($files); 
    foreach($files as $file) { 
     $gallery = new Gallery;  
     $rules = array('file' => 'required'); 
     $validator = Validator::make(array('file'=> $file), $rules); 
     if($validator->passes()){ 
      $filename = str_random(20). '.' . $file->getClientOriginalExtension(); 
      $file->move('uploads', $filename); 
     $gallery->image = $filename; 
     $gallery->save(); 
     } 
    } 
} 
+0

Dankten aber was ist diese Route auf dem freien Tag ' 'Weg' => 'image.upload','? Ich habe route in meinem 'route.php' –

+0

Okay, ich habe Fehler:' 'Kann nicht das Verzeichnis "/ uploads" erstellen ... aber das Verzeichnis ist schon da? –

+0

Ich definiere eine Regel in route.php Route :: post ('image/upload', ['als' = 'image.upload', 'uses' => 'ImageController @ upload']); Sie können es –

Verwandte Themen