codeigniter - >上传空问题

时间:2011-04-10 22:23:24

标签: php codeigniter upload

我收到以下错误:

Column 'thumbpath' cannot be null

我已尝试$thumb_info = $thumb_info = $this->image_lib->resize();,然后在我的$data数组中,我将其设置为'thumbpath' => $thumb_info['create_thumb'],但这会导致PHP错误

Fatal error: Call to a member function resize() on a non-object in addimage.php on line 60

我也在努力将错误或成功消息发布到视图中:

查看

<?php
    //Setting form attributes
$formAddImage = array('id' => 'addImage', 'name' => 'addImage');
$imageImage = array('id' => 'userfile', 'name' => 'userfile', 'placeholder' => 'File Location*');
$imageDescription = array('id' => 'description','name' => 'description','placeholder' => 'Image Description*');
?>
<section id = "validation"><?php echo validation_errors();?></section>

<?php
if(!empty($success)) : ?>

<section id="validation"><?php echo $success; ?></section>

<?php endif; ?>

<?php
echo form_open_multipart('admin/addimage', $formAddImage);
echo form_fieldset();
echo form_upload($imageImage);
echo form_textarea($imageDescription);
echo form_submit('submit','Submit');
echo form_fieldset_close();
echo form_close();
?>`

控制器

 if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Addimage extends CI_Controller { 

function __construct(){ 
parent::__construct(); 
} 
function index() { 
if(!$this->session->userdata('logged_in')) { 
    redirect('admin/home'); 
} 
// Main Page Data 
$data['cms_pages'] = $this->navigation_model->getCMSPages(); 
$data['title'] = 'Add Gallery Image'; 
$data['content'] = $this->load->view('admin/addimage',NULL,TRUE); 

$this->load->view('admintemplate', $data); 

//Set Validation 
//$this->form_validation->set_rules('userfile', 'userfile', 'trim|required'); 
$this->form_validation->set_rules('description', 'Description', 'trim|required'); 

if($this->form_validation->run() === TRUE) { 

//Set File Settings 
$config['upload_path'] = 'includes/uploads/gallery/'; 
$config['allowed_types'] = 'jpg|png'; 
$config['max_size'] = '100'; 
$config['max_width'] = '1024'; 
$config['max_height'] = '768'; 

$this->load->library('upload', $config);
if(!$this->upload->do_upload()) {

$error = array('imageError' => $this->upload->display_errors());
$this->load->view('admintemplate', $data); 
}
else{
$data = array('upload_data' => $this->upload->data());
$config['image_library'] = 'GD2';
$config['source_image'] = $this->upload->upload_path.$this->upload->file_name;
$config['new_image'] = 'includes/uploads/gallery/thumbs/';
$config['create_thumb'] = 'TRUE';
$config['maintain_ratio'] = 'FALSE';
$config['width'] = '200';
$config['height'] = '150';

$this->load->library('image_lib', $config);
$this->image_lib->resize();
}

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

$data = array(   
    'description' => $this->input->post('description', TRUE), 
    'fullpath' => $file_info['file_name'],
'thumbpath' => $file_info['create_thumb']
    ); 
$this->image_model->addImage($data);

$this->data['success'] = 'Thank You, Your Image Has Been Uploaded';
} 
} 

}

模型

function addImage($data) {
    $this->db->insert('images',$data);
    return;
}

2 个答案:

答案 0 :(得分:0)

第58行:

'thumbpath' => $file_info['create_thumb']

我不相信'create_thumb'是来自$ this-&gt; upload-&gt; data()

的有效字段

也许你想要:

 'thumbpath' => $config['new_image'].$this->upload->file_name  

好的 - 关于让拇指路径正确的第二个问题,我想你想要这样的东西:

$imagefullpath = $config['new_image'].$this->upload->file_name;
$path_parts = pathinfo($imagefullpath);
$imageextension = $path_paths['extension'];
$imagerenamed = $part_parts['dirname'].'/'.$path_parts['filename'].'_thumb'.$imageextension;

(未经测试,并确保您至少运行PHP 5.2)

让我知道结果如何。

答案 1 :(得分:-1)

我通过执行以下操作修复了此问题:

    $data = array(   
    'description' => $this->input->post('description', TRUE), 
    'fullpath' => $file_info['file_name'],
'thumbpath' =>$file_info['raw_name'].'_thumb'.$file_info['file_ext'] 
    );