codeigniter upload file not uploaded - php

$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

$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

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

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

$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

Related

Codeigniter - Need help uploading picture to both folder and database

I need help uploading the picture to the folder and database. It seems I can only send it to the database, but not upload it to the folder. Is there anything wrong with the code?
admin.php (controller)
public function input_siswa(){
$config['upload_path'] = './gambarfolder/';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = '100';
$config['max_width'] = '1024';
$config['max_height'] = '768';
$this->load->library('upload');
$this->upload->initialize($config);
$location=base_url().'/gambarfolder/';
$pict=$location.$data_imge;
$nama_siswa = $this->input->post('nama_siswa');
$nis = $this->input->post('nis');
$id_jurusan = $this->input->post('id_jurusan');
$data = array(
'nama_siswa'=>$nama_siswa,
'nis'=>$nis,
'id_jurusan'=>$id_jurusan,
'gambar'=>$pict
);
$this->m_model->create('siswa',$data);
redirect(base_url('index.php/admin/tampil_siswa'));
}
Upload image in codeigniter using upload library.
public function input_siswa(){
$config['upload_path'] = './gambarfolder/';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = '100';
$config['max_width'] = '1024';
$config['max_height'] = '768';
$location=base_url().'/gambarfolder/';
$pict=$location.$data_imge;
$this->load->library('upload', $config);
if ($this->upload->do_upload('userfile'))
{
$nama_siswa = $this->input->post('nama_siswa');
$nis = $this->input->post('nis');
$id_jurusan = $this->input->post('id_jurusan');
$data = array(
'nama_siswa'=>$nama_siswa,
'nis'=>$nis,
'id_jurusan'=>$id_jurusan,
'gambar'=>$pict
);
$this->m_model->create('siswa',$data);
redirect(base_url('index.php/admin/tampil_siswa'));
// redirect succsess page when image upload succsessfully.
}else{
$error = array('error' => $this->upload->display_errors());
$this->load->view('upload', $error);
// return image upload error.
}
}
first check your folder gambarfolder is in the root of your codigniter application
second check in your form tag you add enctype="multipart/form-data"
and add that lines for upload image to gambarfolder folder
$this->load->library('upload', $config);
if($this->upload->do_upload('userfile'))
{
}
where userfile is the name of file tag like
<input type="file" name="userfile" size="20" />
and $config is your config array
and also check https://www.codeigniter.com/userguide3/libraries/file_uploading.html
You need to add below line after initialise config
$this->upload->do_upload('userfile')

when is using uploader system in codeigniter the system say Call to a member function site_url() on array

This is a very simple file uploading with CI but the system says functions site_url() or base_url() can't be used in original file Helper Codeigniter
$this->config = array('upload_path' => dirname($_SERVER["SCRIPT_FILENAME"])."/images/",
'upload_url' => base_url()."images/",
'allowed_types' => "png|jpg",
'overwrite' => TRUE,
'max_size' => "10000KB",
'max_height' => "7680",
'max_width' => "10240",
'file_name' => "about"
);
$this->load->library('upload', $this->config);
if($this->upload->do_upload('logo'))
{
$this->session->set_flashdata('success_upload', 'You logo has been uploaded' );
}
else
{
$this->session->set_flashdata('error_upload', 'Can\'t Upload this file, Please try agine.' );
}
Use this code it will helps you.
$obj = &get_instance();
$obj->load->library('upload');
$file_name = "about." . pathinfo($_FILES['logo']['name'], PATHINFO_EXTENSION); //userrandom file name instead of about
$full_path = FCPATH . 'images/';
$config['upload_path'] = $full_path;
$config['allowed_types'] = 'gif|jpg|png|jpeg|GIF|JPG|PNG|JPEG';
$config['max_size'] = 2048;
$config['max_width'] = 2048;
$config['max_height'] = 2048;
$config['file_name'] = $file_name;
$obj->upload->initialize($config);
if (!$obj->upload->do_upload('logo')) {
$this->session->set_flashdata('error_upload', $obj->upload->display_errors());
return $obj->upload->display_errors();
} else {
$this->session->set_flashdata('success_upload', 'You logo has been uploaded');
return $obj->upload->data();
}
don't use dirname() for file path .
use FCPATH it will gile you absolute path from root like "/var/www/html/project_name"
upload_url is not a key in codeiginter upload library
use random file name because if you upload another image it will override or create file new file like file_name(1).extension
As per your this comment I think you have to load the URL helper first.
$this->load->helper('url');
Or you can load it in autoload.php
$autoload['helper'] = array('url');
To Upload the image try this code:
$config['upload_path'] = './images/';
$config['allowed_types'] = 'png|jpg';
$config['max_size'] = 10000;
$config['max_width'] = 10240;
$config['max_height'] = 7680;
$this->load->library('upload', $config);
if ( ! $this->upload->do_upload('userfile'))
{
$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);
}

I can't upload a markdown file by the CodeIgniter

I don't know how to set the allowed_types, and the markdown file suffix is .md. It always says:
The filetype you are attempting to upload is not allowed.
English is not my native language, so I hope this makes sense. My code:
public function do_upload()
{
$config['upload_path'] = './blog/';
$config['allowed_types'] = ''; //file's tye
$config['max_size'] = 50;
$config['max_width'] = 1024;
$config['max_height'] = 768;
$this->load->library('upload', $config);
if ( ! $this->upload->do_upload('userfile'))
{
$error = array('error' => $this->upload->display_errors());
$this->load->view('houtai_view', $error);
}
else
{
$data = array('upload_data' => $this->upload->data());
//$this->load->view('upload_success', $data);
}
}
You just need to allowed_types in your $config array
$config['allowed_types'] = 'md';
If you want to allow all kinds of files than you can use * like:
$config['allowed_types'] = '*';
File Uploading Class
**Customize File type allow **
$config['allowed_types'] = 'gif|jpg|png';
You can edit APPATH.'config/mimes.php' file and add 'md' => 'text/plain', to array.

Codeigniter Upload multiple files different path to database

I have a problem with my multiple upload with codeigniter,
using image
another using pdf
When I uploaded the file uploaded twice and how to call different path to uploaded to database. this my code
Controller
public function upload(){
$catalog='catalog';
$userfile='userfile';
//for cover
$config['upload_path'] = './file/book/'; //Use relative or absolute path
$config['allowed_types'] = 'gif|jpg|png|';
$config['max_size'] = '100000';
$config['max_width'] = '1024';
$config['max_height'] = '768';
$this->load->library('upload', $config);
$this->upload->initialize($config);
//$c=$this->upload->do_upload($userfile);
//for catalog
$config['upload_path'] = './file/book/pdf/'; //Use relative or absolute path
$config['allowed_types'] = 'pdf';
$config['max_size'] = '1000000';
$this->load->library('upload', $config);
$this->upload->initialize($config);
//$cat=$this->upload->do_upload($catalog);
if(!$this->upload->do_upload($catalog) && $this->upload->do_upload($userfile)){
$error = array('error' => $this->upload->display_errors());
$this->load->view('uploadds', $error);
}else{
$this->load->model("book_model");
$this->book_model->addnewbook();
redirect('book/book');
}
}
This model
function addnewbook(){
$fcat=array('upload_data' => $this->upload->data($userfile));
$fcatalog=array('upload_dataa' => $this->upload->data($catalog));
}
You need to handle multiple uploads independently. For this, you have to create separate custom objects for both uploads while loading the upload library. (See the code comments)
public function upload() {
// Cover upload
$config = array();
$config['upload_path'] = './file/book/';
$config['allowed_types'] = 'gif|jpg|png|';
$config['max_size'] = '100000';
$config['max_width'] = '1024';
$config['max_height'] = '768';
$this->load->library('upload', $config, 'coverupload'); // Create custom object for cover upload
$this->coverupload->initialize($config);
$upload_cover = $this->coverupload->do_upload('cover');
// Catalog upload
$config = array();
$config['upload_path'] = './file/book/pdf/';
$config['allowed_types'] = 'pdf';
$config['max_size'] = '1000000';
$this->load->library('upload', $config, 'catalogupload'); // Create custom object for catalog upload
$this->catalogupload->initialize($config);
$upload_catalog = $this->catalogupload->do_upload('catalog');
// Check uploads success
if ($upload_cover && $upload_catalog) {
// Both Upload Success
// Data of your cover file
$cover_data = $this->coverupload->data();
print_r($cover_data);
// Data of your catalog file
$catlog_data = $this->catalogupload->data();
print_r($catlog_data);
} else {
// Error Occured in one of the uploads
echo 'Cover upload Error : ' . $this->coverupload->display_errors() . '<br/>';
echo 'Catlog upload Error : ' . $this->catalogupload->display_errors() . '<br/>';
}
}
Use the data on $cover_data['full_path'] and $catlog_data['full_path'] to update your database
$config['upload_path'] = 'frontend_assets/images/hospital';
$config['allowed_types'] = 'gif|jpg|png|jpeg|JPEG||JPG|PNG';
$this->load->library('upload', $config);
if($_FILES['logo']['name']){
$config['file_name'] = time() . $_FILES["logo"]['name'];
if (!$this->upload->do_upload('logo')) {
$this->upload->display_errors();
} else {
$upload = $this->upload->data();
$insert_data['logo'] = $config['upload_path'] . '/' . $upload['file_name'];
}
}
if($_FILES['bulding_photo']['name']){
$config['file_name'] = time() . $_FILES["bulding_photo"]['name'];
if (!$this->upload->do_upload('bulding_photo')) {
$this->upload->display_errors());
} else {
$upload = $this->upload->data();
$insert_data['bulding_photo'] = $config['upload_path'] . '/' . $upload['file_name'];
$this->image_size_fix($insert_data['bulding_photo'], $width = 200, $height = 200);
}
}

image upload in codeigniter

I want to upload an image in three foler. My folder structure is like:
upload/large,upload/original,upload/thumb.
How to store the image after resizing into these folders using codeigniters 'upload' library.
I agree with Natrium, you need to accept more answers.
However, for this question:
It looks to me like you're only actually uploading to the original folder and then using the image library on this copy, so, try the following:
$config['upload_path'] = './upload/original';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = '100';
$config['max_width'] = '500';
$config['max_height'] = '300';
$this->load->library('upload', $config);
$this->upload->do_upload()
$upload_data = $this->upload->data();
$copies = array(
array('dir' => 'upload/large', 'x' => 1000, 'y' => 600), //note: x&y could be replaced with a percentage or ratio etc.
array('dir' => 'upload/thumbnail', 'x' => 100, 'y' => 60)
);
foreach($copies as $copy)
{
$config['image_library'] = 'ImageMagick';
$config['library_path'] = '/usr/bin/';
$config['source_image'] = $upload_data['full_path'];
$config['new_image'] = $copy['dir'] . $upload_data['file_name'];
$config['maintain_ratio'] = TRUE;
$config['width'] = $copy['x'];
$config['height'] = $copy['y'];
$config['master_dim'] = 'width';
$this->image_lib->initialize($config);
$this->image_lib->resize();
}
Hope this helps!
You should just change the "upload_path" value in your configuration Array.
Here is some kind of code you could use :
$config['upload_path'] = './uploads/';
$this->load->library('upload', $config);
Referring to the User Guide :
http://codeigniter.com/user_guide/libraries/file_uploading.html

Categories