PHP codeigniter File Upload Class code example _php tutorial

Source: Internet
Author: User
Tags 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 to upload a file of a certain type and a specified size.

Processing process

Common process for 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.

At the same time, the file will be verified to meet the requirements you set.

Once the file is uploaded successfully, return a confirmation window with the successful upload.

Here's a short tutorial to show the process. You will then 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 is used to create the start tag of the form, and the file upload requires a multipart form, because the form helper function creates a suitable statement for you. You'll also see that we've used a $error variable that displays a related 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 in 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 destination folder to store the uploaded images. Create a file named uploads on the root directory and set the property of the file to 777. (Can read and write)

Submit Form

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

example.com/index.php/upload/

You'll see an upload form, with any one (jpg, GIF, or PNG) image to submit. If the path you set in the controller is correct, it will begin to work.

Initialize file Upload class

Similar to some other classes of CodeIgniter, file upload classes are 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

Similar to other libraries, you will control the files to be uploaded according to your preferences, and in the controller, you have 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 config. autoload.php file, or if it is loaded inside the constructor, you can call the initialization function initialize to load the settings. ———— This parenthesis is translated by the IT Tumbler, adding its own understanding "

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

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

Preferences setting parameters

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

Preferences Default value option description

Upload_path none File upload path. The path must be writable, and both the relative and absolute paths can be.

Allowed_types None None allows you to upload the MIME type of the file, usually the file extension can be MIME type. Allow multiple types with a vertical bar ' | ' Separate

file_name None The file name you want to use

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 the allowed file type.

Overwrite FALSE True/false (Boolean) is overridden. When this parameter is true, the original file will be overwritten if the file is uploaded with the same name, and if the parameter is False, the CI will append a number to the file names of the new file.

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

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

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

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

Encrypt_name FALSE True/false (Boolean) whether to rename 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 it impossible for a file uploader to differentiate the file names of the files that they upload. This option only works when overwrite is FALSE.

When the Remove_spaces true True/false (Boolean) parameter is true, the space in the file name is replaced with an underscore. Recommended use.

Setting preferences parameters in the configuration file

If you do not want to apply the preferences as above, 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 loaded automatically. When you save the configuration parameters to the file, you do not need to use the $this->upload->initialize function to manually load.

Functions applied to the

The following functions are used

$this->upload->do_upload ()

Perform the operation according to your preference configuration parameters. Note: Files uploaded by default come from a 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 in the following ways:

$field _name = "Some_field_name";

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

$this->upload->display_errors ()

If Do_upload () returns failure, an error message is displayed. This function does not automatically output, but returns data, so you can arrange it as you want.

Formatting errors

The above function is used by default

Flags the error message. You can set your own delimiter 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 uploaded.

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= "200" height= "

)

Explain

Here is an explanation of the above array items.

Item Description

file_name uploaded file name (including extension)

MIME type of File_type file

File_path file absolute path that does not include a file name

Full_path file absolute path, including file name

Raw_name file name parts that do not include extensions

Orig_name the original file name of the uploaded file. This only works if you set the upload file rename (encrypt_name).

Client_name the file name of the uploaded file on the client.

File_ext file extension (including '. ')

File_size image size, in kilobytes

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, which is file extension (not included '. ')

Image_size_str A string that contains width and height. To be placed in an IMG tag.

http://www.bkjia.com/PHPjc/769527.html www.bkjia.com true http://www.bkjia.com/PHPjc/769527.html techarticle 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 to upload a file of a certain type and a specified size. ...

  • 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.