search
HomeBackend DevelopmentPHP TutorialUsing CodeIgniter's class library to upload images_PHP tutorial

CodeIgniter's file upload class allows files to be uploaded. You can set up to upload files of a certain type and size.

The common process for uploading files:

  • A form for uploading files, allowing the user to select a file and upload it.
  • When this form is submitted, the file is uploaded to the specified directory.
  • At the same time, the document will be verified to see if it meets the requirements you set.
  • Once the file is uploaded successfully, a confirmation window indicating successful upload will be returned.

Here is the form:

<form method="post" action="<?=base_url()?>admin/img_upload/" enctype="multipart/form-data" />
	<div style="margin:0 0 0.5em 0em;">
		<input type="file" name="userfile" size="20" class="button" />
		<input type="submit" value=" 上传 " class="button" />
	</div>
</form>

Then the following is the upload class:

	public function img_upload()
	{
		$this->load->helper('url');
		
		$config['upload_path'] = './images/'.date('Ym', time()).'/';
		$config['allowed_types'] = 'gif|jpg|png';
		$config['file_name'] = date('Y_m_d', time()).'_'.sprintf('%02d', rand(0,99));
		$config['max_size'] = '500';
		$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());
  		} 
		else
  		{
   			$data = array('upload_data' => $this->upload->data());
  		}
	}

Preference parameters

Preferences Defaults Options Description
upload_path None None File upload path. The path must be writable, both relative and absolute paths are acceptable.
allowed_types None None MIME types that allow uploading files; usually file extensions can As a MIME type. Allow multiple types separated by vertical bars '|'
file_name None Want to use File name
偏好设置 默认值 选项 描述
upload_path None None 文件上传路径。该路径必须是可写的,相对路径和绝对路径均可以。
allowed_types None None 允许上传文件的MIME类型;通常文件扩展名可以做为MIME类型. 允许多个类型用竖线‘|’分开
file_name None 想要使用的文件名

如果设置了这个参数,CodeIgniter 将根据这里设置的文件名来对上传的文件进行重命名。文件名中的扩展名也必须是允许的文件类型。

overwrite FALSE TRUE/FALSE (boolean) 是否覆盖。该参数为TRUE时,如果上传文件时碰到重名文件,将会把原文件覆盖;如果该参数为FALSE,CI将会在新文件的文件名后面加一个数字。If set to true, if a file with the same name as the one you are uploading exists, it will be overwritten. If set to false, a number will be appended to the filename if another with the same name exists.
max_size 0 None 允许上传文件大小的最大值(以K为单位)。该参数为0则不限制。注意:通常PHP也有这项限制,可以在php.ini文件中指定。通常默认为2MB。
max_width 0 None 上传文件的宽度最大值(像素为单位)。0为不限制。
max_height 0 None 上传文件的高度最大值(像素为单位)。0为不限制。
max_filename 0 None 文件名的最大长度。0为不限制。
encrypt_name FALSE TRUE/FALSE (boolean) 是否重命名文件。如果该参数为TRUE,上传的文件将被重命名为随机的加密字符串。当你想让文件上传者也不能区分自己上传的文件的文件名时,是非常有用的。当 overwrite 为 FALSE 时,此选项才起作用。
remove_spaces TRUE TRUE/FALSE (boolean) 参数为TRUE时,文件名中的空格将被替换为下划线。推荐使用。
If this parameter is set, CodeIgniter will rename the uploaded file according to the file name set here. The extension in the file name must also be an allowed file type.

overwrite FALSE TRUE/FALSE (boolean) Whether to overwrite. When this parameter is TRUE, if a file with the same name is encountered when uploading a file, the original file will be overwritten; if this parameter is FALSE, CI will add a number after the file name of the new file. If set to true, if a file with the same name as the one you are uploading exists, it will be overwritten. If set to false, a number will be appended to the filename if another with the same name exists.
max_size 0 None The maximum allowed upload file size (in K). If this parameter is 0, there is no limit. Note: Usually PHP also has this restriction, which can be specified in the php.ini file. Usually the default is 2MB.
max_width 0 None The maximum width of the uploaded file (in pixels). 0 means no limit.
max_height 0 None The maximum height of the uploaded file (in pixels). 0 means no limit.
max_filename 0 None The maximum length of the file name. 0 means no limit.
encrypt_name FALSE TRUE/FALSE (boolean) Whether to rename the file. If this parameter is TRUE, the uploaded file will be renamed to a random encrypted string. This is very useful when you want the file uploader to be unable to distinguish the file names of the files they upload. This option only works when overwrite is FALSE.
remove_spaces TRUE TRUE/FALSE (boolean) When the parameter is TRUE, the file name Spaces in will be replaced with underscores. Recommended.
Several used functions
  • $this->upload->do_upload(): Perform operations based on your preferred configuration parameters. Note: By default, the uploaded file comes from the file field named userfile
  • in the submission form, and the form must be of type "multipart".
  • $this->upload->display_errors(): If do_upload()
  • returns failure, an error message will be displayed. This function does not automatically output, but returns data, so you can arrange it however you want.
  • $this->upload->data(): This is a helper function that returns an array of all relevant information about the file you uploaded.

http://www.bkjia.com/PHPjc/752371.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/752371.htmlTechArticleCodeIgniter’s file upload class allows files to be uploaded. You can set up to upload files of a certain type and size. The common process of uploading files: One way to upload files...
Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
如何在CodeIgniter中实现自定义中间件如何在CodeIgniter中实现自定义中间件Jul 29, 2023 am 10:53 AM

如何在CodeIgniter中实现自定义中间件引言:在现代的Web开发中,中间件在应用程序中起着至关重要的作用。它们可以用来执行在请求到达控制器之前或之后执行一些共享的处理逻辑。CodeIgniter作为一个流行的PHP框架,也支持中间件的使用。本文将介绍如何在CodeIgniter中实现自定义中间件,并提供一个简单的代码示例。中间件概述:中间件是一种在请求

CodeIgniter中间件:加速应用程序的响应速度和页面渲染CodeIgniter中间件:加速应用程序的响应速度和页面渲染Jul 28, 2023 pm 06:51 PM

CodeIgniter中间件:加速应用程序的响应速度和页面渲染概述:随着网络应用程序的复杂性和交互性不断增长,开发人员需要使用更加高效和可扩展的解决方案来提高应用程序的性能和响应速度。CodeIgniter(CI)是一种基于PHP的轻量级框架,提供了许多有用的功能,其中之一就是中间件。中间件是在请求到达控制器之前或之后执行的一系列任务。这篇文章将介绍如何使用

在CodeIgniter框架中使用数据库查询构建器(Query Builder)的方法在CodeIgniter框架中使用数据库查询构建器(Query Builder)的方法Jul 28, 2023 pm 11:13 PM

在CodeIgniter框架中使用数据库查询构建器(QueryBuilder)的方法引言:CodeIgniter是一个轻量级的PHP框架,它提供了许多功能强大的工具和库,方便开发人员进行Web应用程序开发。其中一个令人印象深刻的功能是数据库查询构建器(QueryBuilder),它提供了一种简洁而强大的方法来构建和执行数据库查询语句。本文将介绍如何在Co

php如何使用CodeIgniter5框架?php如何使用CodeIgniter5框架?Jun 01, 2023 am 11:21 AM

CodeIgniter是一个轻量级的PHP框架,采用MVC架构,支持快速开发和简化常见任务。CodeIgniter5是该框架的最新版本,提供了许多新的特性和改进。本文将介绍如何使用CodeIgniter5框架来构建一个简单的Web应用程序。步骤1:安装CodeIgniter5下载和安装CodeIgniter5非常简单,只需要遵循以下步骤:下载最新版本

PHP开发:使用 CodeIgniter 实现 MVC 模式和 RESTful APIPHP开发:使用 CodeIgniter 实现 MVC 模式和 RESTful APIJun 16, 2023 am 08:09 AM

随着Web应用程序的不断发展,更加快速和高效地开发应用程序变得非常重要。并且,随着RESTfulAPI在Web应用程序中的广泛应用,对于开发人员来说,必须理解如何创建和实现RESTfulAPI。在本文中,我们将讨论如何使用CodeIgniter框架实现MVC模式和RESTfulAPI。MVC模式简介MVC(Model-Vie

使用PHP框架CodeIgniter开发一个实时聊天应用,提供便捷的通讯服务使用PHP框架CodeIgniter开发一个实时聊天应用,提供便捷的通讯服务Jun 27, 2023 pm 02:49 PM

随着移动互联网的发展,即时通信变得越来越重要,越来越普及。对于很多企业而言,实时聊天更像是一种通信服务,提供便捷的沟通方式,可以快速有效地解决业务方面的问题。基于此,本文将介绍如何使用PHP框架CodeIgniter开发一个实时聊天应用。了解CodeIgniter框架CodeIgniter是一个轻量级的PHP框架,提供了一系列的简便的工具和库,帮助开发者快速

如何使用PHP框架CodeIgniter快速搭建一个后台管理系统如何使用PHP框架CodeIgniter快速搭建一个后台管理系统Jun 27, 2023 am 09:46 AM

现今互联网时代,一款深受用户喜爱的网站必须具备简洁明了的前端界面和功能强大的后台管理系统,而PHP框架CodeIgniter则是一款能够让开发者快速搭建后台管理系统的优秀框架。CodeIgniter拥有轻量级、高效率、易扩展等特点,本文将针对初学者,详细说明如何通过该框架快速搭建一个后台管理系统。一、安装配置安装PHPCodeIgniter是一个基于PHP的

php如何使用CodeIgniter\Config进行配置管理?php如何使用CodeIgniter\Config进行配置管理?Jun 02, 2023 pm 06:01 PM

一、CodeIgniter简介CodeIgniter是一个轻量级且全面的PHP开发框架,旨在为Web开发人员提供快速且强大的工具来构建Web应用程序。它是一个开源的框架,使用MVC架构模式来实现快速开发和基础功能,同时支持多种数据库。二、Config库简介Config库是CodeIgniter框架中的一个组件,用于对代码进行配置管理。Config库包含了很多

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

Atom editor mac version download

Atom editor mac version download

The most popular open source editor