【问题标题】:Download zip folder based on form input - Codeigniter根据表单输入下载 zip 文件夹 - Codeigniter
【发布时间】:2020-03-05 05:13:58
【问题描述】:

我想创建搜索文件表单,用户根据年份搜索文件。我已经在一年内制作了文件夹文件(例如文件夹 2019、文件夹 2018 等),所以当用户输入值时,结果将显示基于用户输入。我得到了我想要的结果,但我无法将文件下载为 zip,因为 og 路径文件夹的值为空。我已经尝试过使用 input-> get 和 session-> set_flashdata,但结果仍然为空。我的问题是如何获得年份值,所以可以直接到路径文件夹吗? 注意:tahun 是英文的年份

控制器

public function download_zip() {    
// Read files from directory
$tahun = $this->input->get('tahun');
if($this->input-post('but_createzip2') != NULL){
// File name
$filename = $tahun . _laporan.zip;
// Directory path (uploads directory stored in project root)
$path = './uploaded/laporan/'.$tahun.'/';

// Add directory to zip
$this->zip->read_dir($path, FALSE);

// Save the zip file to archivefiles directory
$this->zip->archive('./uploaded/backup_laporan/'. $filename);

// Download
$this->zip->download($filename);
}
// Load view
$this->load->view('v_detail_laporan');
}}

查看

<form role="form" action="<?php echo base_url().'laporan'?>">
<input type = "text" id="tahun" name="tahun" class="form-control" placeholder="Masukkan Tahun" required/>
</form>
// Download 
<?php echo form_open('laporan/download_zip'); ?>

【问题讨论】:

    标签: codeigniter zip


    【解决方案1】:

    您正在使用 GET 方法发布 dat 那么为什么要在 POST 方法中设置条件

    public function download_zip() {    
    // Read files from directory
        $tahun = $this->input->get('tahun');
    
        if($this->input->get('tahun') != NULL){ // MODIFIED HERE YOU ARE PASSING VALUES USING GET METHOD 
        // File name
        $filename = $tahun ."_laporan.zip";
        // Directory path (uploads directory stored in project root)
        $path = './uploaded/laporan/'.$tahun.'/';
    
        // Add directory to zip
        $this->zip->read_dir($path, FALSE);
    
        // Save the zip file to archivefiles directory
        $this->zip->archive('./uploaded/backup_laporan/'. $filename);
    
        // Download
        $this->zip->download($filename);
        }
        // Load view
        $this->load->view('v_detail_laporan');
    }
    

    试试这个代码

    【讨论】:

      【解决方案2】:

      你有一个错误的表单目标,尝试修改代码如下。

      查看:

      <?php echo form_open('laporan/download_zip', ['method' => 'post', 'role' => 'form']); ?>
      <?php echo form_hidden('but_createzip2','1');?>
      <input type = "text" id="tahun" name="tahun" class="form-control" placeholder="Masukkan Tahun" required/>
      <?php echo form_close(); ?>
      

      控制器:

      public function download_zip() {    
          // Read files from directory
          $tahun = $this->input->post('tahun');
          if($this->input-post('but_createzip2') != NULL){
              // File name
              $filename = $tahun . _laporan.zip;
              // Directory path (uploads directory stored in project root)
              $path = './uploaded/laporan/'.$tahun.'/';
      
              // Add directory to zip
              $this->zip->read_dir($path, FALSE);
      
              // Save the zip file to archivefiles directory
              $this->zip->archive('./uploaded/backup_laporan/'. $filename);
      
              // Download
              $this->zip->download($filename);
          }
          // Load view
          $this->load->view('v_detail_laporan');
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2014-11-02
        • 1970-01-01
        • 2020-02-12
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多