我想检查用户是否在我的文本区放了什么东西,或者他是否添加了图像。如何在codeigniter中完成?
我的场景是,他可以发布文本和图像,也可以只发布文本或只发布图像,但其中任何一个都不能为空。
但当我尝试时,它不起作用。尽管我添加了一个图像,但我总是得到一个错误,即2都是空的。
变量:
text区域=机身
file=image_upload
if ($_POST) {
if (empty($_POST['body']) == true && !isset($_POST['image_upload']))
{
//no message & image
header("location:". $_POST['base_url'] . '?error_post');
}
elseif(empty($_POST['body']) == false && !isset($_POST['image_upload']))
{
//message only
}
elseif (empty($_POST['body']) == true && isset($_POST['image_upload']) == true) {
//image only
$config['upload_path'] = './public/uploads/';
$config['allowed_types'] = 'gif|jpg|png|jpeg';
$config['max_size'] = '100';
$config['max_width'] = '1024';
$config['max_height'] = '768';
$this->load->library('upload', $config);
if ( ! $this->upload->do_upload())
{
$error = array('error' => $this->upload->display_errors());
//$this->load->view('upload_form', $error);
var_dump($error);
die();
}
else
{
$data = array('upload_data' => $this->upload->data());
//$this->load->view('upload_success', $data);
var_dump($data);
die();
}
}
else{
//both message & image
var_dump($_POST);
}
}
form_validation可以帮助您,https://ellislab.com/codeigniter/user-guide/libraries/form_validation.html
试试这样的东西:
<?php
$this->form_validation->set_rules('body','body','required');
$config['upload_path'] = './public/uploads/';
$config['allowed_types'] = 'gif|jpg|png|jpeg';
$config['max_size'] = '100';
$config['max_width'] = '1024';
$config['max_height'] = '768';
$this->upload->initialize($config);
$this->load->library('upload', $config);
if($this->form_validation->run())
{
$image_data = $this->upload->data();
$image_name = $image_data['file_name'];
$data = array(
'image_upload'=>$image_name,
'body'=>$this->input->post('body')
);
$this->MyModel->create($data);
redirect('...');
}else{
if(!$this->upload->do_upload('image_upload'))
{
redirect('...');
}
else
{
$image_data = $this->upload->data();
$image_name = $image_data['file_name'];
$data = array(
'image_upload'=>$image_name
);
$this->MyModel->create($data);
redirect('...');
}
}