問題描述
我正在嘗試驗證圖片上傳的文件上傳,但它沒有像其他字段一樣得到驗證.我正在使用 Form_Validation.php
過程進(jìn)行驗證.
I am trying to validate file upload for image uploading, but it is not getting the validation like other fields. I am using Form_Validation.php
process for validation.
圖片上傳數(shù)組:
array(
'field'=>'image',
'label' => 'Image',
'rules' => 'required'
)
當(dāng)我嘗試上傳圖像時,它沒有像需要等那樣響應(yīng).我還想驗證它的 .jpg
等以及如何在不正確的文件上設(shè)置文件值,例如.jpg
我們嘗試上傳 .pdf
" 就像我們設(shè)置輸入字段的值 set_value('field name')
等.
when i try to upload the image it did not response like it is required etc. I also want to validate it for .jpg
etc and "how to set the file value on incorrect file like instead of .jpg
we try to upload the .pdf
" like we set the value of input field set_value('field name')
etc.
我查了很多問題,也嘗試使用call方法,但沒能解決.
I checked a lot of questions and also try to use call method, but did not able to fix it.
更新:
請?zhí)峁в写a示例的詳細(xì)答案.請在示例中使用 form_validation.php 方式并提供回調(diào)示例代碼,以便我可以閱讀/學(xué)習(xí)并相應(yīng)地修改它.
Please provide detail answer with code example. Please use the form_validation.php way in example and also provide the callback example code, so i can read / learn and modify it accordingly.
更新 2:
public function Task()
{
if ($this->form_validation->run('Sub_Admin/task') == FALSE) {
$this->data['Task'] = $this->bm->get_usr();
$data['title'] = "Add New Task";
$this->load->view('Subadmin/header',$data);
$this->load->view('Subadmin/nav');
$this->load->view('Subadmin/sidebar');
$this->load->view('Subadmin/task', $this->data);
$this->load->view('Subadmin/footer');
}
else
{
$config['upload_path'] = './taskimages/'; //The path where the image will be save
$config['allowed_types'] = 'gif|jpg|png'; //Images extensions accepted
$config['max_size'] ='10048'; //The max size of the image in kb's
//$config['max_width'] = '1024'; //The max of the images width in px
//$config['max_height'] = '768'; //The max of the images height in px
$config['overwrite'] = FALSE; //If exists an image with the same name it will overwrite. Set to false if don't want to overwrite
$this->load->library('upload', $config); //Load the upload CI library
$this->load->initialize($config);
$this->upload->do_upload('task');
$file_info = $this->upload->data();
$file_name = $file_info['file_name'];
$data = array(
'Job_Title' => $this->input->post('jtitle'),
'Priority' => $this->input->post('jnature'),
'Assignee' => $this->input->post('assigne'),
'Employee_Name' => $this->input->post('assignto'),
'Due_Date' => $this->input->post('ddate'),
'Reminder' => $this->input->post('reminder'),
'Task_Image' => $file_name,
);
$this->bm->add_task($data);
}
}
我已經(jīng)在使用 CI 上傳類但它不起作用,現(xiàn)在我想從 form_validation 端驗證圖像/文件.
推薦答案
我為你的問題寫了一個完整的例子,希望對你有所幫助.在以下代碼中,我使用了 CI 的表單驗證回調(diào)和表單驗證自定義錯誤消息.
I wrote a complete example for your problem, I hope it will help. In the following code I am using CI's Form validation callback and Form Validation Custom Error Messages.
控制器: Front.php
Controller: Front.php
class Front 擴展 CI_Controller {
class Front extends CI_Controller {
public function index() {
$this->load->view('form');
}
public function upload_image() {
$this->load->library('form_validation');
if ($this->form_validation->run('user_data') == FALSE) {
$this->load->view('form');
}
else {
echo 'You form Submitted Successfully ';
}
}
public function validate_image() {
$check = TRUE;
if ((!isset($_FILES['my_image'])) || $_FILES['my_image']['size'] == 0) {
$this->form_validation->set_message('validate_image', 'The {field} field is required');
$check = FALSE;
}
else if (isset($_FILES['my_image']) && $_FILES['my_image']['size'] != 0) {
$allowedExts = array("gif", "jpeg", "jpg", "png", "JPG", "JPEG", "GIF", "PNG");
$allowedTypes = array(IMAGETYPE_PNG, IMAGETYPE_JPEG, IMAGETYPE_GIF);
$extension = pathinfo($_FILES["my_image"]["name"], PATHINFO_EXTENSION);
$detectedType = exif_imagetype($_FILES['my_image']['tmp_name']);
$type = $_FILES['my_image']['type'];
if (!in_array($detectedType, $allowedTypes)) {
$this->form_validation->set_message('validate_image', 'Invalid Image Content!');
$check = FALSE;
}
if(filesize($_FILES['my_image']['tmp_name']) > 2000000) {
$this->form_validation->set_message('validate_image', 'The Image file size shoud not exceed 20MB!');
$check = FALSE;
}
if(!in_array($extension, $allowedExts)) {
$this->form_validation->set_message('validate_image', "Invalid file extension {$extension}");
$check = FALSE;
}
}
return $check;
}
}
查看: form.php
<!DOCTYPE html>
<html>
<head>
<title>Image Upload</title>
</head>
<body>
<h1><a href="<?= base_url() ?>">Form</a></h1>
<?php if(!empty(validation_errors())): ?>
<p><?= validation_errors() ?></p>
<?php endif; ?>
<?= form_open('front/upload_image', ['enctype' => "multipart/form-data"]) ?>
<label>Name: </label><input type="text" name="name" value="<?= set_value('name') ?>"></label>
<label>E-mail: </label><input type="email" name="email" value="<?= set_value('email') ?>"></label>
<input type="file" name="my_image">
<button type="submit">Submit</button>
<?= form_close() ?>
</body>
</html>
form_validation.php
$config = array(
'user_data' => array(
array(
'field' => 'name',
'label' => 'Name',
'rules' => 'trim|required'
),
array(
'field' => 'email',
'label' => 'Email',
'rules' => 'trim|required|valid_email'
),
array(
'field' => 'my_image',
'label' => 'Image',
'rules' => 'callback_validate_image'
)
)
);
在上面的例子中,首先我驗證了 name
和 email
并且對于圖像我調(diào)用了 validate_image
函數(shù)來驗證它,因為form_validation 庫不提供圖像驗證,但我有回調(diào)來進(jìn)行自定義驗證,validate_image
將檢查圖像內(nèi)容類型,然后檢查圖像文件大小,然后檢查圖像擴展名,如果沒有滿足任何這些要求,它會使用 form_validation
庫的 set_message()
函數(shù)為每個需求設(shè)置錯誤消息.
In above example first I am validating the name
and email
and for the Image I am calling the validate_image
function to validate it, since form_validation library does not provide image validation but i has callbacks to do custom validations, the validate_image
will check image content type then check image file size and then check image extension if any of these requirements are not fulfilled it will set error message for each requirement using set_message()
function of form_validation
library.
這篇關(guān)于Codeigniter 中的文件上傳驗證的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!