view upload image in codeigniter - php

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) ; ?>" />

Related

Upload file and save its path in database using codeingiter

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

How can i Upload image files only using code igniter?

hi i'm trying to upload only image files using code igniter.I need to upload only png,jpg,jpeg files.but other type files also uploaded like .txt,.mp3 etc.my code is given below.please help me to solve this issue as immediately.Thanks to all in advance.
$config['upload_path'] = './avatar/';
$config['allowed_types'] = 'gif|jpeg|png|jpg';
$config['max_size'] = '1024mb';
$config['max_width'] = '3000';
$config['max_height'] = '3000';
$this->load->library('upload', $config);
if (!$this->upload->do_upload()) {
$ve['data'] = $this->upload->display_errors();
} else {
$this->upload->data();
}
$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);
// Alternately you can set preferences by calling the initialize function. Useful if you auto-load the //class:
$this->upload->initialize($config);
if($this->upload->do_upload())
{
$data = array('upload_data' => $this->upload->data());
$this->load->view('upload_success',$data);
}else
{
$error = array('error' => $this->upload->display_errors());
$this->load->view('file_view', $error);
}
I think you need to send in the user file/uploaded file while trying to validate it within the if condition.
if ( ! $this->upload->do_upload('userfile'))
{
$error = array('error' => $this->upload->display_errors());
$this->load->view('upload_form', $error);
}
else
{
$this->upload->data();
}

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

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);
}

You did not select a file to upload. PHP Code Igniter

I am trying to implement a post functionality and want to pick message and image from a php view. My message functionality is working good. But on image upload i receive an error You did not select a file to upload. This is my controller function
function post_func()
{
session_start();
echo $post_message=$_POST['post'];
echo $share_with=$_POST['share_with'];
echo $image=$_POST['image'];
if($image==null){
echo "<br/>no image<br/>";
}
else{
////////////////////////////////////////////////////////////////////////////////////////
$config['upload_path'] = './application/css';
$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);
if ( ! $this->upload->do_upload())
{
$error = array('error' => $this->upload->display_errors());
echo "<br/>";
echo $this->upload->display_errors();
echo "<br/> image error<br/>";
}
else
{
echo "<br/> reached <br/>";
session_start();
$this->membership_model->insert_images($this->upload->data(),$email);
$data = array('upload_data' => $this->upload->data());
echo "<br/ problem<br/>";
}
////////////////////////////////////////////////////////////////////////////////////////
}
$public;
if($share_with=="public"){
echo "1";
$public=true;
}else{
echo "0";
$public=false;
}echo "-----------------------------<br/>";
echo $user=$this->session->userdata('user_identification');
$data = array
(
'userid'=> $user,
'public' => $public,
'message' => $post_message,
'picname' => "None"
);
$this->load->model('membership_model');
$this->membership_model->add_message($data);
echo "</br>";
echo $user=$this->session->userdata('user_identification');
}
This is my view.
<?php echo form_open('search/post_func');?>
<!--<form id="loginForm" action="../search/post_func" method="post" enctype="multipart/form-data" >-->
<div id="your_post">
<div id="post_image">
<img id ="post_img" src="<?php echo $this->config->item('base_url'); ?><?php echo '/application/css/'. $img ?>"/>
</div>
<textarea name="post" rows="5" cols="30" placeholder="Share an update..." id="post_text" rows="2" value=""></textarea>
//other view items
<?php
echo form_close();
?>
Please help me
Change:
$this->upload->do_upload()
To this:
$this->upload->do_upload('my_file_input_name')
and it will work!! :)
Change:
<?php echo form_open_multipart('search/post_func');?>
Your form type should be "multipart"
Change your form tag to:
<?php echo form_open_multipart('search/post_func');?>
enctype='multipart/form-data'
you should put previous attribute in form tag
$this->upload->do_upload('file_name')
pass input tag file name to pervious line in your controller
<input type="file" name ="file_name" >
example code is
$config['upload_path'] = FCPATH.'upload/';
$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);
if ( ! $this->upload-> do_upload('userfile'))
{
$errors = $this->upload->display_errors();
$this->session->set_flashdata('error', $errors);
redirect('ur_link');
}
else
{
$data = array('upload_data' => $this->upload->data());
$fullpath= $data['upload_data']['full_path'];
$file_name = $data['upload_data']['file_name'];
}
}
Do this:
if(isset($_FILES['profile_pic']['name']) && !empty($_FILES['profile_pic']['name'])){
$config['upload_path']= './assets/img/';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = 10000;
$config['max_width'] = 10000;
$config['max_height'] = 10000;
$this->load->library('upload', $config);
if ( ! $this->upload->do_upload('profile_pic'))
{
$error = array('error' => $this->upload->display_errors());
print_r($error);
exit;
}
else
{
$data = array('upload_data' => $this->upload->data());
if(!empty($data)){
$img1 = $data['upload_data']['file_name'];
}
}
}
$insert_data['profile_pic'] = $img1;
Another reason I find this problem to be occurred. if you forget to initialize after load your library:
$this->load->library('upload', $config);
$this->upload->initialize($config); //Make this line must be here.
I hope this might help you.
So note I was doing a mysql upload import and I got the "You did not select a file to upload" message. It turns out my import sql file had extra columns which caused the fail and returned this error message.

Categories