【问题标题】:Codeigniter upload storing "real" file system path instead of clean URLCodeigniter 上传存储“真实”文件系统路径而不是干净的 URL
【发布时间】:2013-06-11 12:30:55
【问题描述】:

我在 codeigniter 中有以下 php:

$config['upload_path'] = './uploads/';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = '4000';
$config['max_width']  = '3000';
$config['max_height']  = '3000';

$this->load->library('upload', $config);
$image_data = $this->upload->do_upload();

但是,当文件上传时,$image_data['file_path'] 返回为 /home/public_html/sitename/uploads。我需要它去 www.domain.com/uploads。关于为什么会发生这种情况的任何想法?

我宁愿不必在其中硬编码 str 替换。希望这不是重复的。

【问题讨论】:

    标签: php codeigniter


    【解决方案1】:

    上传函数返回的数据是根据应用程序的DOCUMENT ROOT。您可以获取 file_name 并相应地使用 base_url() 来完成此操作

    示例。

    $config['upload_path'] = './uploads/';
    $config['allowed_types'] = 'gif|jpg|png';
    $config['max_size'] = '4000';
    $config['max_width']  = '3000';
    $config['max_height']  = '3000';
    
    $this->load->library('upload', $config);
    $image_data = $this->upload->do_upload();
    
    $image_data_file = $image_data["file_name"];
    $image_file_url = base_url()."/uploads/".$image_data_file;
    

    希望这会有所帮助。

    【讨论】:

      【解决方案2】:

      在 codeigniter 文档中指定 $image['file_path'] 返回文件的绝对服务器路径。

      你也可以这样做

      $upload_path = 'uploads/';
      $config['upload_path'] = './'. $upload_path;
      $config['allowed_types'] = 'gif|jpg|png';
      $config['max_size'] = '4000';
      $config['max_width']  = '3000';
      $config['max_height']  = '3000';
      
      $this->load->library('upload', $config);
      $image_data = $this->upload->do_upload();
      $dir_url = base_url($upload_path);
      

      请记住,您必须在 codeigniter 中包含 url helper 才能使用 base_url 函数

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-05-15
        • 1970-01-01
        • 2013-12-26
        • 2011-04-08
        • 1970-01-01
        相关资源
        最近更新 更多