2015-09-24 65 views
0

現在我正在嘗試用戶圖像(user_pic,background_image)upoload。 但我的代碼不起作用。 我應該做一些不同的事情嗎?不確定發生了什麼問題。如何在Codeigniter中上傳多張圖片?

這裏我的html代碼

<form action="<?=base_url();?>edit/up_profile/" method="post" enctype="multipart/form-data"> 
<input type="file" name="pic_file"> 
<input type="file" name="pic_bgfile" /> 
<button type="submit"> 

我的控制器代碼

if($this->form_validation->run() === false){ 
}else{ 
    $config = array(
    'upload_path' => "./images/u/photo", 
    'allowed_types' => "gif|jpg|png|jpeg|pdf", 
    'overwrite' => TRUE, 
    'max_size' => "2048000", 
    'max_height' => "768", 
    'max_width' => "1024" 
); 
    $this->load->library('upload',$config); 
    if($this->upload->do_upload('pic_file')){ 
    $data['profile_image'] = $this->upload->data(); 
    }else if($this->upload->do_upload('pic_bgfile')){ 
    $data['pic_bgfile'] = $this->upload->data(); 
    } 
    $this->load->model('user_model'); 
    $data = array(
    'user_id' => $this->session->userdata('user_id'), 
    'info_tit' => $this->input->post('info_tit'), 
    'scope' => $this->input->post('scope') 
); 
    $this->user_model->p_update($data); 
    //redirect 
    redirect('/edit/profile/', 'location', 301); 
} 
+0

如果你想上傳笨僅僅是由上傳一次在一個圖像的多個圖像,但這種聯繫可能有助於http://stackoverflow.com/questions/11524356/multiple -files-upload-array-with-codeigniter-2-0 – user4419336

+0

@ wolfgang1983 thx! – bothsa24312

回答

0

您應該使用,而不是使用HTML代碼CodeIgniter的自己上傳類。

在視圖中,或者更確切地說,.html文件:

<?php 
    echo form_open_multipart('user/upload_picture'); 
?> 
<input type="file" name="userfile" /> 
<br /><br /> 
<input type="submit" value="submit" /> 
</form> 

指出,在「用戶/ upload_picture」,「用戶」是控制器的名稱,「upload_picture」是在該方法中'用戶'。

在 'upload_picture' 的方法:

$config['upload_path'] = './uploads/'; 
$config['allowed_types'] = 'jpg'; 
$config['max_size'] = 0; 
$config['max_width'] = 0; 
$config['max_height'] = 0; 
$config['file_name'] = 'Apink.jpg'; 
$this->load->library('upload', $config); 
$this->upload->do_upload(); 
+0

感謝您的意見和反饋 – bothsa24312

0

的觀點:

<html> 
<head> 
<title>Upload Form</title> 
</head> 
<body> 
<?php echo $error;?> <!-- Error Message will show up here --> 
<?php echo form_open_multipart('upload_controller/do_upload');?> 
<?php echo "<input type='file' name='userfile' size='20' />"; ?> 
<?php echo "<input type='submit' name='submit' value='upload' /> ";?> 
<?php echo "</form>"?> 
</body> 
</html> 

的控制器

<?php if (! defined('BASEPATH')) exit('No direct script access allowed'); 
     class Upload_Controller extends CI_Controller { 
     public function __construct() { 
     parent::__construct(); 
     } 
     public function file_view(){ 
     $this->load->view('file_view', array('error' => ' ')); 
     } 
     public function do_upload(){ 
     $config = array(
     'upload_path' => "./uploads/", 
     'allowed_types' => "gif|jpg|png|jpeg|pdf", 
     'overwrite' => TRUE, 
     'max_size' => "2048000", // Can be set to particular file size , here it is 2 MB(2048 Kb) 
     'max_height' => "768", 
     'max_width' => "1024" 
     ); 
     $this->load->library('upload', $config); 
     if($this->upload->do_upload()) 
     { 
     $data = array('upload_data' => $this->upload->data()); 
     $this->load->view('upload_success',$data); 
     } 
     else 
     { 
     $error = array('error' => $this->upload->display_errors()); 
     $this->load->view('file_view', $error); 
     } 
     } 
     } 
     ?> 

注意:您可以根據您想要的文件偏好的變化上傳。

該頁面顯示上傳的文件:

<html> 
     <head> 
     <title>Upload File Specification</title> 
     </head> 
     <body> 
     <h3>Your file was successfully uploaded!</h3> 
     <!-- Uploaded file specification will show up here --> 
     <ul> 
     <?php foreach ($upload_data as $item => $value):?> 
     <li><?php echo $item;?>: <?php echo $value;?></li> 
     <?php endforeach; ?> 
     </ul> 
     <p><?php echo anchor('upload_controller/file_view', 'Upload Another File!'); ?></p> 
     </body> 
     </html> 
+0

感謝您的意見和反饋 – bothsa24312