From b0dd10f8171945e0c1f3527dd1e9d18b043e01a7 Mon Sep 17 00:00:00 2001 From: admin Date: Fri, 25 Aug 2006 17:25:49 +0000 Subject: Initial Import --- user_guide/libraries/file_uploading.html | 429 +++++++++++++++++++++++++++++++ 1 file changed, 429 insertions(+) create mode 100644 user_guide/libraries/file_uploading.html (limited to 'user_guide/libraries/file_uploading.html') diff --git a/user_guide/libraries/file_uploading.html b/user_guide/libraries/file_uploading.html new file mode 100644 index 000000000..7fabb095d --- /dev/null +++ b/user_guide/libraries/file_uploading.html @@ -0,0 +1,429 @@ + + + + +Code Igniter User Guide + + + + + + + + + + + + + + + + + + + + + + +
+ + + + + +

Code Igniter User Guide Version 1.4.0

+
+ + + + + + + + + +
+ + +
+ + + +
+ + +

File Uploading Class

+ +

Code Igniter's File Uploading Class permits files to be uploaded. You can set various +preferences, restricting the type and size of the files.

+ + +

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 applications/views/ +folder:

+ + + + +

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 applications/views/ folder:

+ + + + +

The Controller

+ +

Using a text editor, create a controller called upload.php. In it, place this code and save it to your applications/controllers/ +folder:

+ + + + + +

The Upload Folder

+ +

You'll need a destination folder for your uploaded images. Create a folder at the root of your Code Igniter 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:

+ +www.your-site.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 Code Igniter, the Upload class is initialized in your controller using the $this->load->library function:

+ +$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->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.

+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
PreferenceDefault ValueOptionsDescription
upload_pathNoneNoneThe path to the folder where the upload should be placed. The folder must be writable and the path can be absolute or relative.
allowed_typesNoneNoneThe mime types corresponding to the types of files you allow to be uploaded. Usually the file extension can be used as the mime type. Separate multiple types with a pipe.
overwriteFALSETRUE/FALSE (boolean)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_size0NoneThe maximum size (in kilobytes) that the file can be. Set to zero for no limit. Note: Most PHP installations have their own limit, as specified in the php.ini file. Usually 2 MB (or 2048 KB) by default.
max_width0NoneThe maximum width (in pixels) that the file can be. Set to zero for no limit.
max_height0NoneThe maximum height (in pixels) that the file can be. Set to zero for no limit.
encrypt_nameFALSETRUE/FALSE (boolean)If set to TRUE the file name will be converted to a random encrypted string. This can be useful if you would like the file saved with a name that can not be discerned by the person uploading it.
remove_spacesTRUETRUE/FALSE (boolean)If set to TRUE, any spaces in the file name will be converted to underscores. This is recommended.
+ + +

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 $this->upload->initialize function if you save your preferences in a config file.

+ + +

Function Reference

+ +

The following functions are available

+ + +

$this->upload->do_upload()

+ +

Performs the upload based on the preferences you've set. Note: The upload routine expects the file to come from a form field +called userfile, and the form must be a "multipart type:

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

$this->upload->display_errors()

+ +

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

+ +

Formatting Errors

+

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

+ +$this->upload->display_errors('<p>', '</p>'); + +

$this->upload->data()

+ +

This is a helper function 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
+    [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"
+)
+ +

Explanation

+ +

Here is an explanation of the above array items.

+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
ItemDescription
file_nameThe name of the file that was uploaded including the file extension.
file_typeThe file's Mime type
file_pathThe absolute server path to the file
full_pathThe absolute server path including the file name
raw_nameThe file name without the extension
orig_nameThe original file name. This is only useful if you use the encrypted name option.
file_extThe file extension with period
file_sizeThe file size in kilobytes
is_imageWhether the file is an image or not. 1 = image. 0 = not.
image_widthImage width.
image_heigthImage height
image_typeImage type. Typically the file extension without the period.
image_size_strA string containing the width and height. Useful to put into an image tag.
+ +
+ + + + + + + \ No newline at end of file -- cgit v1.2.3-24-g4f1b