PHP foreach($images as $image) 不上传图片
PHP foreach($images as $image) not uploading images
我正在尝试将多张图片上传到我的服务器,但无法正常工作。它给了我状态代码 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你可以循环浏览所有上传的文件
喜欢$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);
我正在尝试将多张图片上传到我的服务器,但无法正常工作。它给了我状态代码 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你可以循环浏览所有上传的文件
喜欢$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);