codeigniter upload image store userfile in database rather than image name - codeigniter

Database output: userfile rather than image name?
rather than
CONTROLLER:
my upload directory is fine but when i use another method and call it when needed it doesnt give my desired output.. im very sorry for novicity
`
public function uploadImage()
{
$config['upload_path'] = './uploads/files';
$config['allowed_types'] = 'gif|jpg|png|jpeg';
$config['max_size'] = '2048';
$config['max_width'] = '2000';
$config['max_height'] = '2000';
$this->load->library('upload', $config);
if ( !$this->upload->do_upload()){
$error = array('error' => $this->upload->display_errors());
echo "<script>window.alert('failed to load UserFile');</script>";
}else{
$data = array('upload_data' => $this->upload->data());
$post_image = $_FILES['userfile']['name'];
echo "<script>window.alert('your image has been successfully uploaded');</script>";
}
}
`
public function signupPost()
{
if ($this->form_validation->run('signup') == FALSE) {
//fails wont continue to next page
$this->signup_view();
} else {
//upload image
$this->uploadImage();
//insert data into the database
$data = array(
'userId' => $this->input->post('userId'),
'userfile' => $this->input->post('userfile'),
);
//user data has successfully signed up
$this->User->signup_client($data);
redirect('http://localhost/GFC/index.php/main','refresh');
}
}
VIEW:
<?php
$attributes = array("name" => "signupform");
$hidden = array('userId' => 'userId', 'userfile' => 'userfile');
echo form_open_multipart("Client_Dashboard/signupPost", $attributes, $hidden);
?>
<div class="w3-row">
<input type="file" name="userfile" size="20" style="margin-left:30%; margin-top:8%;"/>
<span class="text-danger"><?php echo form_error('userfile'); ?></span>
</div>
<div class="container" style="padding:0%;">
<br>
<input type="submit" value="Register" class=" w3-btn w3-teal w3-large w3-hover-white w3-padding-large" />
</div>
<?php echo form_close(); ?>

You are not specifying the input and not returning the name of image to your function
function uploadImage() {
$config['upload_path'] = './uploads/files';
$config['allowed_types'] = 'gif|jpg|png|jpeg';
$config['max_size'] = '2048';
$config['max_width'] = '2000';
$config['max_height'] = '2000';
$this->load->library('upload', $config);
if (!$this->upload->do_upload('userfile')){
$error = array('error' => $this->upload->display_errors());
// var_dump($error); die; // check error
} else {
$fileName = $this->upload->data();
return $fileName;
}
}
function signupPost() {
if ($this->form_validation->run('signup') == FALSE) {
// fails wont continue to next page
$this->signup_view();
} else {
// upload image
$data['UserFile'] = $this->uploadImage();
// insert data into the database
$data = array(
'userId' => $this->input->post('userId'),
'password' => $this->input->post('password'),
'emailAddress' => $this->input->post('emailAddress'),
'userfile' => $this->input->post('userfile'),
'branchId' => $this->input->post('branchId')
);
// user data has successfully signed up
$this->User->signup_client($data);
redirect('http://localhost/GFC/index.php/main', 'refresh');
}
}

Related

file uploading multiple times in codeigniter

i am trying to upload files in codeigniter.but my files are uploading multiple times.this is my controller code.
public function savedocument() {
if (isset($_FILES) && !empty($_FILES)) {
$category_id = $_POST['category'];
$category_name = $_POST['category_name'];
$config = array();
$config['upload_path'] = 'uploadpath';
$config['allowed_types'] = 'gif|jpg|png|pdf|doc|docx';
$config['max_size'] = '20480';
$config['overwrite'] = FALSE;
$this->load->library('upload', $config);
$files = $_FILES;
for($i=0; $i< count($_FILES['files']['name']); $i++)
{
$_FILES['files']['name']= $files['files']['name'][$i];
$_FILES['files']['type']= $files['files']['type'][$i];
$_FILES['files']['tmp_name']= $files['files']['tmp_name'][$i];
$_FILES['files']['error']= $files['files']['error'][$i];
$_FILES['files']['size']= $files['files']['size'][$i];
$this->upload->initialize($config);
if (!$this->upload->do_upload('files')) {
$response['message'] = $this->upload->display_errors();
}
else {
$this->upload->do_upload('files');
$file_details = $this->upload->data();
//print_r($file_details);
$user_id = $this->session->userdata('user_id');
$uploaded_file_details = array(
'tmp_name' => $file_details["file_name"],
'file_name' => $file_details["orig_name"],
'size' => $file_details["file_size"],
'type' => $file_details["file_type"],
'category_name' => $category_name,
'category_id' => $category_id,
'upload_at' => date("Y-m-d H:i:s"),
'uploaded_by' => $user_id
);
$document_id = $this->document->CraeteNewDocument($uploaded_file_details);
}
redirect('admin/documents', 'refresh');
}
}
}
can anyone point out the issue.how to make if there is only one file to upload upload it once and if multiple files are selected.have to upload it.

How to keep same image during update in laravel without choosing image once again?

I want to update some records. Though I didnot want update image and click on update buuton, It shows me error-
Call to a member function getClientOriginalName() on null
Here is my controller
public function update(Request $request)
{
$participants = new participants;
$file = $request->file("select_file");
if($request->hasfile("select_file"))
{
$file->move("public/images/",$file->getClientOriginalName());
}
$id = $request->id;
$First_name = $request->First_name;
$Last_name = $request->Last_name;
$Date_of_Birth = $request->Date_of_Birth;
$GST_no = $request->GST_no;
$email = $request->email;
$Mobile_No = $request->Mobile_No;
$Company_name = $request->Company_name;
$Address = $request->Address;
$State = $request->State;
$City = $request->City;
$Pincode = $request->Pincode;
$Country = $request->Country;
$Amount = $request->Amount;
// $Image = $request->Image;
$Image = $file->getClientOriginalName();
DB::table('participants')
->where('id',$id )
->update(['First_name'=>$First_name,'Last_name'=>$Last_name,'Date_of_Birth'=>$Date_of_Birth,'GST_no'=>$GST_no,'email'=>$email,'Mobile_No'=>$Mobile_No,'Company_name'=>$Company_name,'Address'=>$Address,'State'=>$State,'City'=>$City,'Pincode'=>$Pincode,'Country'=>$Country,'Amount'=>$Amount,'Image'=>$Image]);
$participants->update();
session()->flash('success','Data Updated successfully!');
return redirect()->back();
//return back()->with('error','Update Data successfully!');
}
view page
<div class="form-group row">
<label for="File" class="col-md-2 col-form-label text-md-left">{{ __('Image') }}</label>
<div class="col-md-10">
<input type="file" name="select_file" class="form-control" value="{{$input[0]->Image}}">
<img src="/public/images/{{$input[0]->Image}}" alt="Image not Found" style="width:100px; height:100px;" />
</div>
</div>
I want the data to be updated with previous image(since I donot want to update image).
Make the change like this:
$file = $request->file("select_file");
if($request->hasfile("select_file"))
{
$file->move("public/images/",$file->getClientOriginalName());
$Image = $file->getClientOriginalName();
}
$id = $request->id;
$First_name = $request->First_name;
$Last_name = $request->Last_name;
$Date_of_Birth = $request->Date_of_Birth;
$GST_no = $request->GST_no;
$email = $request->email;
$Mobile_No = $request->Mobile_No;
$Company_name = $request->Company_name;
$Address = $request->Address;
$State = $request->State;
$City = $request->City;
$Pincode = $request->Pincode;
$Country = $request->Country;
$Amount = $request->Amount;
// $Image = $request->Image;
Now $file->getClientOriginalName(); will only be called when there is file uploaded.
More like you should change the image only when a new one is uploaded otherwise leave it the same
$file = $request->file("select_file");
if($request->hasfile("select_file")) {
$file->move("public/images/",$file->getClientOriginalName());
$Image = $file->getClientOriginalName();
} else {
$Image = $request->Image;
}
because you are using $Image variable in your update statement later on which will give an undefined error if you only define $Image in your if statement.
Or you could take a better approach to enhance your overall script like so:
public function update(Request $request)
{
$id = $request->id;
$update = [
'First_name' => $request->First_name,
'Last_name' => $request->Last_name,
'Date_of_Birth' => $request->Date_of_Birth,
'GST_no' => $request->GST_no,
'email' => $request->email,
'Mobile_No' => $request->Mobile_No,
'Company_name' => $request->Company_name,
'Address' => $request->Address,
'State' => $request->State,
'City' => $request->City,
'Pincode' => $request->Pincode,
'Country' => $request->Country,
'Amount' => $request->Amount
];
$file = $request->file("select_file");
if ($request->hasfile("select_file")) {
$file->move("public/images/", $file->getClientOriginalName());
$update['Image'] = $file->getClientOriginalName();
}
DB::table('participants')->where('id', $id)->update($update);
session()->flash('success', 'Data Updated successfully!');
return redirect()->back();
}
public function update(Request $request)
{
$participants = new participants;
$id = $request->id;
$file = $request->file("select_file");
if($request->hasfile("select_file"))
{
$file->move("public/images/",$file->getClientOriginalName());
$Image = $file->getClientOriginalName();
DB::table('participants')
->where('id',$id )
->update(['Image'=>$Image]);
}
$id = $request->id;
$First_name = $request->First_name;
$Last_name = $request->Last_name;
$Date_of_Birth = $request->Date_of_Birth;
$GST_no = $request->GST_no;
$email = $request->email;
$Mobile_No = $request->Mobile_No;
$Company_name = $request->Company_name;
$Address = $request->Address;
$State = $request->State;
$City = $request->City;
$Pincode = $request->Pincode;
$Country = $request->Country;
$Amount = $request->Amount;
DB::table('participants')
->where('id',$id )
->update(['First_name'=>$First_name,'Last_name'=>$Last_name,'Date_of_Birth'=>$Date_of_Birth,'GST_no'=>$GST_no,'email'=>$email,'Mobile_No'=>$Mobile_No,'Company_name'=>$Company_name,'Address'=>$Address,'State'=>$State,'City'=>$City,'Pincode'=>$Pincode,'Country'=>$Country,'Amount'=>$Amount]);
$participants->update();
session()->flash('success','Data Updated successfully!');
return redirect()->back();
//return back()->with('error','Update Data successfully!');
}

How to get the uploaded filename from upload.php page Codeigniter

I have Upload.php page as follows=>
<?php
class Upload extends CI_Controller {
public function __construct() {
parent::__construct();
$this->load->helper(array('form', 'url'));
}
public function index() {
$this->load->view('upload_form', array('error' => ' ' ));
}
public function do_upload() {
$config['upload_path'] = './uploads/';
$config['allowed_types'] = 'gif|jpg|png|pgn';
$config['max_size'] = 0;
//$config['max_width'] = 1024;
//$config['max_height'] = 768;
$config['detect_mime'] = TRUE;
$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);
}
}
}
?>
and the Hesaplama.php controller as =>
$upload_data = $this->upload->data(); //(line 51) Should return array of containing all of the data related to the file you uploaded.
$file_name = $upload_data['file_name'];
$file = fopen("<?php echo site_url('uploads/$file_name'); ?>", "r");
while(! feof($file))
{...
However I get the following error =>
A PHP Error was encountered
Severity: Notice
Message: Undefined property: Hesaplama::$upload
Filename: controllers/Hesaplama.php
Line Number: 51
Backtrace:
File: D:\wamp\www\proje\application\controllers\Hesaplama.php
Line: 51
Function: _error_handler
File: D:\wamp\www\proje\application\controllers\Hesaplama.php
Line: 249
Function: pgn_oku
File: D:\wamp\www\proje\application\controllers\Welcome.php
Line: 28
Function: pozisyon_tutma
File: D:\wamp\www\proje\index.php
Line: 292
Function: require_once
How can we remedy that and get the file name of the newly uploaded file?
I personally think that the program does not recognize the data in another page(controller).
Thank you...
Sorry i can't comment yet, so i write it here. I think the library for
$this->upload->data();
wasn't loaded.
if you want to get only the filename, maybe you can try using session.
in Upload controller:
$data = $this->upload->data();
$this->session->set_userdata('filename', $data['file_name']);
in Hesaplama :
$filename = 'uploads/'.$this->session->userdata('filename');
$file = fopen("<?php echo site_url('$filename'); ?>", "r");
hope this help.
to get the filename and extention file to save it in a db you can use tis
<?php
class Upload extends CI_Controller {
public function __construct() {
parent::__construct();
$this->load->helper(array('form', 'url'));
}
public function index() {
$this->load->view('upload_form', array('error' => ' ' ));
}
public function do_upload() {
$config['upload_path'] = './uploads/';
$config['allowed_types'] = 'gif|jpg|png|pgn';
$config['max_size'] = 0;
$config['detect_mime'] = TRUE;
$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 {
$upload_data = $this->upload->data();
/*
Array
(
[file_name] => mypic.jpg
[file_type] => image/jpeg
[file_path] => /path/to/your/upload/
[full_path] => /path/to/your/upload/jpg.jpg
[raw_name] => mypic
[orig_name] => mypic.jpg
[client_name] => mypic.jpg
[file_ext] => .jpg
[file_size] => 22.2
[is_image] => 1
[image_width] => 800
[image_height] => 600
[image_type] => jpeg
[image_size_str] => width="800" height="200"
)
*/
$this->model->save_img_info($upload_data['file_name']);
$data = array('upload_data' => $upload_data );
$this->load->view('upload_success', $data);
}
}
}
?>

file_get_contents() expects parameter 1 to be a valid path

My image is uploading to images/news successfully. I want to now save that image in the database. but it keeps giving me the above error. My model query is correct because i use it to insert other data and it works.
How can i save the image into the database once the user clicks submit?
My controller:
function news()
{
$config = array(
'upload_path' => "./images/news",
'allowed_types' => "gif|jpg|png|jpeg|JPG",
'overwrite' =>False,
'max_size' => "2048000",
'max_height' => "768",
'max_width' => "1024"
);
$this->load->library('upload', $config);
$this->load->helper(array('form', 'url'));
if($this->upload->do_upload())
{
$data = array('upload_data' => $this->upload->data());
$this->load->view('manage_news',$data);
}
else
{
$message2 = "Please choose another file type. gif,jpg,png,jpeg,pdf ";
echo "<script type='text/javascript'>alert('$message2'); </script>";
redirect('resetPasswordController/add_news', 'refresh');
}
$this->db->trans_start();
$data = array('upload_data' => $this->upload->data());
$data = array(
'image' => file_get_contents( $data )
);
$this->load->model('users_model');
$this->users_model->insert($data);
$this->db->trans_complete();
$this->db->trans_complete();
redirect('resetPasswordController/manage_news', 'refresh');
}
my view:
<?php echo form_open_multipart('resetPasswordController/news');?>
<label>Image</label>
<input name = "userfile" type="file" />
my model:
function insert($data){
$this->db->insert('news',$data);
return $this->db->insert_id();
}
$data contain array values, you have to use upload_data
for ex :
$data = array('upload_data' => $this->upload->data());
$data = array(
'image' => file_get_contents( $data )
);
change this to
$data = array('upload_data' => $this->upload->data());
$data = array(
'image' => file_get_contents( $data['upload_data'] )
);
or
you can directly use upload data function
$data = array(
'image' => file_get_contents( $this->upload->data())
);

Codeigniter view url?

i cal upload images to folder if i use absolute url.
but how to use absolute url?
function do_upload()
{
$config['upload_path'] = 'upload/';
$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())
{
$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);
}
}
The above is my code for controller.
i try to upload, but i can't judge in which path the folder should be located? either it should be in root or in controller or in view?
It should go in root.

Resources