I have some code that i am using to upload multiple images i a form. I am uploading and renaming the images like this
//Obtain Picture
$config['upload_path'] = './uploads/';
$config['allowed_types'] = 'gif|jpg|png|pdf';
$config['max_size'] = 5000;
$config['max_width'] = 2000;
$config['max_height'] = 2000;
$rand_name = 'vehicles'.rand(321,8999).'_'.time();
$config['file_name'] = $rand_name;
$this->load->library('upload', $config);
$this->upload->do_upload('logbook');
$this->upload->do_upload('inrep');
$this->upload->do_upload('insurance');
$this->upload->do_upload('vp');
$data = array('upload_data' => $this->upload->data());
the form names for the multiple uploads which are unrelated are as follows logbook,inrep,insurance and vp
The code above uploads the files and renames them but i would like to obtain the new names of the files given the form name of the fields.
Right now, the files are uploaded and renamed but i cant they are from which field name.
You can get new file name using
$this->upload->data();
This will return details of uploaded files.
So you can do it like
$config['upload_path'] = './uploads/';
$config['allowed_types'] = 'gif|jpg|png|pdf';
$config['max_size'] = 5000;
$config['max_width'] = 2000;
$config['max_height'] = 2000;
$rand_name = 'vehicles'.rand(321,8999).'_'.time();
$config['file_name'] = $rand_name;
$this->load->library('upload', $config);
$this->upload->do_upload('logbook');
$logbook = $this->upload->data();
$logbook_new_filename = $logbook['file_name'];
$this->upload->do_upload('inrep');
$inrep = $this->upload->data();
$inrep_new_filename = $inrep['file_name'];
$this->upload->do_upload('insurance');
$insurance= $this->upload->data();
$insurance_new_filename = $insurance['file_name'];
$this->upload->do_upload('vp');
$vp= $this->upload->data();
$vp_new_filename = $vp['file_name'];
I haven't tested it. But I use like this to get uploaded filename.Hope it will help.
I want to upload large images on my website. I want to reduce the size of those using codeigniter. So I am doing this code
function upload_image($data) {
$config['upload_path'] = './temp/';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = 10000;
$this->load->library('upload', $config);
if (!$this->upload->do_upload('image')) {
$error = array('error' => $this->upload->display_errors());
pre($error);
} else {
$config = array();
$data = array('upload_data' => $this->upload->data());
$config['image_library'] = 'gd';
$config['source_image'] = './temp/' . $data['upload_data']['file_name'];
$config['create_thumb'] = FALSE;
$config['maintain_ratio'] = TRUE;
$config['quality'] = 50;
$config['new_image'] = './temp/' . $data['upload_data']['file_name'];
$this->load->library('image_lib', $config);
$this->image_lib->resize();
pre($data);
}
}
But images are not being compressed. Original and uploaded size are the same. Where am I wrong?
Maybe exists an error. Check for errors:
if (!$this->image_lib->resize()){
echo $this->image_lib->display_errors();
}
Note: Use gd2 as library (default value is gd2):
$config['image_library'] = 'gd2';
$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
I have a problem making thumbs after an image upload. I can upload images perfectly but it doesn't make thumbs for me. I have a map uploads where the original images are stored and a map thumbs in uploads. It's probably a path issue i guess. Thanks
This is my code in my controller.
$config['upload_path'] = './uploads/';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = '6000';
$config['max_width'] = '3024';
$config['max_height'] = '3768';
$this->load->library('upload', $config);
if ( ! $this->upload->do_upload())
{
$error = array('error' => $this->upload->display_errors());
}
$datap = array('upload_data' => $this->upload->data());
$gallery_path = realpath(APPPATH . '../uploads');
$upload_data = $this->upload->data();
$config['image_library'] = 'gd2';
$config['source_image'] = $upload_data['full_path'];
$config['new_image'] = $gallery_path . '/thumbs';
$config['create_thumb'] = TRUE;
$config['maintain_ratio'] = TRUE;
$config['width'] = 75;
$config['height'] = 50;
$this->load->library('image_lib', $config);
$this->image_lib->resize();
I am trying to create an upload folder for each user on my site, and display their photos in a specific area. I have been able to get the codeigniter upload helper to work and can upload files to a folder. Now I want to be able to:
have the photos upload by a specific user be placed in a folder for that user created on the fly specific to their id
auto size the photo for two sizes. A thumbnail and a larger size
I think I have it set up from research on how to do those things I just don't know how to plug in their id's syntactically as well as creating a folder and uploading it.
the last step would be displaying these photos in the locations of my choice tied to their ids.
Here is my controller
public function upload()
{
mkdir('./upload/' . $this->session->userdata('id'), 0777);
$config['image_library'] = 'gd2';
$config['source_image'] = 'PATH TO FOLDER??';
$config['create_thumb'] = TRUE;
$config['maintain_ratio'] = TRUE;
$config['width'] = 75;
$config['height'] = 50;
$this->load->library('image_lib', $config);
$this->image_lib->resize();
$config['upload_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());
$data['main_content'] = 'account/upload';
$this->load->view('includes/templates/main_page_template', $data);
}
else
{
$data = array('upload_data' => $this->upload->data());
$this->load->view('upload_success', $data);
}
}
You're creating the users directory but not setting it as the upload path...
$user_folder = './upload/' . $this->session->userdata('id');
if(!is_dir($user_folder)){
mkdir($user_folder, 0777);
}
...
$config['upload_path'] = $user_folder;
....