2016-09-01 39 views
0

幫助我的代碼在xampp和php artisan上運行localhost:8000 但是在服務器託管中驗證失敗。當我嘗試上傳圖片吧,返回Errors: The image field is required.Laravel文件上傳圖像不能用於託管

public function store(Request $request) 
{ 
$this->validate($request, array(
     'image' => 'mimes:jpeg,png,bmp|required|max:3000' 
)); 

Session::flash('success', 'Image been uploaded.'); 

return redirect()->route('galleries.index'); 
} 

HTML

{!! Form::open(['route' => 'galleries.store', 'class' => 'form-inline', 'files' => true]) !!} 

{!! Form::file('image', ['required' => '']) !!} 

{!! Form::submit('Upload', ['class' => 'btn btn-success']) !!} 

{!! Form::close() !!} 

回答

0

閃存數據

有時你可能希望在會話中存儲的項目只爲下一個請求。您可以使用閃光燈方法。使用此方法存儲在會話中的數據將僅在隨後的HTTP請求期間可用,然後將被刪除。閃存數據是短命的狀態消息主要是有用的:

在這種情況下,你可以使用這個源上傳文件

$fileName = $this->getFileName($file); 
$pathUpload = $this->createPath(sprintf('%s', $config['path'])); 
$media = $file->move($pathUpload, $fileName); 

protected function createPath($paths) 
{ 
    if (!is_array($paths)) { 
     if (!\File::exists($paths)) { 
      \File::makeDirectory($paths, $mode = 0777, true, true); 
     } 
    } else { 
     foreach ($paths as $path) { 
      if (!\File::exists($path)) { 
       \File::makeDirectory($path, $mode = 0777, true, true); 
      } 
     } 
    } 
    return $paths; 
} 

功能createPath用於創建文件夾,並設置權限。

功能移動使用file_put_content上傳文件的方法