我正在尝试使用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
}
?>
答案 0 :(得分:3)
我建议使用form_validation的validation_errors()。我就是这样做的。
在this answer中查看图书馆
这个库有两个方法validate_upload(它只检查文件是否有效)
和do_upload(仅当validate_upload返回true时才能使用)。
Codeigniter中有一个文件上传库Here是文档。 复制我的答案的代码并将其粘贴到名为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");
答案 1 :(得分: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(); ?>
答案 2 :(得分:0)
正如我在评论中所说;
您正在获取阵列,因为您正在设置Flash错误消息 作为数组[
$error = array('error' => $this->upload->display_errors());
],尝试将其设置为字符串
如果你看一下源代码中的函数,它需要一个哈希数组或一个字符串
如果传递了一个数组,那么它将使用密钥设置flashdata作为flash消息类型,消息带有值;忽略第二个参数。
如果将两个参数作为字符串传递,它将使用第一个字符串设置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);
}
}
}