Laravel 5.2在会话中存储值数组

时间:2016-04-28 08:55:57

标签: arrays ajax session laravel-5.2

我试图在Ajax上传期间存储文件路径的值。这是我的代码:

public function ajaxUpload() {
  $name = "";
  $path = public_path('files/uploads/articles/');

  if (Input::hasFile('file')) {
    $files = Input::file('file');
    $filePath = [];

  foreach($files as $file){
      $fP = $path.$file->getClientOriginalName();
      return json_encode($fP);
      Image::make($file)->resize(100, 100)->save($path.'/'.$file->getClientOriginalName());
  }

  if (Session::has('file.Path')){
    Session::push('file.Path', $fP);
    Session::save();
  }
  else {
    Session::put('file.Path', $fP);

    Session::save();
  }

  return json_encode(Session::get('file.Path'));
}

控制台中$ fp的输出是:

"\/home\/vagrant\/Projects\/myProject\/public\/files\/uploads\/articles\/image_2733536a64.png" 

错误是:

 LOG.error: Symfony\Component\Debug\Exception\FatalThrowableError: Fatal error: [] operator not supported for strings in /home/vagrant/Projects/myProject/vendor/laravel/framework/src/Illuminate/Session/Store.php:411

当我尝试这样的时候:

public function ajaxUpload() {
  $name = "";
  $path = public_path('files/uploads/articles/');

  if (Input::hasFile('file')) {
    $files = Input::file('file');

  foreach($files as $file){
      $filePath[] = $path.'/'.$file->getClientOriginalName();
      Image::make($file)->resize(100, 100)->save($path.'/'.$file->getClientOriginalName());
    }
      Session::put('filePath', $filePath);
      return json_encode($filePath);
   }
   return 'string';
}

然后它分别存储每个路径而不是一个数组,所以我只在DB中保存一个路径。

0 个答案:

没有答案