Use the CodeIgniter class library to upload images _ PHP Tutorial

Source: Internet
Author: User
Use the CodeIgniter class library to upload images. CodeIgniter's file Upload class allows files to be uploaded. You can specify the file type to be uploaded and the file size to be specified. A common process of uploading files: CodeIgniter files used to upload files can be uploaded. You can specify the file type to be uploaded and the file size to be specified.

The general process of uploading files:

  • A form used to upload a file. you can select a file and upload it.
  • When this form is submitted, the file is uploaded to the specified directory.
  • At the same time, the file will be verified to meet your set requirements.
  • Once the file is uploaded successfully, a confirmation window is returned.

The following is a form:

 

Next 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
Preference settings Default value Option Description
Upload_path None None File upload path. The path must be writable. the relative path and absolute path can both be used.
Allowed_types None None The MIME type of the file to be uploaded. Generally, the file extension can be used as the MIME type. multiple types can be separated by vertical bars (| ).
File_name None File name to be used

If this parameter is set, CodeIgniter will rename the uploaded file based on the file name set here. The file name extension must also be of the allowed file type.

Overwrite FALSE TRUE/FALSE (boolean) Overwrite or not. If this parameter is set to TRUE, the original file will be overwritten if the file is uploaded with a duplicate name. If this parameter is set to 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 Maximum size of the file to be uploaded (in K ). If this parameter is set to 0, no restriction is imposed. Note: PHP also has this restriction, which can be specified in the php. ini file. The default value is 2 MB.
Max_width 0 None The maximum width (in pixels) of the uploaded file ). 0 is unlimited.
Max_height 0 None The maximum height (in pixels) of the uploaded file ). 0 is unlimited.
Max_filename 0 None The maximum length of a file name. 0 is unlimited.
Encrypt_name FALSE TRUE/FALSE (boolean) Whether to rename the file. If this parameter is set to TRUE, the uploaded file is renamed as a random encrypted string. It is useful when you want the file uploader not to distinguish the file name uploaded by himself. This option takes effect only when overwrite is FALSE.
Remove_spaces TRUE TRUE/FALSE (boolean) If the parameter is TRUE, the space in the file name is replaced with an underscore. Recommended.
Several functions used
  • $ This-> upload-> do_upload (): configure parameters according to your preference to perform the operation. Note: By default, the uploaded file is namedUserfileAnd the form must be of the "multipart" type.
  • $ This-> upload-> display_errors (): ifDo_upload ()An error is returned. This function does not automatically output but returns data, so you can arrange it as required.
  • $ This-> upload-> data (): this is an auxiliary function that returns an array of all relevant information about the uploaded file.

The file Upload class for the http://www.bkjia.com/PHPjc/752371.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/752371.htmlTechArticleCodeIgniter allows the file to be uploaded. You can specify the file type to be uploaded and the file size to be specified. The general process of uploading files: a file used for uploading...

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.