Upload file and save its path in database using codeingiter - php

I wish to upload a file to folder and then save its path in database, but i got stuck in first step only
whenever i am trying to upload a file i am getting a message
You did not select a file to upload
the code that i used is
view
<?php
echo form_open_multipart('recruiter/adddata);
$data = array(
'type'=>'file',
'name'=>'userfile',
'class'=>'fileinput btn-info',
'id'=>'filename3',
'data-filename-placement'=>'inside',
'title'=>'Resume'
);
echo form_upload($data);
$data = array(
'type'=>'submit',
'class'=>'btn btn-danger',
'name'=>'submit',
'content'=>'Submit'
);
echo form_button($data);
echo form_close();
?>
Controller
$config['upload_path'] = './uploads/';
$config['allowed_types'] = 'doc|docx|pdf';
$config['max_size'] = 10000;
$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());
print_r($error); //only for checking purposes
}
else
{
$data = array('upload_data' => $this->upload->data());
print_r($data); //only for checking purposes
}
Can anyone please tell how can i upload the file and save its path in database

Make some changes like:
echo form_upload($data);
to
<?php echo form_open_multipart();?>
Controller function part:
//start of file upload code
$config['upload_path'] = './uploads/';
$config['allowed_types'] = '*';
$this->load->library('upload', $config);
$this->upload->do_upload();
$data = $this->upload->data();
//end of file upload code

Related

Codeigniter Upload Image Choice

I have a form including different inputs and file upload input. I want the users if they want to upload images then upload, but if they don't want to upload. Don't give an error.
if($_POST){
$config['upload_path'] = 'images/tmp';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = 2048;
$config['min_width'] = 480;
$config['min_height'] = 360;
$this->load->library('upload', $config);
if (!$this->upload->do_upload('userfile')) {
$error = array('error' => $this->upload->display_errors());
//Sending this $error to view and if a user didnt upload image and just filled
//the other inputs it shows error. but i dont want that.
} else { }
} else {
redirect(site_url());
}
You are on right track. Just delete or comment this line
$error = array('error' => $this->upload->display_errors());
You might be send your $error to view file like below:
$this->load->view('upload_form', $error);
So Don't send your value to view file. Just call your view when image successfully uploaded below:
if ( ! $this->upload->do_upload('userfile') )
{
// $error = array('error' => $this->upload->display_errors());
/*Delete or comment this line. Do your other stuff here if image not uploaded.*/
}else{
$data = array('upload_data' => $this->upload->data());
$this->load->view('upload_success', $data);
/* This is example. You can do anything on successfully image upload.*/
}
You can refer codeigniter manual https://www.codeigniter.com/userguide3/libraries/file_uploading.html
$config['upload_path'] = 'images/tmp';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = 2048;
$config['min_width'] = 480;
$config['min_height'] = 360;
$this->load->library('upload', $config);
//$rand_str="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
// $filename=md5(str_shuffle($rand_str.mt_rand()));
// $config['file_name']=$filename;
$this->upload->initialize($config);
if ( $this->upload->do_upload('userfile'))
{
//$data = array('upload_data' => $this->upload->data());
// $data["filename"]=$filename;
// $data["ad_id"]=$lid;
// $data["filename"]=$rand_name;
// $this->Ads_model->insert_image($data);
}
I solved my problem changing the Upload class to give no error. I commented the lines including "no selected files".

How to upload multiple files with multiple inputs in codeigniter

How to upload multiple files with multiple inputs in codeigniter. Below is my code. I want to add many files but with different inputs for files.
if(!empty($_FILES['countryfile']['name']))
{
$filesCount = count($_FILES['countryfile']['name']);
for($i = 0; $i < $filesCount; $i++)
{
$imgFile=$_FILES['countryfile']['name'][$i];
$tmp_dir=$_FILES['countryfile']['tmp_name'][$i];
$imgSize=$_FILES['countryfile']['size'][$i];
//$upload_dir='../uploads/dish_images';
$imgExt=strtolower(pathinfo($imgFile,PATHINFO_EXTENSION));
$valid_extensions = array('jpeg', 'jpg', 'png', 'gif','pdf');
$image=rand(1000,10000).".".$imgExt;
$config['upload_path'] = '../admin/upload_doc';
//$config['upload_path'] = 'http://teq-staging.com/maswad-phase2/admin/uploads/dish_images/';
$config['allowed_types'] = 'jpg|jpeg|png|gif|pdf';
$config['file_name'] = $_FILES['countryfile']['name'];
$upload_dir=$config['upload_path'];
//$config['file_name']=$image;
$upload_dir=$config['upload_path'];
$this->upload->initialize($config);
if($this->upload->do_upload('countryfile')){
$upload_data = $this->upload->data();
}
}
upload form.php
<?php echo form_open_multipart('upload'); ?>
<p>
<?php echo form_label('File 1', 'userfile') ?>
<?php echo form_upload('userfile') ?>
</p>
<p>
<?php echo form_label('File 2', 'userfile1') ?>
<?php echo form_upload('userfile1') ?>
</p>
<p><?php echo form_submit('submit', 'Upload them files!') ?></p>
<?php form_close() ?>
the controller
function index()
{
// Has the form been posted?
if (isset($_POST['submit']))
{
// Load the library - no config specified here
$this->load->library('upload');
// Check if there was a file uploaded - there are other ways to
// check this such as checking the 'error' for the file - if error
// is 0, you are good to code
if (!empty($_FILES['userfile']['name']))
{
// Specify configuration for File 1
$config['upload_path'] = 'uploads/';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = '100';
$config['max_width'] = '1024';
$config['max_height'] = '768';
// Initialize config for File 1
$this->upload->initialize($config);
// Upload file 1
if ($this->upload->do_upload('userfile'))
{
$data = $this->upload->data();
}
else
{
echo $this->upload->display_errors();
}
}
// Do we have a second file?
if (!empty($_FILES['userfile1']['name']))
{
// Config for File 2 - can be completely different to file 1's config
// or if you want to stick with config for file 1, do nothing!
$config['upload_path'] = 'uploads/dir2/';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = '100';
$config['max_width'] = '1024';
$config['max_height'] = '768';
// Initialize the new config
$this->upload->initialize($config);
// Upload the second file
if ($this->upload->do_upload('userfile1'))
{
$data = $this->upload->data();
}
else
{
echo $this->upload->display_errors();
}
}
}
else
{
$this->load->view("upload_form");
}
}

view upload image in codeigniter

I have program to upload image and display result from upload. But when I display image only show path image not image. And in database I save by full_path. Please tell me code how to display image from my code.
Here is the controller:
function do_upload()
{
$config['upload_path'] = './images/';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = '100000';
$config['max_width'] = '10240';
$config['max_height'] = '7680';
$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 = array('upload_data' => $this->upload->data());
print_r($this->upload->data());
$datafoto=$this->upload->data();
$nm_file = time().$datafoto['full_path'];
$this->load->model('mkegiatan');
$this->mkegiatan->update_foto($nm_file);
copy('images/'.$datafoto['full_path'], 'images/'.$nm_file);
}
}
Here is the view:
<?php echo e($kegiatan->image) ; ?>
So you said you see just image path and not image itself? Try to replace this:
<?php echo e($kegiatan->image) ; ?>
With this:
<img src="/<?php echo e($kegiatan->image) ; ?>" />

How to get file upload progress from the code igniter?

I just want to get the file upload progress in code Igniter.Please don't recommend any j-query integration as it is not the matter right now. Just give me a plain solution that could be done to get file progress in code igniter.
This is my code to uplaod pictures:
$base=$this->config->item('base_url');
$images=$this->config->item('images');
$config['upload_path'] = './images/teacherimages';
$config['allowed_types'] = 'gif|jpg|png';
$this->load->library('upload', $config);
if ( ! $this->upload->do_upload())
{
$error = array('error' => $this->upload->display_errors());
echo "Error<br>";
}
else
{
$data = array('upload_data' => $this->upload->data());
$upload_data = $this->upload->data();
$pic = $upload_data['file_name'];
$this->load->model('Teacher/InsertModel');
$this->InsertModel->updatePic($pic,$id);
}

codeigniter file is not uploading on remote server

this is a code for swf fiel uploading but the problem is it works fine on localhost but not in the remote server.here is my controller
function do_upload()
{
$pid=$this->input->post('Page_id');
$config['upload_path'] = './uploads/';
$config['allowed_types'] = 'swf';
$config['max_size'] = '1048';
$config['file_name'] =$pid;
$this->load->library('upload', $config);
if ( ! $this->upload->do_upload())
{
$error = array('error' => $this->upload->display_errors());
}
else{
$data=array(
'id'=>$pid,
'link'=>base_url()."uploads/",
'file_name'=>$config['file_name']
);
$this->file_upload_model->upload_data($data);
$this->upload_success();
}
}
i have not done anything except just uploading the path and file name to the database in model. see the demo here
Try this
$pid=$this->input->post('Page_id');
$config['file_name'] = $pid;
$config['upload_path'] = './uploads/';
$config['allowed_types'] = 'swf';
$config['max_size'] = '1048';
$config['remove_spaces'] = TRUE;
$this->load->library('upload', $config);
if ($this->upload->do_upload())
{
//upload successful
//do something
}
else
{
$this->session->set_flashdata('error',$this->upload->display_errors());
redirect('controller/method');
/*
In the view file you can echo the error like this
echo $this->session->flashdata('error');
*/
}
You have pass the the field name in the do_upload call like this:
$config = array(
'allowed_types' => 'jpg|jpeg|swf |png', // pipe seperated file types
'upload_path' => './uploads/', // should be the root path
'max_size' => '1048',
'file_name' => $pid
);
$this->load->library('upload', $config);
if (!$this->upload->do_upload('your_field_name')) {
// like $this->upload->do_upload($pid)
$error = array('error' => $this->upload->display_errors());
} else {
$swf_data = $this->upload->data();
}
Hope it makes sense
it may be due to PHP server version and it's option.
1).go to cpanel account
2).click "select php version", in the software section.
3).tap the "fileinfo" chexbox in the php option and save.
now you can upload file perfectly.

Categories