Php 无法在codeigniter的查看页面上显示文件错误消息
我试图使用Php 无法在codeigniter的查看页面上显示文件错误消息,php,codeigniter,file-upload,Php,Codeigniter,File Upload,我试图使用flashdata显示图像上载错误消息,但下面显示的代码工作不正常。原因可能是什么 请给我一个解决这个问题的办法 mycontrollerPage.php class Booksetups extends CI_Controller { function book($book_id = 0) { $config = array(); $config['base_url'] = 'http://localhost/thexxr.com/B
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());
}
}
}
类书籍设置扩展CI_控制器
{
功能书($book\u id=0)
{
$config=array();
$config['base_url']='http://localhost/thexxr.com/Booksetups/book/pgn/';
$config[“总计行数”]=$this->Booksmodel->record_count();
$config['uri_segment']=4;
$config['per_page']=5;
$config['full_tag_open']='';
$config['full_tag_close']='';
//------------未写入文件上载错误验证------------------------------------------
$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('','')->set_rules('subject_id','subject_id','trim | required | min_length[1]| max_length[150]| xss_clean');
$this->form_validation->set_error_delimiters('','')->set_rules('cover_id','cover_id','trim | required | min_length[1]| max_length[150]| xss_clean');
$this->form_validation->set_error_delimiters('','')->set_rules('language_id','language_id','trim | required | min_length[1]| max_length[150]| xss_clean');
$this->form_validation->set_error_delimiters('','')->set_rules('edition_id','edition_id','trim | required | min_length[1]| max_length[150]| xss_clean');
如果($this->input->post('book_id'))&($this->form_validation->run()==TRUE))
{
如果(!$this->upload->do_upload())//试图显示错误
{
$error=array('error'=>$this->upload->display_errors());
$this->session->set_flashdata('error',$error);
重定向(当前url());
}
$this->session->set_flashdata('msg','1行受影响');
$this->Booksmodel->entry_update();
重定向(当前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
}
?>
以下是一些my helper函数,用于设置和显示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(); ?>
以下是一些my helper函数,用于设置和显示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(); ?>
正如我在评论中所说
您正在获取阵列,因为您正在设置闪存错误消息 作为数组[
$error=array('error'=>
$this->upload->display_errors();
],尝试将其设置为一个字符串
如果查看源代码中的函数,它要么接受哈希数组,要么接受字符串。如果传递了一个数组,那么它将使用键将flashdata设置为FlashMessage类型,message设置为value;忽略第二个参数。
如果将两个参数作为字符串传递,它将使用第一个字符串设置flashdata键 也可以这样做
$this->session->set_flashdata('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);
}
}
}
正如我在评论中所说
您正在获取阵列,因为您正在设置闪存错误消息 作为数组[
$error=array('error'=>
$this->upload->display_errors();
],尝试将其设置为一个字符串
如果查看源代码中的函数,它要么接受哈希数组,要么接受字符串。如果传递了一个数组,那么它将使用键将flashdata设置为FlashMessage类型,message设置为value;忽略第二个参数。
如果将两个参数作为字符串传递,它将使用第一个字符串设置flashdata键 也可以这样做
$this->session->set_flashdata('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);
}
}
}
我建议使用form_validation的validation_errors()。我是这样做的。
请查看中的库。
此库有两种方法验证上传(它只检查文件是否有效)
和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是表单的文件字段。
您可以看到我在规则回调\u valid\u 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>
更多编辑:我注意到您的代码中有一个问题
$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");
$config=array();
$config['base_url']='http://localhost/thexxr.com/Booksetups/book/pgn/';
$config[“总计行数”]=$this->Booksmodel->record_count();
$config['uri_segment']=4;
$config['per_page']=5;
$config['full_tag_open']='';
$config['full_tag_close']='';
$this->load->library('upload',$config);
$this->upload->do_upload(“img1”);
未设置($配置);
$config['upload_path']=“uploads/”;
$config['allowed_types']='gif | jpg | jpeg | png';
$config['max_size']='1000';
$config['max_width']='1920';
$config['max_height']='1280';