无法在codeigniter的查看页面上显示文件错误消息 [英] unable to show file error message on the view page in codeigniter
问题描述
我正在尝试使用flashdata
显示图像上传错误消息,但是下面显示的代码无法正常工作.可能是什么原因?
i am trying to show image upload error message using flashdata
but the code which i shown below is not working fine. What could be the reason?
请向我建议解决此问题的解决方案.
Please suggest me a solution to solve this issue.
mycontrollerPage.php
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
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时使用).
I will recommend using form_validation's validation_errors(). Here is how i do it.
Take a look at the library in this answer.
This library has two methods validate_upload (it only checks if file is valid)
and do_upload(must be used only when validate_upload returns true).
Codeigniter中有一个文件上传库,此处是文档. 复制我的答案的代码,并将其粘贴到名为MY_Upload.php的文件中,然后将此文件保存在application/Code文件夹中. 现在,我定义了这样的规则
There is a file upload library available in Codeigniter Here is the documentation. Copy the code of my answer and paste it in a file called MY_Upload.php and save this file in application/Code folder. And now i define a rule like this
$this->form_validation->set_rules('userfile', 'New Image', 'trim|callback_valid_upload');
当图片上传是可选的时,您可以将其包装成一个条件
When the image upload is optional you can wrap it in a condition
if(isset($_FILES['userfile']) AND $_FILES['userfile']['name']!= ''){
$this->form_validation->set_rules('userfile', 'New Image', 'trim|callback_valid_upload');
}
其中userfile是表单的文件字段.
您可以看到我在规则callback_valid_upload
中调用了一个函数
这是回调中使用的控制器方法
Where userfile is file field of form.
You can see i have called a function in rule callback_valid_upload
Here is method of controller used in callback
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.
验证失败后,加载表格
This method will check if file we are uploading is valid and will return true on success else false.
When validation is failed load form
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')?>
如果验证成功,现在就可以像这样上传文件.
And if validation is successfull upload file now like this.
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
}
更多
我在您的代码中注意到的一件事是一个问题
MORE EDITS :
One thing i noticed in your code is a problem
$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");
这篇关于无法在codeigniter的查看页面上显示文件错误消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!