How to get the uploaded filename from upload.php page Codeigniter - 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);
}
}
}
?>

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.

codeigniter upload image store userfile in database rather than image name

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

How to upload renamed image in codeigniter

I want to upload image in codeigniter. But how to upload renamed image, please see the below code.
$filename = $_FILES['filename']['name']."_".date("Y-m-d")."_".date("H:i:s");
public function index()
{
$this->form_validation->set_rules('title', 'Title', 'required');
$this->form_validation->set_rules('description', 'Description', 'required');
$this->form_validation->set_rules('author', 'Author', 'required');
$filename = $_FILES['filename']['name']."_".date("Y-m-d")."_".date("H:i:s");
$this->do_upload();
$this->load->model('News_model');
$this->News_model->insert_news($filename);
$this->load->view('news/success', $data);
}
this is what I'm doing to do that:
$folder = "./Path/To/Uploaded/files"
$config['upload_path'] = $folder;
// We initiate the CI upload library
$this->load->library('upload', $config);
if (!$this->upload->do_upload('file')) {
$error = array('error' => $this->upload->display_errors());
} else {
// Upload success, we get back the image info
$data = array('upload_data' => $this->upload->data());
// we rename the file
$filename = $data['upload_data']['raw_name'] . "_".date("Y-m-d") . "_" . date("H:i:s") . $data['upload_data']['file_ext'];
rename($data['upload_data']['full_path'], $folder . $filename);
}
See details in Codeigniter documentation : https://www.codeigniter.com/user_guide/libraries/file_uploading.html

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

$this->upload->data() doesn't work properly

I'm a beginner in CodeIgniter. I'm trying to get the properties for one file that I've uploaded, like name, type, etc. But I can't retrieve that information. It's outputing an empty array. Where I'm doing wrong? I tried:
function do_upload() {
$config['upload_path'] = './uploads/';
$config['allowed_types'] = 'gif|jpg|png|pdf';
$config['max_size'] = '999999';
$config['max_width'] = '3000';
$config['max_height'] = '2000';
$this->load->library('upload', $config);
$details = $this->upload->data();
echo "<pre>";
print_r ($details);
echo "</pre>";
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 output:
Array
(
[file_name] =>
[file_type] =>
[file_path] => ./uploads/
[full_path] => ./uploads/
[raw_name] =>
[orig_name] =>
[client_name] =>
[file_ext] =>
[file_size] =>
[is_image] =>
[image_width] =>
[image_height] =>
[image_type] =>
[image_size_str] =>
)
$this->upload->data() will return the uploaded data after executing the $this->upload->do_upload(). So try in your else part.
For more details, refer CI user Guide

Resources