[英]unable to show file error message on the view page in codeigniter
我正在嘗試使用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());
}
}
}
Myview.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
}
?>
我將建議使用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");
這是我的一些幫助函數,用於設置和顯示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(); ?>
正如我在評論中所說的;
您正在獲取數組,因為您將Flash錯誤消息設置為數組[
$error = array('error' => $this->upload->display_errors());
],嘗試將其設置為僅字符串
如果您在源代碼中查看該函數,則該函數將采用哈希數組或字符串。
如果傳遞了數組,則它將使用鍵將flashdata設置為flash消息類型,message設置為value; 忽略第二個參數。
如果您將兩個參數作為字符串傳遞,它將使用第一個字符串設置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);
}
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.