codeigniter图片上传无法获取文件名

时间:2011-12-27 10:50:09

标签: php image codeigniter upload

我试过尝试,但我无法上传我的图像。 当我试图回应它时,我无法获取我的图像名称:S。

你能看出我做错了吗?

这是我的控制器:

<?php 
//ADMIN PAGE
if (! defined('BASEPATH')) exit('No direct script access');

class News extends CI_Controller {


    //Write post when logged in as admin
    function write()
    {

        //insert image
        $config['upload_path'] = APPPATH .'/archive/img/news/';
        $config['allowed_types'] = 'gif|jpg|jpeg|png';
        $config['max_size']         = '9000';
        $config['encrypt_name']     = true;

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

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

        $newsData = array(
            'headline'      => $this->input->post('headline'),
            'description'   => $this->input->post('description'),
            'content'       => $this->input->post('content'),
            'creater'       => $this->session->userdata('username'),
            'ip'            => $this->session->userdata('ip'),
            'imgPath'       => $file_data['file_name']
        );


        echo "<pre>";
        //print_r( $this->upload->data());
        //print_r($file_data);
        //print_r($_FILES);
        //print_r($this->input->post());
        print_r($newsData);
        echo "</pre>";


        $this->load->model('admin/news_model');
        $this->news_model->insertNews($newsData);


        $data['main_content'] = 'admin/write_view';
        $this->load->view('template', $data);

    }

}

我的视图文件,我上传我的图像

<div id="inputStyle">


<?php

echo form_open_multipart('admin/news/write');

echo form_input('headline', 'overskrift');

echo form_upload('newsImage');

echo form_textarea('description', 'indhold');

echo form_textarea('content', 'content');

echo form_submit('create', 'Opret nyhed');

echo form_close();


?>




</div><!-- inputStyle -->

1 个答案:

答案 0 :(得分:4)

我已经编辑了你的代码。它可能适合你。如果您的文件夹位于应用程序的根文件夹中,则无需使用APPPATH。我也在你的代码中对此进行了编辑。试试这个。

//Write post when logged in as admin
function write()
{
    //insert image
    $config['upload_path'] = 'archive/img/news/';
    $config['allowed_types'] = 'gif|jpg|jpeg|png';
    $config['max_size']         = '9000';
    $config['encrypt_name']     = true;

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

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

    $newsData = array(
        'headline'      => $this->input->post('headline'),
        'description'   => $this->input->post('description'),
        'content'       => $this->input->post('content'),
        'creater'       => $this->session->userdata('username'),
        'ip'            => $this->session->userdata('ip'),
        'imgPath'       => $_FILES['newsImage']['name']
    );


    echo "<pre>";
    //print_r( $this->upload->data());
    //print_r($file_data);
    //print_r($_FILES);
    //print_r($this->input->post());
    print_r($newsData);
    echo "</pre>";


    $this->load->model('admin/news_model');
    $this->news_model->insertNews($newsData);


    $data['main_content'] = 'admin/write_view';
    $this->load->view('template', $data);
}