首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >未上传codeigniter上传文件

未上传codeigniter上传文件
EN

Stack Overflow用户
提问于 2016-10-05 19:50:20
回答 4查看 3.6K关注 0票数 0
代码语言:javascript
复制
$config['upload_path'] = './assets/images/gambar_paket/';
            $config['allowed_types'] = 'gif|jpg|png|jpeg|bmp'; 
            $config['max_size'] = 1000;
            $config['max_width'] = 1024; 
            $config['max_height'] = 900;
            $config['file_name'] = $file;
            $this->load->library('upload', $config);
            $this->upload->do_upload();

            $data = array('nama_paket' => $nama,
                          'deskripsi' => $deskripsi,
                          'harga' => $harga,
                          'jenis' => $jenis,
                          'gambar' => $file
                         );
            $this->mod_main->createData($data,'paket');
            redirect('con_main/packet','refresh');

这是我的上传控制器,但是文件不会上传到上传路径。请任何人帮帮我

EN

回答 4

Stack Overflow用户

发布于 2016-10-05 20:02:59

代码语言:javascript
复制
            $config['upload_path'] = './assets/images/gambar_paket/';
            $config['allowed_types'] = 'gif|jpg|png|jpeg|bmp'; 
            $config['max_size'] = 1000;
            $config['max_width'] = 1024; 
            $config['max_height'] = 900;
            $config['file_name'] = $file;
            $this->load->library('upload', $config);
            $this->upload->do_upload();
            if(!$this->upload->do_upload()){
                $error = array('error' => $this->upload->display_errors());
                echo <div class="alert alert-danger">'.$error['error'].'</div>';
            }else{
               $data = array('nama_paket' => $nama,
                          'deskripsi' => $deskripsi,
                          'harga' => $harga,
                          'jenis' => $jenis,
                          'gambar' => $file
               );
            $this->mod_main->createData($data,'paket');
            redirect('con_main/packet','refresh');
}

1:-使用错误消息,它将显示错误

2:-还要检查您的表单是否具有enctype='multipart/ form -data‘

3:-检查文件名并使用用户文件->optional

4:-在发布数据之前,打印$_FILES‘’userfile‘,以便检查您的数据是否在上传时丢失

5:-还要检入手动加载loading.Or的自动加载文件

票数 1
EN

Stack Overflow用户

发布于 2016-10-05 20:04:42

您的文件未上载到提供的路径的错误是您为上载目录指定了相对路径

代码语言:javascript
复制
$config['upload_path'] = './assets/images/gambar_paket/';

因此,将使用FCPATH替换相对路径

下面是一些要用到的代码。

代码语言:javascript
复制
EXT: The PHP file extension
FCPATH: Path to the front controller (this file) (root of CI)
SELF: The name of THIS file (index.php)
BASEPATH: Path to the system folder
APPPATH: The path to the "application" folder

因此,您必须替换上载程序代码中的以下两行代码:

替换:

代码语言:javascript
复制
$config['upload_path'] = './assets/images/gambar_paket/';
$this->upload->do_upload();

使用:的

代码语言:javascript
复制
$config['upload_path'] = FCPATH ."assets/fileupload/";
$this->upload->do_upload('userimage'); // Where userimage is the name of the file uplaoder input type name

超文本标记语言将如下所示:

代码语言:javascript
复制
<input type="file" name="userimage"/>

整个上传函数将如下所示。

代码语言:javascript
复制
$config['upload_path'] = FCPATH ."assets/images/gambar_paket/";
$config['allowed_types'] = 'gif|jpg|png';
$config['allowed_types'] = 'gif|jpg|png|jpeg|bmp'; 
$config['max_size'] = 1000;
$config['max_width'] = 1024; 
$config['max_height'] = 900;
$config['file_name'] = $file;
$this->load->library('upload', $config);
$this->upload->initialize($config);     
if ( ! $this->upload->do_upload('userimage'))  {// Here you can handle the Failure Upload}
else
{ $data = $this->upload->data(// Here you can handle the operations after the image is uploaded);}

下面是从HTML上载图像所需的示例表单:

代码语言:javascript
复制
<?php
echo form_open_multipart('employee/addemployee', array('name' => 'addemployee', 'class'=>'form-horizontal'));
?>
<div class="form-group">
     <label class="control-label col-sm-4" for="pwd">Profile:</label>
     <div class="col-sm-8">
        <input type="file" class="" id="profile" name="userimage">
     </div>
</div>
<?php
echo form_close();
?>

注意:它将重定向到Employee Controlleremployee/addemployee,并搜索名为addemployee的函数,在那里您有上传图像的代码,然后使用模型保存它。

因此,我希望这个解释将清楚地理解你得到的错误,并在你所做的进一步项目中纠正它。

快乐编码:)

票数 0
EN

Stack Overflow用户

发布于 2016-10-06 03:26:53

示例模型

代码语言:javascript
复制
public function InsertBerita(){
    // Direktori File "folder-CI->berita"
    $config['upload_path'] = './berita/';
    // Format Image
    $config['allowed_types'] = 'jpg|png|jpeg';
    $config['encrypt_name'] = TRUE;
    // Load Libary Uploud
    $this->load->library('upload', $config);
    if ($this->upload->do_upload()) {
        $cekUser = $this->db->get_where('berita', array('judul_berita' => $this->input->post('judul_berita')));
        unlink("berita/".$cekUser->first_row()->cover_berita);
        $data['upload_data'] = $this->upload->data();
        $this->resize($data['upload_data']['full_path'], $data['upload_data']['file_name']);
        $file_gambar = $data['upload_data']['file_name'];
        $insert = $this->db->insert('berita', array(
            'cover_berita' => $file_gambar,
            'ringkasan_berita' => $this->input->post('ringkasan_berita'),
            'judul_berita' => $this->input->post('judul_berita'),
            'isi_berita' => $this->input->post('isi_berita'),
            'tanggal_berita' => date('Y-m-d H:i:s'),
            'id_admin' => '1',
            ));
        sleep(2);
        redirect(base_url('databerita?insertsuccess'));

    }else{
        redirect(base_url('insertberita?failed'));
    }
}

// image manipulasi merisize
public function resize($path,$file){
    $config['image_library']='GD2';
    $config['source_image'] = $path;
    $config['maintain_ratio'] = TRUE;
    $config['create_thumb'] = FALSE;
    // size image
    $config['width'] = 1158;
    $config['height'] = 550;
    // kualitas diturunkan 20%
    $config['quality'] = 20;
    $config["image_sizes"]["square"] = array(1158, 550);
    $this->load->library('image_lib', $config);
    $this->image_lib->fit();
}
enter code here

Gunakan Libary Uploud Jangan Lupa

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/39873187

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档