codeigniter上传文件错误

时间:2012-01-09 18:04:13

标签: php codeigniter

我收到错误消息“您没有选择要上传的文件”。当我尝试使用此代码上传照片时:

function index()
{
$this->load->helper(array('form', 'url'));
$this->load->library('session');
$this->load->library('form_validation');
$this->form_validation->set_rules('title', 'title', 'trim|required|max_length[255]');
$this->form_validation->set_rules('description', 'description', 'trim|required|max_length[2550]');
$this->form_validation->set_error_delimiters('<p class="error">', '</p>');

$data['title'] = $this->input->post('username');
$data['description'] = $this->input->post('description');
if($this->form_validation->run() == FALSE)
{
  $this->load->view('upload_form', $data);
}
else
{
  $this->load->helper('string');
  $config['upload_path'] = realpath($_SERVER['DOCUMENT_ROOT'] . '/uploads') . '/';
  $config['allowed_types'] = 'gif|jpg|png';
  $config['max_size'] = '5000';
  $config['max_width'] = '10240';
  $config['max_height'] = '10240';
  $config['overwrite'] = FALSE;
  $config['file_name'] =  random_string('alpha', 6);
  $config['remove_spaces'] = TRUE;

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

  if(!$this->upload->do_upload('file'))
  {
    $error = array('upload_message' => $this->upload->display_errors());
    $this->load->view('upload_form', $error);
  }
  else
  {
    //do upload stuff
  }
}

这就是观点:

<?php echo form_open('upload'); ?>
<h5>Title: </h5>
<br />
<?php echo form_input('title', set_value('title')); ?>
<?php $a = form_error('title'); ?>
<br />
<?php echo '<h5>'; echo $a=="" ? '<br />' : $a.'<br />'; echo '</h5>'; ?>
<h5>Description: </h5>
<br />
<?php echo form_textarea('description', set_value('description')); ?>
<?php $a = form_error('description'); ?>
<br />
<?php echo '<h5>'; echo $a=="" ? '<br />' : $a.'<br />'; echo '</h5>'; ?>
<h5>Upload: </h5>
<br />
<?php echo form_upload('file'); ?>
<?php $a = (isset($upload_message) ? $upload_message : "<br/>&nbsp<br /><br />"); ?>
<?php echo '<h5>'; echo $a=="" ? '<br />a' : $a.''; echo '</h5>'; ?>
<br />
<?php echo form_submit('upload', 'Upload'); ?>
<?php form_close(); ?>

我已经使用相同的配置制作了一个测试上传表单,它完美无缺。但是,当我尝试使用此表单时,我收到错误“您没有选择要上传的文件。”

2 个答案:

答案 0 :(得分:1)

您需要使用form_open_multipart()代替form_open()。没有它,你的测试脚本可能无法工作。

答案 1 :(得分:0)

使用form_open_multipart()方法代替form_opne()。它为我工作