How to upload multipl file fiedls in codeigniter - php

I have form with 4 file fields and I want to upload these files properly.
My code is as follows :
public function do_upload(){
$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('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);
}
}

private function fileUpload($name, $config = []){
$config['upload_path'] = './uploads/';
$config['encrypt_name'] = TRUE;
$config['file_ext_tolower'] = TRUE;
$config['allowed_types'] = 'jpg|jpeg|png';
$this->upload->initialize($config);
if ( ! $this->upload->do_upload($name))
{
$error = array('error' => true, 'message' => $this->upload->display_errors());
return $error;
}
else
{
return array('error' => false, 'upload_data' => $this->upload->data());
}
}
Create this private function in the controller just call this function when you have to upload the file with the name of the file field if you want some other config just pass config array to as a next parameter
$data = $this->fileUpload('site_logo_dark')
if($data['error']){
echo data['message']; // error message while uplaoding an file if have any
}else{
echo $data['upload_data']['file_name']; // return you a name of the file which you just upload it you can save it to the database
}

Related

Codeigniter multiple upload with different area name and rename the file before uploaded

I have a problem with multiple files upload with different area name and want to change for every area filename before it's upload.
This is HTML form.
<input type="file" placeholder="" name="profilPic"/>
<input type="file" placeholder="" name="topPic"/>
This is controller
$config['upload_path'] = './uploads/';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = 100;
$config['max_width'] = 1024;
$config['max_height'] = 768;
//$config['file_name'] = $this->session->sersession["id"];
$this->load->library('upload', $config);
$profilPic = $this->upload->do_upload('profilPic');
if (!$profilPic){
$error = array('error' => $this->upload->display_errors());
$this->session->set_flashdata("error", "profil pic was not uploaded= ");
}else{
$data = array('upload_data' => $this->upload->data());
$this->session->set_flashdata("success", "profil picture was uploaded.");
}
$topPic = $this->upload->do_upload('topPic');
if (!$topPic){
$error = array('error' => $this->upload->display_errors());
$this->session->set_flashdata("error", "top pic was not uploaded" );
}else{
$data = array('upload_data' => $this->upload->data());
$this->session->set_flashdata("success", "this picture was uploaded.");
}
Note: The pictures are upload to directory. But i want to rename every file file name before uploaded like "userID_profil.jpg" and "userID_top.jpg"
You can set the $config['file_name'] before the second file upload using
$this->upload->initialize($config);
Of course, you also need to set it for the first file either with $this->load->library('upload', $config) or $this->upload->initialize($config).
Docs: https://www.codeigniter.com/userguide3/libraries/file_uploading.html#setting-preferences
I solved it.
$config['upload_path'] = './uploads/';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = 100;
$config['max_width'] = 1024;
$config['max_height'] = 768;
if($_FILES["profilPic"]["name"]){
$config["file_name"] = $this->session->usersession["id"]."_profil.jpg";
$this->load->library('upload', $config);
$profilPic = $this->upload->do_upload('profilPic');
if (!$profilPic){
$error = array('error' => $this->upload->display_errors());
$this->session->set_flashdata("error", ".");
}else{
$profilPic = $this->upload->data("file_name");
$data = array('upload_data' => $this->upload->data());
$this->session->set_flashdata("success", ".");
}
}
if($_FILES["topPic"]["name"]){
$config["file_name"] = $this->session->usersession["id"]."_top.jpg";
if($_FILES["profilPic"]["name"]){
$this->upload->initialize($config);
}else{
$this->loadl->library('upload', $config);
}
$topPic = $this->upload->do_upload('topPic');
if (!$topPic){
$error = array('error' => $this->upload->display_errors());
$this->session->set_flashdata("error", "" );
}else{
$topPic = $this->upload->data("file_name");
$data = array('upload_data' => $this->upload->data());
$this->session->set_flashdata("success", ".");
}
}

Conflict when uploading images with multiple file field in codeigniter

Codeigniter: File successfully upload when setting only one field(individual). But when setting both field second file is also uploaded to first's location. How to solve this conflict.
Is there any tutorial for multiple upload field????
My code is given below:
<input type="file" name="filePrdimage" id="filePrdimage" size="20"/>
<input type="file" name="filePrdlogo" id="filePrdlogo" size="20"/>
. // Image uploading codes
$config['upload_path'] = 'assets/images/b2bproduct';
$config['allowed_types'] = 'gif|jpg|jpeg|png';
$config['max_size'] = '1000';
$config['max_width'] = '2024';
$config['max_height'] = '1768';
$config['overwrite'] = TRUE;
$config['remove_spaces'] = TRUE;
if (isset($_FILES['filePrdimage']['name'])) {
$config['file_name'] = substr(md5(time()), 0, 28) . $_FILES['filePrdimage']['name'];
}
$this->load->library('upload', $config);
if (!$this->upload->do_upload('filePrdimage')) {
//no file uploaded or failed upload
$error = array('error' => $this->upload->display_errors());
} else {
$dat = array('upload_data' => $this->upload->data());
$this->resize($dat['upload_data']['full_path'], 'assets/images/b2bproduct/thump/'.$dat['upload_data']['file_name'],180,400);
}
if (empty($dat['upload_data']['file_name'])) {
$prdimage = $this->input->post('hdPrdimage');
}
else {
$prdimage = $dat['upload_data']['file_name'];
}
// End Image uploading Codes
// Logo uploading codes
$config['upload_path'] = 'assets/images/b2blogo';
$config['allowed_types'] = 'gif|jpg|jpeg|png';
$config['max_size'] = '1000';
$config['max_width'] = '2024';
$config['max_height'] = '1768';
$config['overwrite'] = TRUE;
$config['remove_spaces'] = TRUE;
if (isset($_FILES['filePrdlogo']['name'])) {
$config['file_name'] = substr(md5(time()), 0, 28) . $_FILES['filePrdlogo']['name'];
}
$this->load->library('upload', $config);
if (!$this->upload->do_upload('filePrdlogo')) {
//no file uploaded or failed upload
$error = array('error' => $this->upload->display_errors());
} else {
$dat = array('upload_data' => $this->upload->data());
$this->resize($dat['upload_data']['full_path'], 'assets/images/b2blogo/'.$dat['upload_data']['file_name'],135,300);
}
if (empty($dat['upload_data']['file_name'])) {
$prdlogo= $this->input->post('hdPrdlogo'); ;
}
else {
$prdlogo=$dat['upload_data']['file_name'];
}
// End Logo uploading Codes
public function resize($source,$destination,$width,$height) {
$config['image_library'] = 'gd2';
$config['source_image'] = $source;
$config['create_thumb'] = FALSE;
$config['maintain_ratio'] = TRUE;
$config['width'] = $width;
$config['height'] = $height;
$config['new_image'] = $destination;
$this->load->library('image_lib', $config);
$this->image_lib->resize();
}
I think you should do only one function to upload images that has data array as parameter where you may include the path and other variables.
public function upload(array $data)
{
$config['upload_path'] = isset($data['upload_path']) ? $data['upload_path'] : 'default/path';
...
$this->load->library('upload', $config);
if (!$this->upload->do_upload($data['field')) {
//no file uploaded or failed upload
$error = array('error' => $this->upload->display_errors());
} else {
$dat = array('upload_data' => $this->upload->data());
$this->resize($dat['upload_data']['full_path'], $data['upload_path'].$dat['upload_data']['file_name'],135,300);
}
...
}
When you submit the form, check for all input fields with the $_FILES global and call the upload function when necessary with the upload array $data set for each of them.

uploading file data and textbox ,radio button values to database

i like to upload a file in that form it contain some text boxes and one fileupload button,
how to do it in codeigniter,
when i did that in separately i can but with file upload i cant
function upload()
{
$config['upload_path'] = './uploads/';
$config['allowed_types'] = 'gif|jpg|png|txt|pdf|doc|docx';
$config['max_size'] = '1024';
$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());
$this->load->view('js_home', $error);
}
else
{
$data = array('upload_data' => $this->upload->data(),'phone' =>$this->input->post('phone'),'email' =>$this->input->post('email'),'password' =>$this->input->post('password'));
$this->jobseeker->storefile($data);
$this->load->view('samples/upload_success', $data);// after uploaded the file
}
}
try this
if ( ! $this->upload->do_upload('file'))
{
$error = array('error' => $this->upload->display_errors());
$this->load->view('js_home', $error);
}
else
{
$fInfo = $this->upload->data();
$data = array('upload_data' => $fInfo['file_name'],'phone' =>$this->input->post('phone'),'email' =>$this->input->post('email'),'password' =>$this->input->post('password'));
$this->jobseeker->storefile($data);
$this->load->view('samples/upload_success', $data);// after uploaded the file
}
in view <input type='file' name='file'>

Codeigniter : Filename didn't appear on database

I just learn to save image data to database, those are filename and path. The path is appear on database, but not with filename. Whats the problem?
This is the controller,
function do_upload() {
$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);
$this->upload_model->upload($config);
if(!$this->upload->do_upload()){
$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);
}
}
and the model
function upload ($config) {
$config = $this->upload->data();
$upload_data = array(
'path' => $config['full_path'],
'nama_foto' => $config['file_name']
);
$this->db->insert('tb_picture', $upload_data);
}
and the table
what should i do?
thank you before.
before reading this please try it again yourself, or try any kind of viedotutorial that is on the web http://net.tutsplus.com/sessions/codeigniter-from-scratch/
controller function should look like this
function do_upload()
{
$config['upload_path']='./uploads/'; //needs to set uploads folder CHMOD 0644
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = '100';
$config['max_width'] = '1024';
$config['max_height'] = '768';
$config['overwrite'] = FALSE;
$config['remove_spaces'] = TRUE;
$field_name = "userfile"; //name tag in our HTML form in case you want to change it
$this->load->library('upload', $config);
if ( ! $this->upload->do_upload($field_name)) //upload happens
{
$error = array('error' => $this->upload->display_errors());
$this->load->view('upload_form', $error);
}
else
{
//succesful upload get data from upload and use them with our model
$upload_data = $this->upload->data();
$this->upload_model->upload($upload_data);
}
}
model function
function upload ($data) {
if (empty($data) || $data === FALSE) return FALSE;
$insert_data = array(
'path' => $data['full_path'],
'nama_foto' => $data['file_name']
);
$this->db->insert('tb_picture', $insert_data);
return $this->db->insert_id(); //returns last inserted ID
}
Please note that your model was totaly "wrong" you used upload function in it, try to pass data only to it so model can process it.

How to store path image users into a database: Codeigniter

I am trying to upload the paths of the pictures for different users into at database.
I am quite new on codeigniter and I read a lot of tutorials, but I am sill struggling.
This is my controller but I don't even know how to link the image with the user session.
Here is my code:
function do_upload()
{
$config['upload_path'] = './uploads/';
$config['allowed_types'] = 'gif|jpg|jpeg|png';
$config['max_size'] = '100';
$config['max_width'] = '1024';
$config['max_height'] = '768';
$config['overwrite'] = false;
$this->load->library('upload', $config);
if ( ! $this->upload->do_upload())
{
$error = array('error' => $this->upload->display_errors());
$this->load->view('upload_form', $error);
}else
{
$data = $this->upload->data();
$file_array = $this->upload->data('file_name');
$profile['profile_picture'] = $file_array['file_name'];
$this->db->where('username', $this->input->post('username'));
$this->db->update('users', $profile);
$this->load->view('upload_success', $data);
}
}
My database has a table called users and 'profile_picture' is the field for the path. When I upload an image, I just get all my users fields filled with the same file name.
thanks for the tips guys I solved in this way:
function do_upload(){
if($this->session->userdata('is_logged_in'))
{
$id = $this->session->userdata('id');
$config['upload_path'] = './uploads/';
$config['allowed_types'] = 'gif|jpg|jpeg|png';
$config['max_size'] = '100';
$config['max_width'] = '1024';
$config['max_height'] = '768';
$config['overwrite'] = false;
$this->load->library('upload', $config);
if ( ! $this->upload->do_upload())
{
$error = array('error' => $this->upload->display_errors());
$this->load->view("site_header");
$this->load->view("site_nav");
$this->load->view('upload_form', $error);
}else{
$data = $this->upload->data();
$file_array = $this->upload->data('file_name');
$profile['profile_picture'] = $file_array['file_name'];
$this->db->where('id', $id);
$this->db->update('users', $profile);
$this->load->view('upload_success', $data);
}
}else{
$this->load->view("site_header");
$this->load->view("site_nav");
$this->load->view("login_view");
$this->load->view("site_footer");
}
}
If your use has an ID, just do imagePath = /upload/avatar/'.$userID.'.jpg' or use a random filename and store it as a new field

Categories