我正在尝试将多个图像上传到我的服务器,但它无法正常工作。它给我状态代码200,但它不上传图像。
这就是我做的:
if($request->hasFile('post_image')){
$images = $request->post_image;
$i = 0;
foreach($images as $image){
$i++;
$filename = $post->id.'.'.$i.'jpg';
$location = '/var/www/site/html/'.$post->id;
$image->move($location, $filename);
}
}
如果我删除foreach()
然后它上传图像但只有一个。
为什么不上传它们?我做错了什么?
编辑:
在这里回答我自己的问题。问题是我的关键“post_image
”必须是“post_image[]
”而不是。
你忘了在你的文件名中的'jpg'扩展名之前添加.
:应该是这样的:
$filename = $post->id.'.'.$i.'.jpg';
我正在尝试这种方式上传多个图像:
if(!empty(Input::file('image'))){
$files= Input::file('image');
$destinationPath= 'images';
$images=array();
foreach($files as $file){
$fullname= $file->getClientOriginalName();
$hashname = $fullname;
$upload_success =$file->move($destinationPath, $hashname);
$images[]=$fullname;
$has= implode(",",$images);
}
$categories = new CategoryType;
$categories->name = Input::get('name');
$categories->image_attachment = $has;
$categories->save();
}
按照这个awnser,你可以循环所有上传的文件
Get all files in a foreach loop in Laravel
像$images = Input::file('post_image');
问题是我的关键“post_image
”必须是“post_image[]
”而不是。
$file_ary = array();
$file_count = count($request->file('image') );
$a=($request->file('image'));
$finalArray=array();
for ($i=0; $i<$file_count; $i++) {
$fileName = time().$a[$i]->getClientOriginalName();
$destinationPath = $request->input('path') ;
$finalArray[$i]['image']=$destinationPath.$fileName;
$a[$i]->move($destinationPath,$fileName);
}
return json_encode($finalArray);