Uploading file and display its name as a post - php

I am trying to upload a file and replace its name with the title name, but I am unable to get the file name by echo in controller even not in as a POST in profiler.
I also need to rename it but before that I need to know the post value ie file name.
Here I am posting my code.
My view
<?php echo form_open_multipart('emailtemplate/do_upload');?>
<tr class='odd gradeX'>
<td class='center'>
<input type="file" name="userfile" size="20" />
</td>
<td class='center'>
<input placeholder='Title For Your File' name='title' class='form-control'>
<td class='center'>
<input placeholder='Comment On File' name='comment' class='form-control'>
</td>
</tr>
<input type="submit" value="upload" />
</form>
My Controller
function do_upload()
{
$this->load->helper('form');
$this->load->helper('html');
$config['upload_path'] = './uploads/';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = '1000';
$config['encrypt_name'] = true;
//$file=$this->input->post('userfile');
$this->load->library('upload', $config);
$this->upload->data();
$file_name = $this->upload->do_upload('userfile');
echo $file_name;
if ( ! $this->upload->do_upload())
{
$error = array('error' => $this->upload->display_errors());
$this->load->view('add/addfile', $error);
}
else
{
$data = array('upload_data' => $this->upload->data());
$this->load->view('add/addfile', $data);
}
$this->output->enable_profiler(true);
}

To rename uploaded file with POST value:
//get extension
$ext = end(explode(".", $_FILES["userfile"]['name']));
$config['file_name'] = $this->input->post("title").'.'.$ext;
Remove $config['encrypt_name'] = true;. Otherwise your file name will be encrypted .
Make sure that your title doesn't not exist in your uploaded folder. Or append some random number / characters with title.

I have created a function to upload the file :-
it takes argument i.e field_name
public function fileUpload($field) {
//get file extension
$exts = #split("[/\\.]", $_Files[$field][name]);
$n = count($exts) - 1;
$ext = $exts[$n];
//create image name or replace it with your desired name
$imageName = date("Ymdhis") . time() . rand();
$config['file_name'] = $imageName; //set the desired file name
$config['overwrite'] = true;
$config['allowed_types'] = str_replace(',', "|", $ext);
$config['upload_path'] = ABSOLUTEPATH . 'product/original/';
$config['optional'] = true;
$config['max_size'] = '1000';
$config['max_width'] = '00';
$this->load->library('upload', $config);
$this->upload->initialize($config);
if (!$this->upload->do_upload($field, true)) {
$error = array('error' => $this->upload->display_errors());
$r = $error;
} else {
$data = array('upload_data' => $this->upload->data());
$r = "sucess";
}
return $r;
}

the bellow i have written code it can be helpful you
$config['upload_path'] = './uploads/path';
$config['allowed_types'] = 'png|jpg|jpeg|gif';
$config['max_size'] = '1000000';
$config['max_width'] = '1000000';
$config['max_height'] = '1000000000';
$this->load->library('upload', $config);
$files = $_FILES;
if(isset($files['file']['name']))
{
$filename = $files['file']['name'];
$ext = end(explode(".", $filename));
$_FILES['file']['name']= date('d').date('m').date('y').'_'.date(time()).'.'.$ext; //here i am changing file name with current date as your wish to change your logic
// print_r($_FILES);exit; check the changed name
if ($this->upload->do_upload('file'))
{
$upload = array('upload_data' => $this->upload->data());
$imagename = $upload['upload_data']['file_name']; //uploaded your image name
}
else
{
$error = array('error' => $this->upload->display_errors());
print_r($error); //error
}
}

NOTE:Hey user3345331 , See the line
$this->upload->do_upload()
Replace With this
$this->upload->do_upload('userfile');
This userfile is the ID of Input box with filetype in your HTML code.

Related

Upload multiple image doesn't store to databased and path folder

I'm making upload multiple image and stored to one field. But I have a proble with my code, images can't store to database and to path folder. May be you can help me please
This is my controller
public function post() {
if(empty($_FILES['file']['name'])) {
$data = array( 'id_merk' => $this->input->post('id_merk'),
'createdAt' => $this->input->post('createdAt')
);
// var_dump($data);
$this->m_barang->post( $data );
$this->session->set_flashdata('success', 'success');
redirect('admin/merk');
} else {
$count = count($_FILES['file']['size']);
foreach($_FILES as $value){
for($s=0; $s<=$count-1; $s++) {
$_FILES['file']['name'] = $value['name'][$s];
$_FILES['file']['type'] = $value['type'][$s];
$_FILES['file']['tmp_name'] = $value['tmp_name'][$s];
$_FILES['file']['error'] = $value['error'][$s];
$_FILES['file']['size'] = $value['size'][$s];
// $config['file_name'] = 'pict_'.date('Y_m_d_H_i_s').'.jpg';
$config['upload_path'] = './upload/be/barang';
$config['allowed_types'] = 'gif|GIF|jpg|JPG|jpeg|JPEG|png|PNG';
$config['max_size'] = '8000';
$config['max_width'] = '1366';
$config['max_height'] = '1024';
$this->load->library('upload', $config);
$this->upload->do_upload();
$data = $this->upload->data();
$name_array[] = $data['file_name'];
}
}
$names = implode(', ', $name_array);
$data = array( 'id_merk' => $this->input->post('id_merk'),
'photo_barang' => $names
);
$this->m_barang->post( $data );
$this->session->set_flashdata('success', 'gambar ada');
redirect('admin/merk');
}
}
and this is my view
<input class="form-control" name="file[]" id="files" type="file" multiple="multiple">
Please, can you help me how to fix my problem with my code?
First, Use only 1 loop to upload the multiple images.
Second, give name attribute of <input type="file"> to $this->upload->do_upload()
Third, if uploading path inside the application folder then use APPPATH.'upload/be/barang' or if outside the application folder use FCPATH.'upload/be/barang'
$count = count($_FILES['file']['size']);
for($s=0; $s<=$count-1; $s++) {
$_FILES['file']['name'] = $_FILES['file']['name'][$s];
$_FILES['file']['type'] = $_FILES['file']['type'][$s];
$_FILES['file']['tmp_name'] = $_FILES['file']['tmp_name'][$s];
$_FILES['file']['error'] = $_FILES['file']['error'][$s];
$_FILES['file']['size'] = $_FILES['file']['size'][$s];
$config['upload_path'] = './upload/be/barang';
$config['allowed_types'] = 'gif|GIF|jpg|JPG|jpeg|JPEG|png|PNG';
$config['max_size'] = '8000';
$config['max_width'] = '1366';
$config['max_height'] = '1024';
$this->load->library('upload', $config);
if(!$this->upload->do_upload('file')){
//image uploading error
}else{
$data = $this->upload->data();
$name_array[] = $data['file_name'];
}
}

upload different type files using codeigniter 3?

Is it possible to upload different files type using two input files? I created a form upload with two input and now I'm confused why the file name is same even when i uploaded different files with different name?
this is my controller :
public function file_data() {
$data['program'] = $this->input->post('program');
$this->load->library('upload');
$config['upload_path'] = './uploaded_files/laporan/absen';
$config['allowed_types'] = 'pdf';
$this->upload->initialize($config);
if (!$this->upload->do_upload('file_absen')) {
$error = array('error' => $this->upload->display_errors());
$this->load->view('tutor/upload_form', $error);
} else {
unset($config);
$config['upload_path'] = './uploaded_files/laporan/nilai';
$config['allowed_types'] = 'xls|xlsx';
$config['overwrite'] = FALSE;
//$config['max_size'] = '15000';
$this->upload->initialize($config);
if (!$this->upload->do_upload('file_nilai')) {
$error = array('error' => $this->upload->display_errors());
$this->load->view('tutor/upload_form', $error);
} else {
$upload_data = $this->upload->data();
//get the uploaded file name
$data['file_absen'] = $upload_data['file_name'];
$data['file_nilai'] = $upload_data['file_name'];
//store pic data to the db
$this->laporan_model->simpan_data($data);
redirect(base_url() . "tutor/laporan_np");
}
}
}
this is my model :
//fetch all data from db
function ambil_data(){
$all_files = $this->db->get('laporan');
return $files_pics->result();
}
//save datadata to db
function simpan_data($data){
$insert_data['program'] = $data['program'];
$insert_data['file_nilai'] = $data['file_nilai'];
$insert_data['file_absen'] = $data['file_absen'];
$query = $this->db->insert('laporan', $insert_data);
}
<div class="form-group">
<label for="file_nilai">File Nilai*:</label>
<input type="file" name="file_nilai" class="form-control" id="file_nilai">
</div>
<div class="form-group">
<label for="file_absen">File Absen*:</label>
<input type="file" name="file_absen" class="form-control" id="file_absen">
</div>
You could just rewrite your conditional to :
public function file_data() {
$data['program'] = $this->input->post('program');
$this->load->library('upload');
$config['upload_path'] = './uploaded_files/laporan/absen';
$config['allowed_types'] = 'pdf';
$this->upload->initialize($config);
if (!$this->upload->do_upload('file_absen')) {
$error = array('error' => $this->upload->display_errors());
$this->load->view('tutor/upload_form', $error);
} else {
$upload_data_absen = $this->upload->data(); // added this..
unset($config);
$config['upload_path'] = './uploaded_files/laporan/nilai';
$config['allowed_types'] = 'xls|xlsx';
$config['overwrite'] = FALSE;
//$config['max_size'] = '15000';
$this->upload->initialize($config);
if (!$this->upload->do_upload('file_nilai')) {
$error = array('error' => $this->upload->display_errors());
$this->load->view('tutor/upload_form', $error);
} else {
$upload_data_nilai = $this->upload->data(); // changed this..
//get the uploaded file name
$data['file_absen'] = $upload_data_absen['file_name']; // changed this..
$data['file_nilai'] = $upload_data_nilai['file_name']; // changed this..
//store pic data to the db
$this->laporan_model->simpan_data($data);
redirect(base_url() . "tutor/laporan_np");
}
}
}

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", ".");
}
}

Codeigniter file upload displaying error

Controller code
$config['upload_path'] = './uploads/'.$random;
$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('imageupload'))
{
$error = array('error' => $this->upload->display_errors());
}
else
{
$data = array('upload_data' => $this->upload->data());
}
print_r($data);
HTML code:
<form role="form" enctype="multipart/form-data" accept-charset="utf-8" name="formname" id="formname" method="post" action="http://www.example.com/Property/post">
<input type="file" class="default" id="imageupload1" name="imageupload[]">
</form>
Whenever I am uploading file it displaying warning is_uploaded_file() expects parameter 1 to be string, array given. How can I resolve it?
try this
//place this code in your function for multiple image upload
$data = array();
if(!empty($_FILES['imageupload']['name']))
{
$filesCount = count($_FILES['imageupload']['name']);
for($i = 0; $i < $filesCount; $i++){
$_FILES['imageupload']['name'] = $_FILES['imageupload']['name'][$i];
$_FILES['imageupload']['type'] = $_FILES['imageupload']['type'][$i];
$_FILES['imageupload']['tmp_name'] = $_FILES['imageupload']['tmp_name'][$i];
$_FILES['imageupload']['error'] = $_FILES['imageupload']['error'][$i];
$_FILES['imageupload']['size'] = $_FILES['imageupload']['size'][$i];
$uploadPath = 'uploads/';
$config['upload_path'] = $uploadPath;
$config['allowed_types'] = 'gif|jpg|png';
$this->load->library('upload', $config);
$this->upload->initialize($config);
if($this->upload->do_upload('imageupload')){
$fileData = $this->upload->data();
$uploadData[$i]['file_name'] = $fileData['file_name'];
$uploadData[$i]['created'] = date("Y-m-d H:i:s");
$uploadData[$i]['modified'] = date("Y-m-d H:i:s");
}
}
if(!empty($uploadData)){
//Insert file into the database using your model
}
}
Probably your input name should be without brackets:
<input type="file" class="default" id="imageupload1" name="imageupload">

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.

Categories