【问题标题】:how to save multiple file upload path to database in laravel?如何在laravel中将多个文件上传路径保存到数据库?
【发布时间】:2016-05-08 19:22:57
【问题描述】:

我想上传公共文件夹中的多个文件和数据库中的多个文件路径,我可以上传公共文件夹中的多个文件,但我无法在数据库中保存多个文件路径。数据库中只存储一个文件名和路径。

在视图中:

<form class="form-horizontal row-border" action="<?= URL::to('/checkiou') ?>" method="post" enctype="multipart/form-data">
<input type="hidden" name="_token" value="{{ csrf_token() }}">
<input type="file" name="attachments[]" multiple/> 
<input name="save" type="submit" value="Save">

在控制器中

public function upload() {

        // Request the file input named 'attachments'

   // Request the file input named 'attachments'
      $files = Request::file('attachments');

      //If the array is not empty
      if ($files[0] != '') {
        foreach($files as $file) {
     // Set the destination path
        $destinationPath = 'uploads';
     // Get the orginal filname or create the filename of your choice
        $filename = $file->getClientOriginalName();
     // Copy the file in our upload folder
        $file->move($destinationPath, $filename);
   }

 $data1 = DB::table('tbl_iou')->max('iou_id');
        $check=0;
        $check=DB::table('tbl_iou')
                ->where('iou_id',$data1)
             ->update(array('image_path'=>$destinationPath,   'file_name'=>$filename));

       if ($check > 0)
       {
          $_SESSION['msg']="Petty cash details saved Successfully";
          return Redirect::to('iou_accounts');
       }
  }

【问题讨论】:

    标签: file upload laravel-5.2


    【解决方案1】:

    首先,您在 foreach 循环之外执行代码的第二部分,您希望将数据插入到数据库中。所以你只为最后一个文件更新一次。

    其次,您的$data1 变量仅存储来自tbl_iou 表的最大(假设最后)iou_id。在代码中:

    $check=DB::table('tbl_iou')
        ->where('iou_id',$data1)
        ->update(array(
             'image_path'=>$destinationPath,
             'file_name'=>$filename
        ));
    

    您搜索此 iou_id 与最后一行相同的行,因此您更新唯一一行的值,并在表中使用最大的 iou_id

    我建议以下方法来解决这个问题。我假设在您的数据库表中您已自动生成 iou_id 并且您只插入新行,而不是更新。

    public function upload() {
            // Request the file input named 'attachments'
    
            // Request the file input named 'attachments'
            $files = Request::file('attachments');
            //If the array is not empty
            if (!empty($files[0])) {
                $filesData = array();
                foreach($files as $file) {
                    // Set the destination path
                    $destinationPath = 'testuploads';
                    // Get the orginal filname or create the filename of your choice
                    $filename = $file->getClientOriginalName();
                    // Copy the file in our upload folder
                    $file->move($destinationPath, $filename);
    
                    // ADD DATA TO TEMPORARY ARRAY
                    $filesData[] = array('image_path'=>$destinationPath,   'file_name'=>$filename);
                }
    
                if(DB::table('tbl_iou')->insert($filesData)){
                    $_SESSION['msg']="Petty cash details saved Successfully";
                    return Redirect::to('iou_accounts');
                }
            }
        }
    

    【讨论】:

      猜你喜欢
      • 2018-06-19
      • 2017-09-25
      • 1970-01-01
      • 2016-10-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-04-09
      相关资源
      最近更新 更多