【问题标题】:Upload image codeigniter上传图片代码点火器
【发布时间】:2015-07-05 16:23:49
【问题描述】:

我尝试使用 CodeIgniter 上传图像。图像必须发送到文件夹,路径发送到数据库。当我提交时,它什么也没做。 ($story_img) 的 var_dump 显示:

string(8) "/images/"

这是我的代码:

if ($this->input->server('REQUEST_METHOD') == 'POST') {
        $config['upload_path'] = '/images/';
        $config['allowed_types'] = 'gif|jpg|png';
        $config['max_size']    = '1000000';
        $config['overwrite'] = TRUE;
        $config['remove_spaces'] = TRUE;
        $config['encrypt_name'] = FALSE;

        $this->load->library('upload', $config);
        $this->upload->do_upload('story_img');
        $image_path = $this->upload->data();
        $story_img = $image_path['full_path'];

        echo var_dump($story_img);

        $story_text = $this->input->post("story_text");
        $users_id = $this->input->post('users_id');


        $this->form_validation->set_rules('story_text', 'Story text', 'required');
        $this->form_validation->set_rules('story_img', 'Story image ', 'callback__image_upload');
        if ($this->form_validation->run() != FALSE) {

            $new_story = new Story_model();
            $new_story->Story_text = $story_text;
            $new_story->Users_id = $users_id;
            $new_story->Story_date = date('Y-m-d H:i:s');
            $new_story->Story_img = $story_img;


            $this->Story_model->addStory($new_story);
        }
    }

var_dump of $image_path

array(14) { ["file_name"]=> string(0) "" ["file_type"]=> string(0) "" ["file_path"]=> string(8) "/images/" ["full_path"]=> string(8) "/images/" ["raw_name"]=> string(0) "" ["orig_name"]=> string(0) "" ["client_name"]=> string(0) "" ["file_ext"]=> string(0) "" ["file_size"]=> NULL ["is_image"]=> bool(false) ["image_width"]=> NULL ["image_height"]=> NULL ["image_type"]=> string(0) "" ["image_size_str"]=> string(0) "" }

【问题讨论】:

  • 你能发var_dump( $image_path )
  • @mcklayin this my var_dump of $image_path -> array(14) { ["file_name"]=> string(0) "" ["file_type"]=> string(0) "" [ "file_path"]=> string(8) "/images/" ["full_path"]=> string(8) "/images/" ["raw_name"]=> string(0) "" ["orig_name"]= > string(0) "" ["client_name"]=> string(0) "" ["file_ext"]=> string(0) "" ["file_size"]=> NULL ["is_image"]=> bool( false) ["image_width"]=> NULL ["image_height"]=> NULL ["image_type"]=> string(0) "" ["image_size_str"]=> string(0) "" }
  • 你好像没有上传图片。 ["is_image"]=> bool(false)
  • @mcklayin 那么错误在哪里?
  • 请添加var_dump($_FILES)

标签: php image codeigniter upload


【解决方案1】:
$image_path = $this->upload->data();

foreach ($image_path as $info) {
   $path=$info['full_path'];
}

echo $path;

【讨论】:

    【解决方案2】:

    您确定我们的表单字段名称是 story_img 吗? 尝试添加条件:

    if($this->upload->do_upload('story_img'))
    { 
       //your code...
    }  
    

    并尝试在条件中显示 smth,看看你是否通过了。

    【讨论】:


    • @user3071261 您是否尝试过其他具有不同扩展名的图片(.jpg、.gif)?
    • @user3071261 在$this->upload->do_upload('story_img'); 之后尝试var_dump($this->upload->display_errors()) 并检查ypur upload_path 的权限
    • 显然我的坏事有问题,非常感谢!
    【解决方案3】:

    你有没有在表单标签中提到enctype="multipart/form-data"

    【讨论】:

    • 如果您认为这实际上是问题所在,您可以将其作为答案:将其表述为更像是一个陈述,并解释不指定此内容将如何与发帖者看到的症状相匹配。
    猜你喜欢
    • 2011-09-07
    • 2015-07-09
    • 1970-01-01
    • 2013-01-24
    • 1970-01-01
    • 1970-01-01
    • 2011-10-07
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多