我正在尝试使用代码点火器框架上传图像 因为我的控制器类代码如下
public function addNewSlider()
{
if($this->session->userdata('logged_in'))
{
$this->form_validation->set_rules('title','Title','required');
if($this->form_validation->run()===FALSE)
{
$data['content']=$this->input->get_post('content');
$data['file']=$this->input->get_post('file');
$data['status']=$this->input->get_post('status');
$this->load->view('admin/templates/header',array('sessionArray'=>$this->sessionArray));
$this->load->view('admin/sliders/addNewSlider',$data);
$this->load->view('admin/templates/footer');
}
else
{
/*$title=$this->input->get_post('title');
$content=$this->input->get_post('content');
$image=$this->input->get_post('file');
$status=$this->input->get_post('status');
$this->sliders_model->insertNewMenu($title,$content,$image,$status);
redirect('sliders','refresh');*/
}
}
else
{
redirect('index','refresh');
}
}
function do_upload()
{
$name=time();
$config['upload_path'] = './uploads/';
$config['allowed_types'] = 'gif|jpg|png';
// $config['max_size'] = '100';
// $config['max_width'] = '1024';
//$config['max_height'] = '768';
$config['file_name']=$name;
$this->load->library('upload', $config);
if ( ! $this->upload->do_upload())
{
$error = array('error' => $this->upload->display_errors());
print_r($error);exit;
//$this->load->view('upload_form', $error);
}
else
{
$data = array('upload_data' => $this->upload->data());
//$this->load->view('upload_success', $data);
}
$config['image_library'] = 'gd2';
$config['source_image'] = './uploads/'.$name.'.jpg';
$config['new_image'] = './uploads/'.$name.'.jpg';
$config['create_thumb'] = FALSE;
$config['maintain_ratio'] = FALSE;
$config['width'] = 1000;
$config['height'] = 400;
$this->image_lib->resize();
$config['new_image'] = './uploads/'.$name.'.jpg';
$config['width'] = 1000;
$config['height'] = 400;
$this->image_lib->initialize($config);
$this->image_lib->resize();
$title=$this->input->get_post('title');
$content=$this->input->get_post('content');
$image=$name.'.jpg';
$status=$this->input->get_post('status');
$this->sliders_model->insertNewSlider($title,$content,$image,$status);
redirect('sliders','refresh');
}
**and my view file is code is as follows**
<?php echo form_open_multipart('sliders/do_upload',array('class'=>'search_form general_form'));?>
<!--[if !IE]>start fieldset<![endif]-->
<fieldset>
<!--[if !IE]>start forms<![endif]-->
<div class="forms">
<!--[if !IE]>start row<![endif]-->
<div class="row">
<label>Title:</label>
<div class="inputs">
<span class="input_wrapper"><input class="text" name="title" id="title" type="text" value="<?php //echo $title?>"/>
</span>
</div>
</div>
<!--[if !IE]>end row<![endif]-->
<!--[if !IE]>start row<![endif]-->
<div class="row">
<label>Image Path:</label>
<div class="inputs">
<!--<input type="file" name="file" id="file" /> -->
<input type="file" name="file"/>
</div>
</div>
<!--[if !IE]>end row<![endif]-->
<!--[if !IE]>start row<![endif]-->
<div class="row">
<label>Content:</label>
<span class="input_wrapper textarea_wrapper">
<textarea rows="5" cols="5" class="text" name="content"><?php //echo $content?></textarea>
</span>
</div>
</div>
<!--[if !IE]>end row<![endif]-->
<!--[if !IE]>start row<![endif]-->
<div class="row">
<label>Status:</label>
<div class="inputs">
<span class="input_wrapper blank">
<select name="status">
<?php if(isset($_POST['status']))
{
$one='1';
$zero='0';
if($_POST['status']==$one)
{ echo "InIF";?>
<option value="1" selected="selected" >Active</option>
<option value="0" >Inactive</option>
<?php }else if($_POST['status']==$zero){echo "InElseIF";?>
<option value="0" selected="selected" >Inactive</option>
<option value="1">Active</option>
<?php }
}
else
{
echo "InElse"; ?>
<option value="1" >Active</option>
<option value="0" >Inactive</option>
<?php }?>
</select>
</span>
</div>
</div>
<!--[if !IE]>end row<![endif]-->
<!--[if !IE]>start row<![endif]-->
<div class="row">
<div class="buttons">
<ul>
<li><span class="button send_form_btn"><span><span>SEND FORM</span></span><input value="upload" type="submit" /></span></li>
<li><a href="<?php echo base_url()?>sliderTable.php" class="button uncheck_all"><span><span>Cancel</span></span></a></li>
</ul>
</div>
</div>
<!--[if !IE]>end row<![endif]-->
</div>
<!--[if !IE]>end forms<![endif]-->
</fieldset>
<!--[if !IE]>end fieldset<![endif]-->
</form>
我的主要问题是,当我按下提交按钮错误消息时 您没有选择要上传的文件。 会显示。任何人都可以帮我快速解决这个问题吗?
答案 0 :(得分:6)
在CodeIgniter中,如果您使用的是上传库/助手,则默认情况下您必须将文件输入命名为userfile
。
要解决此问题,您可以将文件输入的名称传递给do_upload
方法。
即。
<input type="file" name="my_file" />
$this->upload->do_upload('my_file');