2012-12-29 44 views
0

一切都很好,但是當我打開上傳文件夾時,沒有任何圖像。代碼圖片上傳

這裏是控制器「admin_news」。我嘗試在localhost/site/asset/upload中上傳圖像文件。但是,當我點擊提交只有信息(標題,新聞和類別)進入數據庫,但文件沒有上傳。

function __construct() 
{ 
    parent::__construct(); 
    $this->load->helper(array('form', 'html', 'file')); 
    $config['upload_path'] = base_url('asset/upload/'); 
    $config['allowed_types'] = 'gif|jpg|png'; 
    $this->load->library('upload', $config); 
} 


public function add_news(){ 
    $this->load->helper('form'); 
    $this->load->model('AddNews'); 
    $this->load->view('templates/header', $data); 
    $this->load->view('admin/add_news'); 
    $this->load->view('templates/footer'); 
    if($this->input->post('submit')){ 
     if ($this->upload->do_upload('image')) 
     { 
      $this->upload->initialize($config); 
      $this->upload->data(); 
     } 
     $this->AddNews->entry_insert(); 
     redirect("admin_news/index"); 
    } 
} 

該圖只有:

 <?php echo form_open_multipart(''); ?> 
    <input type="text" name="title" value="Title..." onfocus="this.value=''" /><br /> 
    <input type="file" name="image" /><br /> 

....

+0

我認爲你是缺少控制器/函數名稱的形式打開標籤 '??????echo form_open_multipart(''); ?> –

+0

這不是說,beacouse以相同的形式,我有標題,新聞和類別,他們進入datebase的權利:) – Ant

回答

1

這不應該是一個URL:

$config['upload_path'] = base_url('asset/upload/'); 

這應該是一個路徑上的某個地方你服務器,無論是完全絕對路徑還是相對路徑(Codeigniter中的所有路徑都與index.php相關)。

使用以下兩種之一:

// Full path 
$config['upload_path'] = FCPATH.'asset/upload/'; 

// Relative 
$config['upload_path'] = './asset/upload/'; 

一兩件事:後

if ($this->upload->do_upload('image')) 
{ 
    // You don't need this, and besides $config is undefined 
    // $this->upload->initialize($config); 

    // You don't seem to be doing anything with this? 
    $this->upload->data(); 

    // Move this here in case upload fails 
    $this->AddNews->entry_insert(); 
    redirect("admin_news/index"); 
} 
// Make sure to show errors 
else 
{ 
    echo $this->upload->display_errors(); 
} 
+0

感謝一切都很好現在 – Ant

+0

@螞蟻,實際上你做了什麼,使它的工作? – newday

0

我想在你的代碼,問題是你有聲明配置do_upload方法

if ($this->upload->do_upload('image')) 
     { 
      $this->upload->initialize($config); 
      $this->upload->data(); 
     } 

在使用該方法之前應該初始化配置。我認爲這是問題所在。所以你必須做do_upload方法