batas

Kamis, 02 Agustus 2012

Upload Form (Code Igniter)

Nah, meskipun masih belum ketemu kenapa kemaren harus pake "index.php" dulu di linknya, tapi kali ini udah pengen bahas materi selanjutnya.. Upload Form di Code Igniter.

Upload Form di code igniter ini kayaknya jauh lebih simple dari pelajaran sebelumnya. Kita cuma butuh 3 File Baru dan satu Folder baru.

Kalian bisa ikutin tata caranya di User Guide CodeIgniter kok :)

Langkah pertama adalah:
1. Buatlah file upload_form.php di bawah folder views sebagai form upload yang pertama kita akses.

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

2. Buatlah sebuah file yang fungsinya sebagai file yang akan diakses jika proses upload berhasil

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


3. Terakhir Buat file yang di taruh di bawah folder controller dengan nama file upload.php
    Berikut sintag sederhananya

<?php
class Upload extends CI_Controller {
    function __construct()
    {
        parent::__construct();
        //$this->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']    = '300';
        $config['max_width']  = '2000';
        $config['max_height']  = '2000';

        $this->load->library('upload', $config);
        $this->upload->initialize($config);

       if ( ! $this->upload->do_upload())
        {
            $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);
        }
    }
} ?> 




4. Langkah paling terakhir adalah buat sebuah folder penampung untuk file-file yang anda upload yang berada di bawah folder CodeIgniter Anda. Beri nama "uploads" pada folder tersebut



 

Hasil Konfigurasi:



 

Tidak ada komentar:

Posting Komentar