codeigniter上传文件未上传

时间:2016-10-05 11:50:20

标签: php codeigniter

$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');

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

4 个答案:

答案 0 :(得分:1)

            $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 =&#39; multipart / form-data&#39;

3: - 检查文件名并使用用户文件 - &gt;可选

4: - 在发布数据之前打印$ _FILES [&#39; userfile&#39;]以检查您的数据是否在上传中丢失

5: - 同时检查正在加载的自动加载文件。或手动加载

答案 1 :(得分:0)

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

$config['upload_path'] = './assets/images/gambar_paket/';

因此,重新路径将被 FCPATH

替换

以下是一些要使用的代码。

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

因此,你必须在你的up-loader代码中替换下面的两行:

<强>替换

$config['upload_path'] = './assets/images/gambar_paket/';
$this->upload->do_upload();

。通过

$config['upload_path'] = FCPATH ."assets/fileupload/";
$this->upload->do_upload('userimage'); // Where userimage is the name of the file uplaoder input type name

HTML 将如下所示:

<input type="file" name="userimage"/>

整个上传功能如下所示。

$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语法上传图像所需的示例表单:

<?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/addemployee Employee Controller并搜索名为addemployee的函数,并且您可以使用该代码上传图像,然后使用模型保存。

我希望这样的解释能够清楚地理解您所获得的错误,并在您进行的其他项目中纠正错误。

快乐编码:)

答案 2 :(得分:0)

示例模型

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

答案 3 :(得分:0)

defineProperty

Semoga bisa membantu。 Jangan lupa dibuat dahulu文件夹$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; $this->load->library('upload', $config); $this->upload->do_upload(); $data = array('nama_paket' => $nama, 'deskripsi' => $deskripsi, 'harga' => $harga, 'jenis' => $jenis, 'gambar' => $config['upload_path'] . $this->upload->data('file_name') ); $this->mod_main->createData($data,'paket'); redirect('con_main/packet','refresh');