Reputation:
$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');
that's my controller for doing upload, but the file doesn't upload to the upload path. Please anyone help me
Upvotes: 0
Views: 4217
Reputation: 312
$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:-Use the error message it will show you error
2:-Also check wheather your form has enctype='multipart/form-data'
3:-check file name and use userfile ->optional
4:-before posting data print $_FILES['userfile'] so to check if your data is missing in uplaod
5:-Also check in autoload file that is loading.Or load manually
Upvotes: 1
Reputation: 105
$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');
Semoga bisa membantu. Jangan lupa dibuat dahulu folder assets/images/gambar_paket
Upvotes: 0
Reputation: 143
Example Model
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
Upvotes: 0
Reputation: 4210
The Error that your File is not uploading to the provided path is that you have given the relative path for the upload directory
$config['upload_path'] = './assets/images/gambar_paket/';
Hence the realative path is to be replaced with the FCPATH
Here are some of the codes that are to be used.
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
Hence you have to replace the two line below in your up-loader code:
Replace:
$config['upload_path'] = './assets/images/gambar_paket/';
$this->upload->do_upload();
With:
$config['upload_path'] = FCPATH ."assets/fileupload/";
$this->upload->do_upload('userimage'); // Where userimage is the name of the file uplaoder input type name
HTML will look like this:
<input type="file" name="userimage"/>
And the Entire upload function will look like as follows.
$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);}
Here is the sample form that you need to upload the image from the HTML Syntax:
<?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();
?>
Note: It will redirect to employee/addemployee
which is the Employee Controller
and search for function called addemployee
and there you have the code to upload the image and then save it using the model.
I hope so this explanation will be clear to understand the Error that you get and to rectify it in further projects that you make on.
Happy Coding:)
Upvotes: 0