代码之家  ›  专栏  ›  技术社区  ›  Tarvo Mäesepp

PHP foreach($images as$image)未上载图像

  •  0
  • Tarvo Mäesepp  · 技术社区  · 7 年前

    我试图上传多个图像到我的服务器,但它不工作。它给我状态代码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[] “相反。

    5 回复  |  直到 7 年前
        1
  •  0
  •   Lexxusss    7 年前

    你忘了添加 . 在文件名中的“jpg”扩展名之前: 应该是这样的:

    $filename = $post->id.'.'.$i.'.jpg';

        2
  •  0
  •   Anil    7 年前

    我正在尝试通过这种方式上载多个图像:

       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();
      }
    
        3
  •  0
  •   Bilal Maqsood    7 年前

    按照这个步骤,你可以在所有上传的文件上循环

    Get all files in a foreach loop in Laravel

    喜欢 $images = Input::file('post_image');

        4
  •  0
  •   Tarvo Mäesepp    7 年前

    问题是我的钥匙“ post_image “必须是” post_image[] “相反。

        5
  •  0
  •   JIJOMON K.A    7 年前
    $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);