Home  >  Article  >  Backend Development  >  Using CodeIgniter's class library to upload images_PHP tutorial

Using CodeIgniter's class library to upload images_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 10:33:58870browse

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