未加星标

CodeIgniter File Upload Validation

字体大小 | |
[开发(php) 所属分类 开发(php) | 发布者 店小二03 | 时间 2017 | 作者 红领巾 ] 0人收藏点击收藏

CodeIgniter provides a form validation class that helps to validate form fields by writing the minimal code. Generally, we are used required rule in CodeIgniter to validate a required form field. But required rule does not work when you tried to validate a file upload field in CodeIgniter. In this CodeIgniter tutorial, we will show you how to add validation to file upload in CodeIgniter .

For better understanding, we’ll demonstrate the complete file upload process with file upload validation . Using our example code you can implement file or image upload functionality with file input field validation in CodeIgniter .

Before you begin to implement CodeIgniter file upload validation take a look at the simple files and folder structure of this example script.


CodeIgniter File Upload Validation
Upload Directory

You need to create a directory to store the uploaded files. For example, this script uploads files to the uploads/files/ directory.

Controller (Files.php)

The Files controller contains 2 functions, upload() and file_check() .

The upload() function handles the following functionality.

Loads the file upload form. Validates file input field to check whether it is empty or the file type is not supported. Uploads the submitted file to the server.

The following built-in CodeIgniter library and helper are used to upload files with validation in CodeIgniter.

form_validation helps to validate form fields. file provides get_mime_by_extension() function to get the mime type of uploaded file. upload helps to upload the file to the server.

The file_check() is a validation callback function that checks whether the file input field is empty or selected file is not allowed.

<php if ( ! defined('BASEPATH')) exit('No direct script access allowed'); /** * Files management class created by CodexWorld */ class Files extends CI_Controller { function __construct() { parent::__construct(); } public function upload(){ $data = array(); //load form validation library $this->load->library('form_validation'); //load file helper $this->load->helper('file'); if($this->input->post('uploadFile')){ $this->form_validation->set_rules('file', '', 'callback_file_check'); if($this->form_validation->run() == true){ //upload configuration $config['upload_path'] = 'uploads/files/'; $config['allowed_types'] = 'gif|jpg|png|pdf'; $config['max_size'] = 1024; $this->load->library('upload', $config); //upload file to directory if($this->upload->do_upload('file')){ $uploadData = $this->upload->data(); $uploadedFile = $uploadData['file_name']; /* *insert file information into the database *....... */ $data['success_msg'] = 'File has been uploaded successfully.'; }else{ $data['error_msg'] = $this->upload->display_errors(); } } } //load the view $this->load->view('files/upload', $data); } /* * file value and type check during validation */ public function file_check($str){ $allowed_mime_type_arr = array('application/pdf','image/gif','image/jpeg','image/pjpeg','image/png','image/x-png'); $mime = get_mime_by_extension($_FILES['file']['name']); if(isset($_FILES['file']['name']) && $_FILES['file']['name']!=""){ if(in_array($mime, $allowed_mime_type_arr)){ return true; }else{ $this->form_validation->set_message('file_check', 'Please select only pdf/gif/jpg/png file.'); return false; } }else{ $this->form_validation->set_message('file_check', 'Please choose a file to upload.'); return false; } } } View (files/upload.php)

This view file contains upload form HTML that allows user to select a file for upload. The form is submitted to the upload() method of Files controller for validation and upload.

<?php if(!empty($success_msg)){ echo '<p class="statusMsg">'.$success_msg.'</p>'; }elseif(!empty($error_msg)){ echo '<p class="statusMsg">'.$error_msg.'</p>'; } ?> <form method="post" enctype="multipart/form-data"> <p><input type="file" name="file"/></p> <?php echo form_error('file','<p class="help-block">','</p>'); ?> <p><input type="submit" name="uploadFile" value="UPLOAD"/></p> </form> Conclusion

Here we’ve explained how you can validate a file input field in CodeIgniter. Also, the CodeIgniter file upload process is shown in this tutorial. In our example script, only GIF, PNG, JPG, and PDF files are allowed to upload, but you can change the allowed file types based on your requirement. If you want to upload multiple files in CodeIgniter, the following tutorial will help you to implement this functionality easily.

Upload Multiple Files in CodeIgniter

Download Source Code

本文开发(php)相关术语:php代码审计工具 php开发工程师 移动开发者大会 移动互联网开发 web开发工程师 软件开发流程 软件开发工程师

主题: HTMLEPA
分页:12
转载请注明
本文标题:CodeIgniter File Upload Validation
本站链接:http://www.codesec.net/view/522811.html
分享请点击:


1.凡CodeSecTeam转载的文章,均出自其它媒体或其他官网介绍,目的在于传递更多的信息,并不代表本站赞同其观点和其真实性负责;
2.转载的文章仅代表原创作者观点,与本站无关。其原创性以及文中陈述文字和内容未经本站证实,本站对该文以及其中全部或者部分内容、文字的真实性、完整性、及时性,不作出任何保证或承若;
3.如本站转载稿涉及版权等问题,请作者及时联系本站,我们会及时处理。
登录后可拥有收藏文章、关注作者等权限...
技术大类 技术大类 | 开发(php) | 评论(0) | 阅读(24)