简体   繁体   中英

How to upload multiple image in laravel php

i am trying to upload multiple images in laravel, but it seems only one image in inserted into the database and i was wondering how can i be uploading all images not just one.

here is my controller

public function addProperty(Request $request){
        $data = $request->all();
        $pModel = new PropertyModel();
        
        return response()->json(["type"=>$pModel->addProperty($data,$request)]);
    }

here is my view

<h3>Gallery</h3>
        <div class="submit-section">
            <div id="selected-files"></div>
            <input type="file" name="files[]" multiple  onchange="Property.onImageUploadChange(event)" id="files" >
        </div>

here is my model

  public function addProperty($propertyData,$request){
        $db  = new Database;
        @session_start();
        $user = $_SESSION["user"];
        $propertyData["user_id"] = $user->user_id;
        unset($propertyData["_token"]);
        
        unset($propertyData["property_files"]);
        $res = $db->query($db->buildQuery($propertyData, $this->tableName)) or die(false);
        $lastID= $db->lastInsertID($res);
        $image = $request->file("property_files");
        $image->move(public_path('images'),$lastID.".jpg");
        return true;
    }

may this code help you! I'm work on backend and use this function:

public static function upload_image($image, $image_folder)
{
$image_new_name = time() . '_' . $image->getClientOriginalName();
$image_path = public_path('uploads\\' . $image_folder);
$image->move($image_path, $image_new_name);

return $image_folder . '/' . $image_new_name;
}

you can create a SettingController and put it there, then in your controller receive array of images and with foreach loop save them in your server like:

foreach($request->arrayImages as $image) {
     SettingController::upload_image($image, 'your folder name');
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM