CI_Upload

class CI_Upload

initialize([array $config = array()[, $reset = TRUE]])
Parameters:
  • $config (array) – Preferences
  • $reset (bool) – Whether to reset preferences (that are not provided in $config) to their defaults
Returns:

CI_Upload instance (method chaining)

Return type:

CI_Upload

do_upload([$field = 'userfile'])
Parameters:
  • $field (string) – Name of the form field
Returns:

TRUE on success, FALSE on failure

Return type:

bool

Performs the upload based on the preferences you’ve set.

Note

By default the upload routine expects the file to come from a form field called userfile, and the form must be of type “multipart”.

<form method="post" action="some_action" enctype="multipart/form-data" />

If you would like to set your own field name simply pass its value to the do_upload() method:

$field_name = "some_field_name";
$this->upload->do_upload($field_name);
display_errors([$open = '

'[, $close = '

']])

Parameters:
  • $open (string) – Opening markup
  • $close (string) – Closing markup
Returns:

Formatted error message(s)

Return type:

string

Retrieves any error messages if the do_upload() method returned false. The method does not echo automatically, it returns the data so you can assign it however you need.

Formatting Errors

By default the above method wraps any errors within <p> tags. You can set your own delimiters like this:

$this->upload->display_errors('<p>', '</p>');
data([$index = NULL])
Parameters:
  • $data (string) – Element to return instead of the full array
Returns:

Information about the uploaded file

Return type:

mixed

This is a helper method that returns an array containing all of the data related to the file you uploaded. Here is the array prototype:

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

To return one element from the array:

$this->upload->data('file_name');       // Returns: mypic.jpg

Here’s a table explaining the above-displayed array items:

Item Description
file_name Name of the file that was uploaded, including the filename extension
file_type File MIME type identifier
file_path Absolute server path to the file
full_path Absolute server path, including the file name
raw_name File name, without the extension
orig_name Original file name. This is only useful if you use the encrypted name option.
client_name File name as supplied by the client user agent, prior to any file name preparation or incrementing
file_ext Filename extension, period included
file_size File size in kilobytes
is_image Whether the file is an image or not. 1 = image. 0 = not.
image_width Image width
image_height Image height
image_type Image type (usually the file name extension without the period)
image_size_str A string containing the width and height (useful to put into an image tag)
doc_CodeIgniter
2016-10-15 16:32:01
Comments
Leave a Comment

Please login to continue.