我试图在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中保存一个路径。