get file name after $config['encrypt_name'] in codeigniter - php

You have added $ config ['encrypt_name'] = TRUE; For the upload code because some user images are called spaces and problems causing problems lifting
After uploading the file its name is changed but I try to take the file name to send it to the database
this way
$ img1 = $ _FILES ['img1'] ['name'];
But I get the original file name before changing
$config['upload_path'] = './uploads/';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = 2000;
$config['max_width'] = 1024;
$config['max_height'] = 768;
$config['overwrite'] = FALSE;
$config['encrypt_name'] = TRUE;
$this->load->library('upload', $config);

If you loop trough each photo and initialize the library with the $_FILES['file_name'] this will help you
$config['upload_path'] = './uploads/';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = 2000;
$config['max_width'] = 1024;
$config['max_height'] = 768;
$config['overwrite'] = FALSE;
$config['encrypt_name'] = TRUE;
$this->load->library('upload', $config);
$this->upload->do_upload('file_name');
$image_uploaded = $this->upload->data();
$filename = pathinfo($image_uploaded['full_path']);
$insert['photo'] = $filename['basename'];
$this->db->insert('your_table', $insert);

Don't fall back to the $_FILE superglobal. Since you're using CI, stick to CI's upload wrapper
Assign the output of the upload process to a variable like this:
$your_variable = $this->upload->data();
By doing this, you get all the upload data in your newly created $your_variable array:
$your_variable['file_name'] // encrypted name
$your_variable['file_type'] // MIME type such as image/jpeg or application/pdf
$your_variable['file_path'] // self explanatory
$your_variable['raw_name'] // encrypted name without the extension
$your_variable['orig_name'] // original name
$your_variable['client_name'] // name the file had on the client's computer
$your_variable['file_ext'] // self explanatory
$your_variable['file_size'] // self explanatory. Size in Kb
$your_variable['is_image'] // 1 if yes, 0 otherwise
$your_variable['image_width'] // only for images
$your_variable['image_height'] // only for images
$your_variable['image_type'] // only for images
$your_variable['image_size_str'] // only for images
I'm pretty sure those are all you get as of CI 3.1.10 (but I may have forgotten a couple, if so I apologize)
You need the ['file_name'] element for your purposes.
Going back to the $_FILE superglobal will not work, as CI won't modify its values

Related

Codeigniter obtain image name for multiple files

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.

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

Not getting the correct path after uploading a file

I am uploading a file on server and storing the its path in database in codeigniter, however i am after the upload i am not getting the correct path
The code that i have used is
$config['upload_path'] = './uploads/';
$config['allowed_types'] = 'doc|docx|pdf';
$config['max_size'] = 100;
$config['max_width'] = 1024;
$config['max_height'] = 768;
$this->load->library('upload', $config);
$this->upload->do_upload('userfile');
$data = array('upload_data' => $this->upload->data());
$image_path = $data['upload_data']['full_path'];
I wish to save the path that i am getting in $image_path inside database.
Now the path that i get is something like this
/home/litehepn/public_html/project_name/uploads/xyz.docx
But the path that i want is
project_name/uploads/xyz.docx
Can anyone please tell how to get the correct path
This is the correct path. Because you have stored your files inside that home directory. But You can use chop($url,"/home/litehepn/public_html/"); for getting it.

codeigniter upload multiple file different directory

In my form have 3 file upload input option.
bot are going to different directory. I can work with one directory but not work with multiple.
My code
$config['upload_path'] = './uploads/video';
$config['allowed_types'] = 'flv|mov|m4v|mp4';
$config['max_size'] = '30720';
$config['encrypt_name'] = TRUE;
$this->load->library('upload', $config);
$this->upload->do_upload('userfile');
$video_upload=$this->upload->data();
$config2['upload_path'] = './uploads/video';
$config2['allowed_types'] = 'jpg|jpeg|bmp|png';
$config2['max_size'] = '30720';
$config2['encrypt_name'] = TRUE;
$this->load->library('upload', $config2);
$this->upload->do_upload('thumbnail1');
$thumbnail_upload=$this->upload->data();
here video file upload successfully but image file not upload
$this->load->library() doesn't reload or reinitialize the library if it's already loaded.
In this case, you need to modify the existing loaded library options:
$this->upload->initialize($config2);
instead of
$this->load->library('upload', $config2);
Should do the trick.

how can I change allowed file type in codignetor which actually work?

Here is my controller code but only jpg and png file are uploaded I also changed it my process model.php also.
Is there any way to change it? And it doesn’t show any warning. You can check it from here but you need to register first...
public function saveTestimonial() {
$config['upload_path'] = './testimonial_photo/';
$config['allowed_types'] = 'gif|jpg|png|psd|zip|tif|ai|eps';
$config['max_size'] = '1024';
$config['max_width'] = '10240';
$config['max_height'] = '7680';
You should declare file upload like the below code. Since you are uploading images, docx, zip files you should not declare max_width and max_height. But you can change the max_size for the file.
$config['upload_path'] = 'testimonial_photo/';
$this->load->library('upload', $config);
$this->upload->initialize($config);
$this->upload->set_allowed_types('gif|jpg|png|jpeg|zip|pdf|doc|docx');
$this->upload->do_upload('filename_in_html_input')

Categories