0

我正在尝试使用显示图像上传错误消息,flashdata但我在下面显示的代码无法正常工作。可能是什么原因?

请建议我解决此问题的解决方案。

mycontrollerPage.php

class Booksetups extends CI_Controller  
{
    function book($book_id = 0)
    {
       $config = array(); 
       $config['base_url'] = 'http://localhost/thexxr.com/Booksetups/book/pgn/';
       $config["total_rows"] = $this->Booksmodel->record_count(); 
       $config['uri_segment'] = 4; 
       $config['per_page'] = 5;  
       $config['full_tag_open'] = '<div id="pagination">';
       $config['full_tag_close'] = '</div>';

        //------------not wroking file upload error validation------------------------------------------   
        $config['upload_path'] = 'uploads/'; 
        $config['allowed_types'] = 'gif|jpg|jpeg|png'; 
        $config['max_size'] = '1000'; 
        $config['max_width'] = '1920'; 
        $config['max_height'] = '1280'; 
        $config['remove_spaces'] = 'TRUE'; 
        $config['file_path'] = 'TRUE'; 
        $config['full_path']    = '/uploads/';

        $this->load->library('upload', $config);
        $this->upload->do_upload("img1");
        $this->upload->do_upload("img2");
        //------------------------------------------------------------------------


       $this->pagination->initialize($config);   
       $page = ($this->uri->segment(4)) ? $this->uri->segment(4) : 0;   

       $this->form_validation->set_error_delimiters('<div  class="error">', '</div>')->set_rules('subject_id', 'subject_id','trim|required|min_length[1]|max_length[150]|xss_clean');
       $this->form_validation->set_error_delimiters('<div  class="error">', '</div>')->set_rules('cover_id', 'cover_id','trim|required|min_length[1]|max_length[150]|xss_clean');
       $this->form_validation->set_error_delimiters('<div  class="error">', '</div>')->set_rules('language_id', 'language_id','trim|required|min_length[1]|max_length[150]|xss_clean');
       $this->form_validation->set_error_delimiters('<div  class="error">', '</div>')->set_rules('edition_id', 'edition_id','trim|required|min_length[1]|max_length[150]|xss_clean');

        if(($this->input->post('book_id'))&& ($this->form_validation->run() === TRUE))
        {  
            if( ! $this->upload->do_upload()) //trying to display error
            {
                   $error = array('error' => $this->upload->display_errors()); 
                   $this->session->set_flashdata('error', $error);
                   redirect(current_url()); 
            }
            $this->session->set_flashdata('msg', '1 row(s) affected.');
            $this->Booksmodel->entry_update();  
            redirect(current_url());
        }
    }
}       

我的视图.php

<?php echo '<fieldset><legend>'. $formtopic.'</legend>' ?>  

<?php echo validation_errors();  ?>   
<?php 
    $message = $this->session->flashdata('msg');  
    if($message)
    {
    ?>
    <div class="success" id="msgdiv"><?php echo  $this->session->flashdata('msg'); ?></div> 
    <?php 
    }
?>  

<?php 
$message = $this->session->flashdata('error');  
    if($message)
    {
    ?> 
    <?php echo $error;?>   
    <!-- Here i am getting. Message like "array" why not i am getting the error message instead? --> 
<?php 
    }
?> 
4

3 回答 3

3

我会推荐使用 form_validation 的 validation_errors()。这是我的做法。
看看这个答案中的图书馆。
这个库有两个方法 validate_upload(它只检查文件是否有效)
和 do_upload(必须仅在 validate_upload 返回 true 时使用)。

Codeigniter 中有一个文件上传库,这里是文档。复制我的答案代码并将其粘贴到名为 MY_Upload.php 的文件中,并将此文件保存在 application/Code 文件夹中。现在我定义这样的规则

$this->form_validation->set_rules('userfile', 'New Image', 'trim|callback_valid_upload');   

当图像上传是可选的时,您可以将其包装在一个条件中

if(isset($_FILES['userfile']) AND $_FILES['userfile']['name']!= ''){
    $this->form_validation->set_rules('userfile', 'New Image', 'trim|callback_valid_upload');   
}   

其中 userfile 是表单的文件字段。你可以看到我在规则 callback_valid_upload 中调用了一个函数
这是回调中使用的控制器方法

public function valid_upload()
{
    $this->load->library('upload'); 

    $config['upload_path']      =   'your path here';           
    $config['allowed_types']    =   'png';
    $config['max_size']         =   2048;
    $config['max_width']        =   85;
    $config['max_height']       =   110;

    $this->upload->initialize($config);

    if (!$this->upload->validate_upload('userfile'))
    {
        $this->form_validation->set_message('valid_upload', $this->upload->display_errors());
        return FALSE;
    }else{
        return TRUE;
    }       
}

此方法将检查我们上传的文件是否有效,成功则返回 true,否则返回 false。
验证失败时加载表单

View

echo validation_errors();
//blah blah
//<form action="">
//  <input type="" />
//  <input type="" />
//  <input type="" />
//</form>

如果你想单独显示消息

<input type="file" name="userfile" id="" /> 
<?php echo form_error('userfile')?>

如果验证成功,现在像这样上传文件。

if($this->form_validation->run())
{
    if(isset($_FILES['userfile']) AND $_FILES['userfile']['name'] !='')
    {
        $this->upload->do_upload('userfile'); // this will upload file
        $image_data =   $this->upload->data();
        $data['image_field_name_of_table']      =   $image_data['raw_name'];
    }
    //other data in $data array here

    $id =   $this->mymodel->insert($data);
    if($id){
        $this->session->set_flashdata('notice', ' Successful');
        redirect('your url');   
    }
}else{
    //load view here
}    

更多编辑:
我在您的代码中注意到的一件事是一个问题

   $config = array(); 
   $config['base_url'] = 'http://localhost/thexxr.com/Booksetups/book/pgn/';
   $config["total_rows"] = $this->Booksmodel->record_count(); 
   $config['uri_segment'] = 4; 
   $config['per_page'] = 5;  
   $config['full_tag_open'] = '<div id="pagination">';
   $config['full_tag_close'] = '</div>';

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

    unset($config);

    $config['upload_path'] = 'uploads/'; 
    $config['allowed_types'] = 'gif|jpg|jpeg|png'; 
    $config['max_size'] = '1000'; 
    $config['max_width'] = '1920'; 
    $config['max_height'] = '1280'; 
    $config['remove_spaces'] = 'TRUE'; 
    $config['file_path'] = 'TRUE'; 
    $config['full_path']    = '/uploads/';

    $this->load->library('upload', $config); //load again with new configuration
    $this->upload->do_upload("img1");
    $this->upload->do_upload("img2");   
于 2013-01-21T13:52:46.423 回答
0

这是我在 CI 中设置和显示错误的一些辅助函数

if (!function_exists('set_flash_message'))
{
    function set_flash_message($title, $message, $type='success')
    {
        $CI =& get_instance();
        $CI->session->set_flashdata(
            'flash_message',
            array(
                'type' => $type,
                'title' => $title,
                'text' => $message));
    }
}

if (!function_exists('flash_message'))
{
    function flash_message()
    {
        $CI =& get_instance();
        $message = $CI->session->flashdata('flash_message');
        return $CI->load->view('desktop/flash_message', $message, TRUE);
    }
}

您可以从控制器设置消息:

set_flash_message('Error', 'Something went wrong', 'error');

并从视图中显示错误

<?php echo flash_message(); ?>
于 2013-01-21T12:30:06.123 回答
0

正如我在评论中所说;

您正在获取数组,因为您将闪存错误消息设置为数组 [ $error = array('error' => $this->upload->display_errors());],请尝试将其设置为字符串

如果您查看源代码中的函数,它要么采用哈希数组,要么采用字符串。
如果传入一个数组,那么它会将带有key的flashdata设置为flash消息类型,带有value的message;忽略第二个参数。
如果您将两个参数作为字符串传递,它将使用第一个字符串设置 flashdata 键。

所以要么做;

$this->session->set_flashdata('error', 'something went wrong');

或者

$this->session->set_flashdata(array('error', 'something went wrong'));

源代码:

function set_flashdata($newdata = array(), $newval = '')
    {
        if (is_string($newdata))
        {
            $newdata = array($newdata => $newval);
        }

        if (count($newdata) > 0)
        {
            foreach ($newdata as $key => $val)
            {
                $flashdata_key = $this->flashdata_key.':new:'.$key;
                $this->set_userdata($flashdata_key, $val);
            }
        }
    }
于 2013-01-21T13:40:24.800 回答