dphj737575
2015-11-18 13:55 阅读 35
已采纳

使用codeigniter(3.1)上传文件失败


I'm having a problem with uploading files with Codeigniter framework. Currently is working like this:
If i don't select any file in HTML form, then variables like $title = $this->input->post('title'); gets value,
but when i select file in html form, then all variables are null and i get message file is not selected.

My .htaccess file

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]

My autoload.php file

$autoload['helper'] = array('url', 'file', 'form');
$autoload['libraries'] = array('database', 'session', 'email', 'form_validation');
$autoload['model'] = array('model_services' => 'ms', 'Model_categories' => 'mc');

My controller which is rendering view:

 public function add_catalog() {
        if ($this->is_admin() == TRUE) {
            $data['cat'] = $this->mc->get();
            $this->load->view('admin_view_map/admin_header');
            $this->load->view('admin_view_map/admin_add_catalog', $data);
            $this->load->view('admin_view_map/admin_footer');
        } else {
            $this->index();
        }
    }

HTML:

<?php echo form_open_multipart('admin321/insert_catalog'); ?>
    *title: <input type="text" name="title" />
    sub_title: <input type="text" name="sub_title" />
    Facebook url: <input type="text" name="facebookUrl" />
    Twitter url: <input type="text" name="twitterUrl" />
    Google +: <input type="text" name="googlePlus" />
    description: <input type="text" name="description" />
    pdf: </span> <input type="file" name="pdf" />
    categories:
    <select name="categories" >
        <?php foreach ($cat as $r) {
                echo '<option value=' . $r->id_category . ' >' . $r->category . '</option>';
        } ?>
    </select>
    <input type="submit" value="Add" class="submit">
<?php echo form_close(); ?>

PHP:

public function insert_catalog(){
    $title = $this->input->post('title');
    $sub_title = $this->input->post('sub_title');
    $facebookUrl = $this->input->post('facebookUrl');
    $twitterUrl = $this->input->post('twitterUrl');
    $googlePlus = $this->input->post('googlePlus');
    $description = $this->input->post('description');
    $category = $this->input->post('categories');
    var_dump($title,$sub_title,$facebookUrl,$twitterUrl,$googlePlus,$description,$category);

    $config['upload_path'] = './assets/pdf/';
    $config['allowed_types'] = 'pdf';
    $this->load->library('upload', $config);

    if (!$this->upload->do_upload('pdf')){
        var_dump($this->upload->display_errors());
        die();
    }else{
        $pdf = array('upload_data' => $this->upload->data());
        var_dump($pdf);
        die();
    }

}

This make result like this:

null
null
null
null
null
null
null
string '<p>You did not select a file to upload.</p>' (length=43)

I'm using WAMP server and this are my php.ini configuration

; Whether to allow HTTP file uploads.
; http://php.net/file-uploads
file_uploads = On

; Temporary directory for HTTP uploaded files (will use system default if not
; specified).
; http://php.net/upload-tmp-dir
upload_tmp_dir = "e:/wamp/tmp"

; Maximum allowed size for uploaded files.
; http://php.net/upload-max-filesize
upload_max_filesize = 64M
  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享

3条回答 默认 最新

  • 已采纳
    donglei1616 donglei1616 2015-11-19 13:38

    Check your php's installation settings in php.ini regarding upload configuration

    upload_max_filesize  = 10M
    post_max_size  = 10M
    
    点赞 评论 复制链接分享
  • doucaishi0077 doucaishi0077 2015-11-18 14:05

    I don't really get it.

    <?php foreach ($cat as $r) {
            echo '<option value=' . $r->id_category . ' >' . $r->category . '</option>';
    } ?>
    

    What are you echoing here ? Where do you get $cat from ?

    点赞 评论 复制链接分享
  • dsavz66262 dsavz66262 2015-11-18 14:06

    Load form helper on your controller

    function __construct(){
    
        parent::__construct();
        $this->load->helper('form');
    
    }
    
    点赞 评论 复制链接分享

相关推荐