Blog Details

File Uploading Class


                                
                                    <h1></h1>
<h2>* <span style="text-decoration: underline;">File Uploading Class :-</span></h2>
<ul>
	<li><span style="font-size: 2em;"> </span><span style="font-size: 13px;">CodeIgniter's <strong>File Uploading</strong> Class permits <strong>files to be uploaded</strong>. You can set various preferences, restricting the <strong>type and size</strong> of the files.</span></li>
</ul>
&nbsp;
<h2> <strong>* <span style="text-decoration: underline;">Uploading a file involves the following general process :-</span></strong></h2>
<ul>
	<li>An <strong>upload form</strong> is displayed, allowing a <strong>user to select a file</strong> and<strong> upload it</strong>.</li>
	<li>When the f<strong>orm is submitted</strong>, the <strong>file is uploaded</strong> to the <strong>destination</strong> you specify.</li>
	<li>Along the way, the file is<strong> validated</strong> to make <strong>sure</strong> it is allowed to be <strong>uploaded based</strong> on the preferences you set.</li>
	<li>Once <strong>uploaded</strong>, the user will be <strong>shown a success message</strong>.</li>
</ul>
&nbsp;
<h2><strong>Example :</strong></h2>
&nbsp;

<strong>Step 1 :</strong>  First, we make controller, create a file name "<strong>upload.php</strong>"   Create a file    at <strong><dfn>application/controllers/</dfn></strong> with the following code.

&lt;?php

class Upload extends CI_Controller {

<strong>function __construct()</strong>
{
parent::__construct();
$this-&gt;load-&gt;helper(array('form', 'url'));
}

<strong>function index()</strong>
{
$this-&gt;load-&gt;view('upload_form', array('error' =&gt; ' ' ));
}

<strong>function do_upload()</strong>
{
$config['upload_path'] = './uploads/';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = '100';
$config['max_width'] = '1024';
$config['max_height'] = '768';

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

if ( ! $this-&gt;upload-&gt;do_upload())
{
$error = array('error' =&gt; $this-&gt;upload-&gt;display_errors());

$this-&gt;load-&gt;view('upload_form', $error);
}
else
{
$data = array('upload_data' =&gt; $this-&gt;upload-&gt;data());

$this-&gt;load-&gt;view('upload_success', $data);
}
}
}
?&gt;

&nbsp;

<strong>Step 2:  </strong>Create the form at <strong><dfn>application/views/upload_form.php</dfn></strong> and add the following code.

&lt;html&gt;
&lt;head&gt;
&lt;title&gt;Upload Form&lt;/title&gt;
&lt;/head&gt;
&lt;body&gt;

&lt;?php echo $error;?&gt;

<strong>&lt;?php echo form_open_multipart('upload/do_upload');?&gt;</strong>

<strong>&lt;input type="file" name="userfile" size="20" /&gt;</strong>

&lt;br /&gt;&lt;br /&gt;

<strong>&lt;input type="submit" value="upload" /&gt;</strong>

&lt;/form&gt;

&lt;/body&gt;
&lt;/html&gt;

&nbsp;

<strong>Step 3:  </strong>Create the form at <strong><dfn>application/views/<dfn>upload_success.php </dfn></dfn></strong> and add the following code.

&lt;html&gt;
&lt;head&gt;
&lt;title&gt;Upload Form&lt;/title&gt;
&lt;/head&gt;
&lt;body&gt;

&lt;h3&gt;Your file was successfully uploaded!&lt;/h3&gt;

&lt;ul&gt;
&lt;?php foreach ($upload_data as $item =&gt; $value):?&gt;
&lt;li&gt;&lt;?php echo $item;?&gt;: &lt;?php echo $value;?&gt;&lt;/li&gt;
&lt;?php endforeach; ?&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;?php echo anchor('upload', 'Upload Another File!'); ?&gt;&lt;/p&gt;

&lt;/body&gt;
&lt;/html&gt;
<h2></h2>
                                    


Categories

CodeIgnitor

Latest posts

13.05.14
Implementing CRUD Operations ¶

<ul> <li>Now comes the fun part. We would like t

13.05.14
View

<ol> <li><a href="http://www.yiiframework.com/do

13.05.14
Creating Model

<ol> <li><a href="http://www.yiiframework.com/do

Get Free Quote