【问题标题】:image upload with codeigniter使用 codeigniter 上传图片
【发布时间】:2018-05-02 16:03:37
【问题描述】:

我在使用 codeigniter 上传图片时遇到困难,它一直说我没有选择要上传的文件。

这是我的代码

function do_upload()
{
    $config['upload_path'] = './uploads/';
    $config['allowed_types'] = 'jpg|png';
    $config['max_size'] = '3000';
    $config['max_width']  = '1024';
    $config['max_height']  = '768';

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

    if (!$this->upload->do_upload()){
        echo $this->upload->display_errors();
    }else{
        echo $this->upload->data();
    }
}

它甚至不将图像发送到目录。

【问题讨论】:

  • 也许一些输出会有帮助?
  • 我不明白,请解释一下。我不是专业人士!
  • @DanielBarde:请包括您收到的任何特定错误消息或输出。此外,您可能还想包含您的视图代码。

标签: codeigniter


【解决方案1】:

试试这个功能:

function upload_done() {
 $config['overwrite'] = TRUE;
 $config['encrypt_name'] = FALSE;
 $config['remove_spaces'] = TRUE;
 $config['upload_path'] = '/var/www/uploadfiles/'; // use an absolute path
 $config['allowed_types'] = 'jpg|png';
 $config['max_size'] = '0'; 
 if ( ! is_dir($config['upload_path']) ) die("THE UPLOAD DIRECTORY DOES NOT EXIST");
 $this->load->library('upload',$config);
 if ( ! $this->upload->do_upload() )
 {
  echo "UPLOAD ERROR ! ".$this->upload->display_errors();
 } else {
  echo "THE IMAGE HAS BEEN UPLOADED : "; var_dump( $this->upload->data() );
 }
}

【讨论】:

  • 但我想立即运行查询并将图像的完整路径存储在我的图像表上我如何获得完整路径。这是包含所有图像信息的数组。 $this->upload->data()
  • 你会在 $t=$this->upload->data(); 中找到你需要的东西。 $full_path_to_image=$t['full_path']; 存储所有数组,只需:$this->db->insert('images_table', $t);
【解决方案2】:

确保您的 html 如下所示。

    <input type="file" name="userfile" size="20" />

Codeigniter 的上传库假定名称为“userfile”

【讨论】:

    【解决方案3】:

    您需要根据您为表单输入提供的名称更改对 do_upload() 的调用。假设您的 HTML 中有以下代码:

    <input type="file" name="image" />
    

    你需要改变:

    $this->upload->do_upload()
    

    $this->upload->do_upload('image')
    

    默认情况下,CodeIgniter 假定名称为“userfile”。

    【讨论】:

      猜你喜欢
      • 2013-12-31
      • 2013-08-23
      • 2011-06-08
      • 2012-01-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多