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 帮我写一个c++工程
  • ¥30 Eclipse官网打不开,官网首页进不去,显示无法访问此页面,求解决方法
  • ¥15 关于smbclient 库的使用
  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教