0

好的,所以我搜索并尝试了谷歌的所有内容,但我被卡住了。我正在尝试在我现有的 CodeIgniter 项目上使用上传功能。我想上传它们,然后以 64x64 像素大小在我的视图上显示它们,而实际大小为 320x320 像素。

我加入了两个表以从另一个表中获取“名称”字段,因此当我需要添加新数据时,我可以在我的添加数据视图中显示它们,并且我设法做到了,但我对如何放置上传代码/函数因为我有这个“add_data”函数,我用它来保存我在“tambah_mahasiswa”视图中发布的内容到数据库。我试图将 'do_upload' 函数与 'add_data' 函数结合起来,但它仍然无法正常工作。我该如何解决这个问题?

这是我在 tb_mahasiswa 表上的照片字段:

Field   Type        Attributes
photo   blob        BINARY

这是我的控制器代码 = 'controlpanel' :

class Controlpanel extends CI_Controller
{
    private $path_uploads;
    private $path_uploads_thumb;
    function __construct()
    {
        parent::__construct();
                $this->cek_login();
        $this->load->model('modelku');

        $this->path_uploads = realpath( APPPATH . '../uploads/');
        $this->path_uploads_thumb = APPPATH . '../uploads/thumbnail';
    }

function add_data()
    {
        $this->load->library('form_validation');
        $this->load->helper('url');

        $data['prodi']=$this->modelku->get_prodi_all();

        $this->form_validation->set_rules('nim','Nomor Induk Mahasiswa','required|exact_length[7]');
        $this->form_validation->set_rules('nim','Nomor Induk Mahasiswa','required|integer');
        $this->form_validation->set_rules('nama','Nama Lengkap Mahasiswa','required');
        $this->form_validation->set_rules('prodi','Prodi','required');
        $this->form_validation->set_rules('tanggal_lahir','Tanggal lahir','required');
        $this->form_validation->set_rules('email','Email','required|valid_email');
        $this->form_validation->set_rules('jenis_kelamin','Jenis Kelamin','required');
        $this->form_validation->set_rules('alamat','Alamat','required');
        $this->form_validation->set_rules('telepon','Telepon','required');

        if($this->form_validation->run())
        {
                        $nim = $this->input->post('nim');
                        $nama = $this->input->post('nama');
                        $prodi = $this->input->post('prodi');
                        $tanggal_lahir = date('Y-m-d',strtotime($this->input->post('tanggal_lahir')));
                        $email = $this->input->post('email');
                        $jenis_kelamin = $this->input->post('jenis_kelamin');
                        $alamat = $this->input->post('alamat');
                        $telepon = $this->input->post('telepon');
                        $photo = $this->input->post('photo');

                        $data_mahasiswa = array('nim'=>$nim,'nama'=>$nama,'prodi'=>$prodi, 'tanggal_lahir'=>$tanggal_lahir,
                                                'email'=>$email, 'jenis_kelamin'=>$jenis_kelamin,'alamat'=>$alamat,
                                                'telepon'=>$telepon,'photo'=>$photo);
                        ('controlpanel/index');
                } else {
            $this->load->view("tambah_mahasiswa",$data);
            }
    }

function do_upload()
    {
        $config['upload_path'] =  $this->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())
        {
            $error = array('error' => $this->upload->display_errors());

            $this->load->view('tambah_mahasiswa', $error);
        }
        else
        {
            $upload_data = $this->upload->data();
            $this->load->view('controlpanel_view', $data);
            $file_name = $upload_data['file_name'];
            $config['image_library']='gd2';
            $config['source_image']=$this->path_uploads . '\\' . $file_name;
            $config['new_image']=$this->path_uploads_thumb . $file_name;
            $config['create_thumb']=TRUE;
            $config['maintain_ratio']=TRUE;
            $config['width']=320;
            $config['height']=320;

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

            if(!$this->image_lib->resize())
            {
                echo $this->image_lib->display_errors();
            } else {
                echo "Berhasil di unggah!";
            }
        }
    }

这是我的观点 = 'tambah_mahasiswa' :

<!DOCTYPE html>
<html>
<head>
<title>PHP Framework</title>
    <link rel="stylesheet" href="<?php echo base_url() ?>asset/css/style.css"/>
</head>
<body>

<h3>Tambah Data Mahasiswa</h3>

    <?php echo form_open('controlpanel/add_data'); ?> 
    <?php echo form_open_multipart('controlpanel/do_upload'); ?> 

<table width="30%" border="0">
    <tr>
    <td>NIM <sup>*</sup></td>
    <td>:</td>
        <td>
            <?php echo form_input(array('name'=>'nim', 'maxlenght'=>'7', 'size'=>'10'),set_value('nim'))?> <?php echo form_error('nim','<span style="color:red;"><small>','</div></span>') ?> </br> 
        </td>
    </tr>

    <tr>
    <td>Nama <sup>*</sup></td> 
    <td>:</td>
        <td>
            <?php echo form_input(array('name'=>'nama', 'maxlenght'=>'35', 'size'=>'15'),set_value('nama'))?> <?php echo form_error('nama','<span style="color:red;"><small>','</div></span>') ?> </br>
        </td>
    </tr>


        <td>Prodi <sup>*</sup> </td> 
    <td>:</td>
    <td>
        <?php
                    foreach($prodi as $item_prodi)
                    {
                        $array_prodi[$item_prodi->kode]=$item_prodi->namaprodi;
                    }
                echo form_dropdown('prodi',$array_prodi);
                ?>
    </td>
    </tr>

    <tr>
    <td width='30%'>Tanggal Lahir <sup>*</sup></td> 
    <td>:</td>
        <td>
            <?php echo form_input(array('name'=>'tanggal_lahir', 'maxlenght'=> '10', 'placeholder'=>'yyyy-mm-dd', 'size'=>'15', 'id'=>'tanggal_lahir'),set_value('tgl_lahir'))?> <?php echo form_error('tgl_lahir','<span style="color:red;"><small>','</small></span>') ?>
        </td>
    </tr>

    <tr>
    <td>Jenis Kelamin <sup>*</sup></td> 
    <td>:</td>
        <td>
            <?php echo form_radio('jenis_kelamin', 'L', TRUE)?> Laki-laki
            <?php echo form_radio('jenis_kelamin', 'P')?> Perempuan
        </td>
    </tr>

    <tr>
    <td>Alamat <sup>*</sup></td> 
    <td>:</td>
        <td>
            <?php echo form_textarea(array('name'=>'alamat','rows'=>'10','cols'=>'30'),set_value('alamat'))?> <?php echo form_error('alamat','<span style="color:red;"><small>','</small></span>') ?> </br>
        </td>

    </tr>

    <tr>
    <td>Propinsi </td> 
    <td>:</td>
        <td>
        <?php echo form_input(array('name'=>'propinsi', 'maxlenght'=>'15', 'size'=>'17','id'=>'propinsi'))?> <?php echo form_error('propinsi','<span style="color:red;"><small>','</small></span>') ?> </br>
    </td>
    </tr>

    <tr>
    <td>No. Telp </td> 
    <td>:</td>
        <td>
        <?php echo form_input(array('name'=>'telepon', 'maxlenght'=>'15', 'size'=>'17'))?> <?php echo form_error('telp','<span style="color:red;"><small>','</small></span>') ?> </br>
    </td>
    </tr>

    <tr>
    <td>Email <sup>*</sup> </td> 
    <td>:</td>
        <td>
        <?php echo form_input(array('name'=>'email', 'maxlenght'=>'30','placeholder'=>'name@example.com', 'size'=>'17'),set_value('email'))?> <?php echo form_error('email','<span style="color:red;"><small>','</small></span>') ?> </br>
    </td>
    </tr>

    <tr>
    <td>Photo </td> 
    <td>:</td>
        <td>
        <?php echo form_input(array('name'=>'photo', 'type'=>'file', 'size'=>'20'),set_value('photo')) ?>
        </br>
    </td>
    </tr>

    <tr>
    <td>
        <?php echo form_submit(array('type'=>"submit", 'name'=>"mysubmit", 'value'=>"Save"))?>
    </td>
    </tr>

    <?php echo form_close();?>

</table>
</body>
</html>

PS:我在我的项目中为图像文件创建了文件夹:

 - myproject\uploads\thumbnail
4

1 回答 1

0

我可以从上面给出的代码中看到,在控制器类“Controlpanel”中,您已经定义了一个函数 do_upload()。

但是,在同一个控制器函数 do_upload 中,您正在调用具有不同类名“upload”的函数,这是一个库类。

$this->upload->do_upload()

您需要将控制器类名称“Controlpanel”更改为“上传”,或者您可以尝试将当前控制器类中的 do_upload 函数的名称更改为其他名称,然后您可以按照当前代码继续操作。

您还应该确保视图页面上的表单名称应该与 do_upload 函数中的表单名称相同。

于 2013-03-24T19:00:58.717 回答