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.
Related
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')
I have a form that uploads many files and images at the same time.
the problem is this method
$this->upload->display_errors()
each time the loop goes through this method it saves error from the previous loop round
$error[$i]= "File error: ".$this->upload->display_errors();
for example, if I uploaded two illegal files it shows me this
index 0: file is not allowed
index 1: file is not allowed file is not allowed
So how can I reset this method?
ps: I tried to reset() function and it didn't work
UPDATE
This is the method
public function do_upload($product_id)
{
$error = array();
if(isset($_FILES['files']['name'])&&!empty($_FILES['files']['name'][0])):
for ($i=0; $i <count($_FILES['files']['name']) ; $i++) :
$config['upload_path'] = './images/';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = 1;
$this->load->library('upload', $config);
$_FILES['files[]']['name'] = $_FILES['files']['name'][$i];
$_FILES['files[]']['tmp_name'] = $_FILES['files']['tmp_name'][$i];
$_FILES['files[]']['size'] = $_FILES['files']['size'][$i];
if(!$this->upload->do_upload('files[]')){
$error[$i]= "File name: ". $_FILES['files']['name'][$i] ." ". $this->upload->display_errors() ."<br>";
}else{
$files = $this->upload->data();
$data = array('file_name'=>$files['file_name'],'product_id'=>$product_id,'file_type'=>$files['file_type']);
$this->Files_model->create_file($data);
}
endfor;
endif;
return $error;
//end method
}
We can't reset $this->upload->do_upload();
But we can reinitialize like that
// first upload
$this->config->load('upload');
-- Code to upload Here --
// Another file
$this->config->load('upload_other_files');
-- Code to upload Here --
OR You can defined multiple config array for each file Like below code :
$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->initialize($config);
For another files
$config2['upload_path'] = './uploads/';
$config2['allowed_types'] = 'gif|jpg|png';
$config2['max_size'] = '100';
$config2['max_width'] = '100';
$config2['max_height'] = '100';
$this->load->library('upload', $config2);
// Alternately you can set
$this->upload->initialize($config2);
You can reset (errors) like this:
$this->upload->error_msg = [];
...do_upload
I try to rename the upload images (3 images) and then insert to mysql.
The problem is, $this->db->insert_id(); has to put after the insert query and the $config['file_name'] has to put before uploaded.
How should I rename to auto-id then upload and insert?
$config['upload_path'] = 'images/location';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = 2048; //2048KB = 2MB
$config['max_width'] = 2000;
$config['max_height'] = 1500;
$this->load->library('upload', $config);
$this->upload->do_upload('userfile');
$data_upl['image1'] = $this->upload->data('file_name');
$this->upload->do_upload('userfile2');
$data_upl['image2'] = $this->upload->data('file_name');
$this->upload->do_upload('userfile3');
$data_upl['image3'] = $this->upload->data('file_name');
$data = array(
'location_pic' => $data_upl['image1'],
'location_pic2' => $data_upl['image2'],
'location_pic3' => $data_upl['image3']
);
$this->db->insert('oav_event', $data);
$get_id = $this->db->insert_id();
$config['file_name'] = "id".$get_id;
$this->load->view('admin/success', $data_upl);
I want to upload two images with my given name
$config['upload_path'] = '/path/to/file';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = '0';
$config['file_name'] ='logo1_'.$_POST['name'];
$config['max_width'] = '0';
$config['max_height'] = '0';
$this->load->library('upload', $config);
if ( ! $this->upload->do_upload('image1'))
{...}
else
{
$data = $this->upload->data();
}
$config2['upload_path'] = '/path/to/file';
$config2['allowed_types'] = 'gif|jpg|png';
$config2['max_size'] = '0';
$config2['file_name'] = 'logo2_'.$_POST['name'];
$config2['max_width'] = '0';
$config2['max_height'] = '0';
$this->load->library('upload', $config2);
if ( ! $this->upload->do_upload('image2'))
{...}
else
{
$data = $this->upload->data();
}
My first image correctly saved in my folder with the name logo1_myname but my second image dosen't save as logo2_myname it save as logo1_myname1 What is wrong with my code ?
Change
$this->load->library('upload', $config2);
to
$this->upload->initialize($config2);
Since the class has already been loaded codeigniter won't load it again.
Hope this helps.
I am trying to create a multiple image uploader and I have come across this link. What I am confused about in relation to my code below and the link is that do I have to have 2
$this->upload->do_upload(); functions to run my code or how do I use
$this->upload->initialize($config); in the below situation?
Code:
//Image Upload Function
$conceptOne = 'conceptOne';
$conceptTwo = 'conceptTwo';
$location = $_SERVER['DOCUMENT_ROOT'].'/_assets/quote/uploads/';
$folderName = $this->quote->getCompanyDetails()->companyName;
$folderName = str_replace(" ", "_", $folderName);
$folderName = strtolower($folderName);
if(!is_dir($location.$folderName))
{
mkdir($location.$folderName);
chmod($location.$folderName, 0777);
//Set File Settings
$config['upload_path'] = $location.$folderName;
$config['allowed_types'] = 'jpg|png|pdf';
$config['file_name'] = $conceptOne;
$config['remove_spaces'] = TRUE;
$config['overwrite'] = TRUE;
$config['max_size'] = '1024';
$config['max_width'] = '1024';
$config['max_height'] = '768';
$this->load->library('upload', $config);
print_r($config);
if(!$this->upload->do_upload($conceptOne)) { #= try upload
$data['uploadError'] = array('uploadError' => $this->upload->display_errors()); #Error
$this->load->view('layout', $data);
} // Do upload
else{
$data = array('upload_data' => $this->upload->data($conceptOne));
}// end else
}// end if folder
You need a loop to reinitialize the file upload library so that you can process some other images that are uploaded by the user.
Let's say a user uploaded 2 images. Then that means you need to put the code that initialize the file upload library and do the file upload inside that loop.
for ($i = 0; $i < 2; $i++)
{
// Change the config here if necessary
$this->upload->initialize($config);
// Call do_upload() here
}
Kemal is right: you have to iterate over the files you have. I would put the "concepts" in an array, so you can use foreach:
// Load upload library without any configuration
$this->load->library('upload');
$concepts = array('conceptOne','conceptTwo');
$location = $_SERVER['DOCUMENT_ROOT'].'/_assets/quote/uploads/';
$folderName = $this->quote->getCompanyDetails()->companyName;
$folderName = str_replace(" ", "_", $folderName);
$folderName = strtolower($folderName);
if(!is_dir($location.$folderName))
{
mkdir($location.$folderName);
chmod($location.$folderName, 0777);
}
$config['upload_path'] = $location.$folderName;
$config['allowed_types'] = 'jpg|png|pdf';
$config['remove_spaces'] = TRUE;
$config['overwrite'] = TRUE;
$config['max_size'] = '1024';
$config['max_width'] = '1024';
$config['max_height'] = '768';
// Upload 'concepts'
foreach($concepts as $concept)
{
$config['file_name'] = $concept;
$this->upload->initialize($config);
$this->upload->do_upload($concept);
}
// Upload logo
$config['file_name'] = 'logo-filename.gif';
$this->upload->initialize($config);
$this->upload->do_upload('logo');