PHP codeigniter File Upload Class code instance

Source: Internet
Author: User
Tags file size file upload php file true true codeigniter

CodeIgniter file Upload Class code instance

File Upload Class

CodeIgniter file Upload class allows files to be uploaded. You can set a file that specifies the upload of a file of a certain type and a specified size.

Processing process

The general process of uploading files:

A form for uploading files that allows the user to select a file and upload it.

When this form is submitted, the file is uploaded to the specified directory.

Also, the file will be validated to meet the requirements you set.

Once the file has been uploaded successfully, return a confirmation window to upload success.

Here's a short tutorial to show the process. Then you will find the relevant reference information.

Create an Upload form

Use a text editor to create a file named upload_form.php, copy the following code and save it in the applications/views/directory:

You will see here a form helper function to create the start tag of the form, and the file upload requires a multipart form, because this form helper function creates a suitable statement for you. You will also see that we have used a $error variable that will display the relevant error message when the user submits the form with an error.

Upload a successful page

Use a text editor to create a file named upload_success.php. Copy the following code to save in the applications/views/directory:

Your file was successfully uploaded!

$value):?>

:

Controller

Using a text editor, create a controller named upload.php. Copy the following code and save it to the applications/controllers/directory:

Load->helper (Array (' form ', ' url ')); function index () {$this->load->view (' Upload_form ', Array (' ERROR ' => '));} function Do_upload () {$config Upload_path '] = './uploads/'; $config [' allowed_types '] = ' gif|jpg|png '; $config [' max_size '] = ' 100 '; $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 ()); $this-& Gt;load->view (' Upload_form ', $error); else {$data = array (' upload_data ' => $this->upload->data ()); $this->load->view (' upload_success ', $ data); }}}?>

Upload file directory

You also need a target folder to store uploaded images. Create a file named uploads on the root directory and set the property of the file to 777. (Can read and write)

Submitting a form

To submit your form, enter a URL similar to the following:

example.com/index.php/upload/

You will see an upload form, optionally a (JPG, GIF, or PNG) picture to submit. If the path you set in the controller is correct, it will start to work.

Initialize file Upload class

Similar to other classes in CodeIgniter, the file upload class is initialized with the $this->load->library function in the controller:

$this->load->library (' upload ');

Once the file upload class is loaded, the object is referenced by the following method: $this->upload

Preference settings

Like other libraries, you will control the files to be uploaded based on your preferences, and in the controller, you set up the following preferences:

$config [' upload_path '] = './uploads/';

$config [' allowed_types '] = ' gif|jpg|png ';

$config [' max_size '] = ' 100 ';

$config [' max_width '] = ' 1024 ';

$config [' max_height '] = ' 768 ';

$this->load->library (' upload ', $config);

Alternately can set preferences by calling the Initialize function. Useful if you auto-load the class:

"If you automatically load the upload class in the autoload.php file in the Config folder, or if you load it within the constructor, you can invoke the initialization function initialize to load the settings." ———— in this bracket by the IT Tumbler translator, added its own understanding "

$this->upload->initialize ($config);

The above preferences will be fully implemented. The following is a description of all preferences parameters.

Preference Settings Parameters

The following preference settings parameters are available. When you do not specifically specify preferences, the default values are as follows:

Preferences Default value option description

Upload_path None None file Upload path. The path must be writable, with relative and absolute paths.

Allowed_types None None allows the MIME type of the uploaded file, which can normally be used as a MIME type. Allow multiple types to use vertical bars ' | ' Separate

file_name None of the file names you want to use

If this parameter is set, CodeIgniter will rename the uploaded file according to the filename set here. The extension in the file name must also be the allowed file type.

Overwrite FALSE True/false (Boolean) is overwritten. When this parameter is true, if the file is uploaded, the original file will be overwritten; If this argument is false, the CI will append a number to the file name of the new file when the filename is uploaded.

Max_size 0 None allows the maximum file size to be uploaded (in k). This parameter is 0 without restriction. Note: PHP also has this limitation, which can be specified in the php.ini file. Usually the default is 2MB.

Max_width 0 The maximum width of the uploaded file (in pixels). 0 is not limited.

Max_height 0 The maximum height of the uploaded file (in pixels). 0 is not limited.

The maximum length of the Max_filename 0 None file name. 0 is not limited.

Whether Encrypt_name FALSE True/false (Boolean) renames the file. If this argument is true, the uploaded file will be renamed to a random encrypted string. This is useful when you want to make file uploads unable to distinguish between file names that you upload. This option works when overwrite is FALSE.

When the Remove_spaces true True/false (Boolean) argument is true, the spaces in the file name are replaced with underscores. Recommended use.

Setting preferences parameters in a configuration file

If you do not want to apply the above method to the preferences, you can replace it with a configuration file. Simply create a file named upload.php, add $config array to the file, and then save the file to: config/upload.php, it will be automatically loaded. When you save the configuration parameters to the file, you do not need to use the $this->upload->initialize function for manual loading.

The function applied to

The following functions are used

$this->upload->do_upload ()

Perform actions according to your preferences configuration parameters. Note: The file that is uploaded by default is from the file field named UserFile in the submission form, and the form must be of type "multipart":

If you want to customize your own file domain name before executing the Do_upload function, you can do this by:

$field _name = "Some_field_name";

$this->upload->do_upload ($field _name)

$this->upload->display_errors ()

If Do_upload () returns a failure, an error message is displayed. This function does not automatically output, but returns the data, so you can arrange it according to your request.

Format error

The above function defaults to using the

Marks the error message. You can set your own separator like this.

$this->upload->display_errors ('

', '

');

$this->upload->data ()

This is an auxiliary function that returns an array of all relevant information about the file you are uploading.

Array

(

[file_name] => mypic.jpg

[File_type] => image/jpeg

[File_path] =>/path/to/your/upload/

[Full_path] =>/path/to/your/upload/jpg.jpg

[Raw_name] => mypic

[Orig_name] => mypic.jpg

[Client_name] => mypic.jpg

[File_ext] =>. jpg

[File_size] => 22.2

[Is_image] => 1

[Image_width] => 800

[Image_height] => 600

[Image_type] => JPEG

[Image_size_str] => width= "A" height= "200"

)

Explain

Here is an explanation of the array entries above.

Item Description

file_name uploaded file name (including extension)

MIME type of File_type file

File_path file absolute path without including file name

Full_path file absolute path including file name

Raw_name filename part without extension

The original file name of the file uploaded by Orig_name. This is only valid if you are setting up the upload file rename (encrypt_name).

Client_name uploaded files on the client file name.

File_ext file name extension (including '. ')

File_size image size, in KB

Whether the is_image is an image. 1 = is an image. 0 = Not an image.

Image_width image width.

Image_height Image Height

Image_type file type, that is, the file name extension (excluding '. ')

Image_size_str A string that contains the width and height. Used in an IMG tag.

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.