CodeIgniter’s File Uploading Class permits files to be uploaded. You can set various preferences, restricting the type and size of the files. Show The Process¶Uploading a file involves the following general process:
To demonstrate this process here is brief tutorial. Afterward you’ll find reference information. Creating the Upload Form¶Using a text editor, create a form called upload_form.php. In it, place this code and save it to your application/views/ directory: <html> <head> <title>Upload Form</title> </head> <body> <?php echo $error;?> <?php echo form_open_multipart('upload/do_upload');?> <input type="file" name="userfile" size="20" /> <br /><br /> <input type="submit" value="upload" /> </form> </body> </html> You’ll notice we are using a form helper to create the opening form tag. File uploads require a multipart form, so the helper creates the proper syntax for you. You’ll also notice we have an $error variable. This is so we can show error messages in the event the user does something wrong. The Success Page¶Using a text editor, create a form called upload_success.php. In it, place this code and save it to your application/views/ directory: <html> <head> <title>Upload Form</title> </head> <body> <h3>Your file was successfully uploaded!</h3> <ul> <?php foreach ($upload_data as $item => $value):?> <li><?php echo $item;?>: <?php echo $value;?></li> <?php endforeach; ?> </ul> <p><?php echo anchor('upload', 'Upload Another File!'); ?></p> </body> </html> The Controller¶Using a text editor, create a controller called Upload.php. In it, place this code and save it to your application/controllers/ directory: <?php class Upload extends CI_Controller { public function __construct() { parent::__construct(); $this->load->helper(array('form', 'url')); } public function index() { $this->load->view('upload_form', array('error' => ' ' )); } public 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('userfile')) { $error = array('error' => $this->upload->display_errors()); $this->load->view('upload_form', $error); } else { $data = array('upload_data' => $this->upload->data()); $this->load->view('upload_success', $data); } } } ?> The Upload Directory¶You’ll need a destination directory for your uploaded images. Create a directory at the root of your CodeIgniter installation called uploads and set its file permissions to 777. Try it!¶To try your form, visit your site using a URL similar to this one: example.com/index.php/upload/ You should see an upload form. Try uploading an image file (either a jpg, gif, or png). If the path in your controller is correct it should work. Reference Guide¶Initializing the Upload Class¶Like most other classes in CodeIgniter, the Upload class is initialized in your controller using the $this->load->library('upload'); Once the Upload class is loaded, the object will be available using: $this->upload Setting Preferences¶Similar to other libraries, you’ll control what is allowed to be upload based on your preferences. In the controller you built above you set 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 you can set preferences by calling the ``initialize()`` method. Useful if you auto-load the class: $this->upload->initialize($config); The above preferences should be fairly self-explanatory. Below is a table describing all available preferences. Preferences¶The following preferences are available. The default value indicates what will be used if you do not specify that preference.
Setting preferences in a config file¶If you prefer not to set preferences using the above method, you can instead put them into a
config file. Simply create a new file called the upload.php, add the $config array in that file. Then save the file in: config/upload.php and it will be used automatically. You will NOT need to use the Class Reference¶classCI_Upload ¶ initialize ([array $config = array()[, $reset =
TRUE]])¶
do_upload ([$field = 'userfile'])¶
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 $field_name = "some_field_name"; $this->upload->do_upload($field_name); display_errors ([$open = '<p>'[,
$close = '</p>']])¶
Retrieves any error messages if the Formatting Errors
data ([$index =
NULL])¶
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:
|