我在codeigniter中使用rest api 在config / form-validation.php文件中,有所有验证信息,这是我的代码
$config = array(
'usersignup' => array(
array('field' => 'first_name', 'label' => 'First Name', 'rules' => 'trim|required'),
array('field' => 'last_name', 'label' => 'Last Name', 'rules' => 'trim|required'),
array('field' => 'username', 'label' => 'Username', 'rules' => 'trim|required'),
array('field' => 'password', 'label' => 'Password', 'rules' => 'trim|required'),
array('field' => 'user_type', 'label' => 'User Type', 'rules' => 'trim|required'),
),
)
我如何验证图像?
答案 0 :(得分:0)
实际上,这取决于您在上传图像时实际要验证的内容。如果只想检查是否选择了图像,可以通过这种方式进行
if(isset($_FILES["fileToUpload"])) {
// Image is selected
}
如果您还想检查mime类型,请执行此操作,
if(isset($_FILES["fileToUpload"])) {
$check = getimagesize($_FILES["fileToUpload"]["tmp_name"]);
if($check !== false) {
echo "File is an image - " . $check["mime"] . ".";
} else {
echo "File is not an image.";
}
}
答案 1 :(得分:0)
您应该看到一个上传表单。尝试上传图像文件(jpg,gif或png)。如果控制器中的路径正确,则应该可以。
<?php
class Upload extends CI_Controller {
public function __construct()
{
parent::__construct();
$this->load->helper(array('form', 'url'));
}
public function index()
{
$this->load->view('upload_form', array('error' => ' ' ));
}
public function do_upload()
{
$config['upload_path'] = './uploads/';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = 100;
$config['max_width'] = 1024;
$config['max_height'] = 768;
$this->load->library('upload', $config);
if ( ! $this->upload->do_upload('userfile'))
{
$error = array('error' => $this->upload->display_errors());
$this->load->view('upload_form', $error);
}
else
{
$data = array('upload_data' => $this->upload->data());
$this->load->view('upload_success', $data);
}
}
}
?>
您需要一个用于上传图像的目标目录。在CodeIgniter安装的根目录下创建一个名为uploads的目录,并将其文件权限设置为777。
有关文件上传的更多信息,请参见链接https://www.codeigniter.com/userguide3/libraries/file_uploading.html#preferences