上传文件错误消息未显示。笨

时间:2016-01-12 23:25:32

标签: php codeigniter

我希望有人可以帮助我。每当我上传格式错误的文件并提交表单时,都会出现错误。但是,在将多个变量从控制器传递回视图后,似乎没有出现任何错误。谢谢。

控制器代码:

function upload()
{
    $this->load->library('pagination');
    //the name on the view must be userfile
    $username = $this->session->userdata('username');
    $company = $this->session->userdata('company');
    $title = $this->input->post('title');
    $description =$this->input->post('description');
    $path = './assets/files/'.$company.'/announcements';
    $config['upload_path'] = $path;
    $config['allowed_types'] = 'pdf';
    $config['max_size']    = '10000';
    $this->load->library('upload',$config);


    $this->load->model('announcement');
    $this->load->library('pagination');
    $config['per_page']=5;
    if(!$this->upload->do_upload())
    {

        //$error = array('error'=>$this->upload->display_errors());
        $data = array(
        'announcement' => $this->announcement->fetch_announcement($username,$company,$config['per_page'],$this->uri->segment(3)),
        'links' => $this->pagination->create_links(), 
        'error' => $this->upload->display_errors()
        );
        print_r($this->upload->display_errors());
        $this->load->view('includes/admin_header');
        $this->load->view('announcements',$data);
        $this->load->view('includes/admin_footer');
        //redirect('announcements/index');
    }
    else
    {
        $data = array(
        'announcement' => $this->announcement->fetch_announcement($username,$company,$config['per_page'],$this->uri->segment(3)),
        'links' => $this->pagination->create_links(), 
        'error' => $this->upload->display_errors()
        );
        $file_data = array('upload_data' => $this->upload->data());
        $result = $this->announcement->create($insert);
        //$this->load->view('includes/admin_header');
        //$this->load->view('announcements',$data);
        //$this->load->view('includes/admin_footer');
        redirect('announcements/index');
    }

}

查看代码:

 <div class="col-sm-9 col-sm-offset-3 col-md-10 col-md-offset-2 main">
<table class="table">
<thead>
    <tr>
    <th>No.</th>
    <th>Title</th>
    <th>Description</th>
    <th>Added By</th>
    <th>Company</th>
    <th>Date Added</th>
    <th>Publish</th>
</tr>

<?php $offset = $this->uri->segment(3,0)+1; ?>
<?php foreach($announcement as $row): ?>
<tr>
<td><?php echo $offset++; ?></td>
<td><?php echo $row->title; ?></td>
<td><?php echo $row->description; ?></td>
<td><?php echo $row->addedby; ?></td>
<td><?php echo $row->company; ?></td>
<td><?php echo $row->dateadded; ?></td>
<td><?php echo $row->published; ?></td>
<?php endforeach; ?>
</tr>
</tbody>

</table>
<?php echo $links; ?>
</div>


<div class="col-sm-9 col-sm-offset-3 col-md-10 col-md-offset-2 main">
<?php $error = ''; ?>
<h2>Add an announcement</h2>
<?php echo form_open_multipart('announcements/upload'); ?>
<p>Title : <input type="text" name="title"/></p>
<p>Description : 
<textarea style="resize:none;" maxlength="200" row="20" cols="20"     name="description"></textarea>
</p>
<p>Upload file :<input type="file" name="userfile"> </p>
<p><input type="submit" name="submit" value="Upload"></p>
</form>
<?php echo $error; ?>
</div>

2 个答案:

答案 0 :(得分:1)

请参考以下代码: -

if ( ! $this->upload->do_upload())
        {
            $error = array('error' => $this->upload->display_errors());

            $this->load->view('upload_form', $error);
        }
        else
        {
            $data = array('upload_data' => $this->upload->data());

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

Ref From:https://ellislab.com/codeigniter/user-guide/libraries/file_uploading.html

如果这不起作用,请告诉我。

答案 1 :(得分:0)

删除下面的编码开始

<?php $error = ''; ?>
公告视图文件中的