dousong5161 2016-04-07 15:06
浏览 66
已采纳

使用CodeIgniter上载库时未抓取文件名

Firstly can I request this is NOT marked as duplicate. I have read the other posts on SO regarding issues with the CodeIgniter upload library and sadly they do not cover this. I have also extensively read the CI documentation and everything suggests this should work correctly.

I am using a very simple form to grab the file, which is uploaded correctly to the images folder. The full_path of the file is also written successfully to a db table called images. The filename however is blank.

My form:

    <?php echo form_open_multipart('image_upload/do_upload');?>

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

    <br /><br />

    <input type="submit" value="upload" />

    </form>

My Controller function:

    function do_upload()
     {
        $config['upload_path'] = 'c:/wamp/www/honest/images';
        $config['allowed_types'] = 'gif|jpg|png';
        $config['max_size'] = '5000';
        $config['overwrite'] = TRUE;
        $config['remove_spaces'] = TRUE;


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

        $image_data = $this->upload->data();

        echo '<pre>'; print_r($image_data); echo '</pre>';

        if ( ! $this->upload->do_upload())
        {
            $error = array('error' => $this->upload->display_errors());

            $data['main_content'] = 'imageupload';
            $this->load->view('includes/template', $data);
            echo "FAILED";
        }
         else
        {
          $data = array(
            'filename' => $image_data['file_name'],
            'fullpath' => $image_data['full_path']
          );
          $this->db->insert('images', $data);   
          $this->load->view('imageupload');
        }
    }

I am using

    echo print_r($image_data); echo;

To display the associated image data but this is all that is returned:

Array
(
    [file_name] => 
    [file_type] => 
    [file_path] => c:/wamp/www/honest/images/
    [full_path] => c:/wamp/www/honest/images/
    [raw_name] => 
    [orig_name] => 
    [client_name] => 
    [file_ext] => 
    [file_size] => 
    [is_image] => 
    [image_width] => 
    [image_height] => 
    [image_type] => 
    [image_size_str] => 
)

I can't work out why it is not grabbing the file name and other details - can someone help spot what is hopefully a simple error?

Many thanks,

DP.

  • 写回答

1条回答 默认 最新

  • douyuan4825 2016-04-07 15:19
    关注

    You are requesting your uploaded data before you actually upload anything. That's why your $image_data only contains your configuration values. Move your $image_data = $this->upload->data(); call to after actually performing the do_upload() (into your else block):

         function do_upload()
         {
            $config['upload_path'] = 'c:/wamp/www/honest/images';
            $config['allowed_types'] = 'gif|jpg|png';
            $config['max_size'] = '5000';
            $config['overwrite'] = TRUE;
            $config['remove_spaces'] = TRUE;
    
    
            $this->load->library('upload', $config);    
    
            if ( ! $this->upload->do_upload())
            {
                $error = array('error' => $this->upload->display_errors());
    
                $data['main_content'] = 'imageupload';
                $this->load->view('includes/template', $data);
                echo "FAILED";
            }
             else
            {
              $image_data = $this->upload->data();
              $data = array(
                'filename' => $image_data['file_name'],
                'fullpath' => $image_data['full_path']
              );
              $this->db->insert('images', $data);   
              $this->load->view('imageupload');
            }
        }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化
  • ¥15 Mirare PLUS 进行密钥认证?(详解)
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
  • ¥20 想用ollama做一个自己的AI数据库
  • ¥15 关于qualoth编辑及缝合服装领子的问题解决方案探寻
  • ¥15 请问怎么才能复现这样的图呀