ci csv file upload to database but page not found - php

the view, as an action after choosing and submitting the file it calls the csv/importcsv.
<form method="post" action="<?php echo base_url() ?>csv/importcsv" enctype="multipart/form-data">
<input type="file" name="userfile" ><br><br>
<input type="submit" name="submit" value="UPLOAD" class="btn btn-primary">
</form>
this is where i think the error happens, and i have tried changing the path of the file time and time again but still does not work, sorry new to ci, i'm just following some tutorial sites and trying to understand their codes.
the model
`
class Csv_model extends CI_Model {
function __construct() {
parent::__construct();
}
function get_sampledb() {
$query = $this->db->get('sampledb');
if ($query->num_rows() > 0) {
return $query->result_array();
} else {
return FALSE;
}
}
function insert_csv($data) {
$this->db->insert('sampledb', $data);
}
}
?>`
the controller
`
class Csv extends CI_Controller {
function __construct() {
parent::__construct();
$this->load->model('csv_model');
$this->load->library('csvimport');
}
function index() {
$data['sampledb'] = $this->csv_model->get_sampledb();
$this->load->view('csvindex', $data);
}
function importcsv() {
$data['sampledb'] = $this->csv_model->get_sampledb();
$data['error'] = ''; //initialize image upload error array to empty
$config['upload_path'] = './uploads/';
$config['allowed_types'] = 'csv';
$config['max_size'] = '1000';
$this->load->library('upload', $config);
// If upload failed, display error
if (!$this->upload->do_upload()) {
$data['error'] = $this->upload->display_errors();
$this->load->view('csvindex', $data);
} else {
$file_data = $this->upload->data();
$file_path = './uploads/'.$file_data['file_name'];
if ($this->csvimport->get_array($file_path)) {
$csv_array = $this->csvimport->get_array($file_path);
foreach ($csv_array as $row) {
$insert_data = array(
'first_name'=>$row['first_name'],
'last_name'=>$row['last_name'],
'item_name'=>$row['item_name'],
'item_price'=>$row['item_price'],
'item_quantity'=>$row['item_quantity'],
'email_ad'=>$row['email_ad'],
'phone_num'=>$row['phone_num'],
'shipping_cost'=>$row['cost'],
);
$this->csv_model->insert_csv($insert_data);
}
$this->session->set_flashdata('success', 'Csv Data Imported Succesfully');
redirect(base_url().'csv');
//echo "<pre>"; print_r($insert_data);
} else
$data['error'] = "Error occured";
$this->load->view('csvindex', $data);
}
}
}
/END OF FILE/
?>`
as for the library i downloaded something from github, i copied and pasted it at application/libraries/csv/csvimport.php as said in the tutorial, i dont know whats happening. my main concern is in the view file, its returning a page not found after uploading,is my path wrong?when looking at firebug it can parse the data of the csv file but cannot save it due to the page not found, is my path wrong?thanks

Related

Codeigniter video upload is not working in live server

I am using following code to upload video in my codeigniter project.
This is my view code
<?php echo $error;?>
<?php echo form_open_multipart('upload/do_upload');?>
<input type="file" name="userfile" size="20" />
<input type="submit" value="upload" />
</form>
This is controller
class Upload extends CI_Controller {
public function __construct()
{
parent::__construct();
$this->load->helper(array('form', 'url'));
}
public function do_upload()
{
$config['upload_path'] = './uploads/';
$config['allowed_types'] = 'gif|jpg|png|mp4';
$config['max_size'] = 100000;
$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());
var_dump($error);
}
else
$data = array('upload_data' => $this->upload->data());
}
}
This code working fine through xampp. But I'm facing error in live server/cpanl. When I try to upload video every time it shows following error,
array(1) {
["error"]=> string(43)
"You did not select a file to upload." }
Looks like uploads or functions might be disabled inside php.ini
trying running your code with error_reporting(E_ALL); to see if some
error occured except for You did not select a file to upload. error

Pass the value from a Controller to the view

I created a form and passed the values for name and picture from the form. The value is accessed from the Upload controller as follows:
$data = array(
'title' => $this->input->post('title', true),
'name' => $this->input->post('name',true),
'picture' => $this->file_upload($_FILES['picture'])
);
return $data;
I need to pass these values to the view so, I modified the above code as:
class Upload extends CI_Controller
{
function __construct() {
parent::__construct();
}
public function input_values(){
$data = array(
'name' => $this->input->post('name',true),
'picture' => $this->file_upload($_FILES['picture'])
);
$this->load->view('documents', $data); }
function add(){
$data = $this->input_values();
if($this->input->post('userSubmit')) {
$this->file_upload(($_FILES['picture']));
if (!empty($_FILES['picture']['name'])) {
$config['upload_path'] = 'uploads/docs/';
$config['allowed_types'] = 'jpg|jpeg|png|gif|pdf|docx';
$config['file_name'] = $_FILES['picture']['name'];
$data['picture']=$this->file_upload($_FILES['picture']);
}
}
return $this->db->insert('files', $data);
}
//logo image upload
public function file_upload($file)
{
$this->my_upload->upload($file);
if ($this->my_upload->uploaded == true) {
$this->my_upload->file_new_name_body = 'file_' . uniqid();
$this->my_upload->process('./uploads/docs/');
$image_path = "uploads/docs/" . $this->my_upload->file_dst_name;
return $image_path;
} else {
return null;
}
}
}
But I am able to get only the value of title. Following error occurs for both name and title:
Message: Undefined variable: name
I have accessed the variables from the view as follows:
<?php var_dump($title)?>
<?php var_dump($name)?
<?php var_dump($picture)?>
so, this part is where you get the post data and load view (contain the upload form)
public function input_values() {
$data = array(
'name' => $this->input->post('name',true),
'picture' => $this->file_upload($_FILES['picture'])
);
$this->load->view('documents', $data);
}
then this part is handle the post request from the upload form:
function add() {
$data = $this->input_values();
if($this->input->post('userSubmit')) {
$this->file_upload(($_FILES['picture']));
if (!empty($_FILES['picture']['name'])) {
$config['upload_path'] = 'uploads/docs/';
$config['allowed_types'] = 'jpg|jpeg|png|gif|pdf|docx';
$config['file_name'] = $_FILES['picture']['name'];
$data['picture']=$this->file_upload($_FILES['picture']);
}
}
return $this->db->insert('files', $data);
}
and this part is where you upload the file
public function file_upload($file)
{
$this->my_upload->upload($file);
if ($this->my_upload->uploaded == true) {
$this->my_upload->file_new_name_body = 'file_' . uniqid();
$this->my_upload->process('./uploads/docs/');
$image_path = "uploads/docs/" . $this->my_upload->file_dst_name;
return $image_path;
} else {
return null;
}
}
when you call add() function, it call input_values() function then load views then the next line of codes won't be executed (cmiiw).
so, maybe you want to change with this :
public function index() {
if ($this->input->post()) {
// then handle the post data and files tobe upload here
// save the post data to $data, so you will able to display them in view
} else {
// set the default data for the form
// or just an empty array()
$data = array();
}
// if the request was not a post, render view that contain form to upload file
$this->load->view('nameOfTheView', $data);
}

Codeigniter upload file name

Usually we can get the form data in Codeigniter by using $this->input->get('field_name') or $this->input->post('field_name') and that's fine.
In raw PHP we use $_FILES["fileToUpload"]["name"] to get the file name that the user trying to upload.
My question is: Is there any Codeigniter way to get the name of the file that needs to be uploaded?
I am trying to say that i need to get the file name that the user is trying to upload before trying to save it in my server using Codeigniter library instead of using raw PHP global $_FILES variable.
<?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';
$config['max_size'] = 100;
$config['max_width'] = 1024;
$config['max_height'] = 768;
$this->load->library('upload', $config);
// get the user submitted file name here
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);
}
}
}
?>
$upload_data = $this->upload->data();
$file_name = $upload_data['file_name'];
Here is the 2 version doc is for 2 and for 3
if you want to get the file name in backend:
$this->upload->file_name It will work based on system/library/upload.php
this function.
public function data()
{
return array (
'file_name' => $this->file_name,
'file_type' => $this->file_type,
...
);
}
If you need to get file name...
Before saving to server... you have work in javascript
<?php echo "<input type='file' name='userfile' size='20' onchange='changeEventHandler(event);' />"; ?>
onchange event in javascript:
<script>
function changeEventHandler(event){
alert(event.target.value);
}
</script>
$data = array('upload_data' => $this->upload->data());
// use file_name within the data() the final code will be
$data = array('upload_data' => $this->upload->data('file_name'));

how to use custom function for uploading files in Codeigniter

I made a custom function before working with framework, that I want to use again now. the problem occurs when I tried uploading image with my custom function. it says error undefined index : [the field_name].
most people uses CI library and CI upload function do_upload() but I want to use my own function because it also creates smaller image to be used as thumb.
I started working with CI 3 days ago, and still don't know how to change anything that can make $_FILES[] working.
the view :
<form action="path/to/controller/method" method="post" enctype="multipart/form-data">
<input type="file" name="fupload">
</form>
the controller :
public function __construct(){
parent::__construct();
$this->load->helper('fungsi_thumb'); // this is the custom function
$config['allowed_types'] = '*';
$this->load->library('upload',$config);
}
public function input_file(){
$data = array(
'location_file' => $_FILES['fupload']['tmp_name'],
'type_file' => $_FILES['fupload']['type'],
'name_file' => $_FILES['fupload']['name']
);
$this->load->model('input_model');
$this->input_model->put_file($data);
}
I already put my custom function file in application\helpers\. Should I show the custom function file and the model file too?
I already change the public $allowed_types = '*'; too
UPDATE
the Model
public function put_file($data){
//BUAT FILE
$lokasi_file = $data['location_file'];
$tipe_file = $data['type_file'];
$nama_file = $data['name_file'];
$acak = rand(1,99);
$nama_file_unik = $acak.$nama_file;
UploadImage($nama_file_unik); // this is my custom function
$sql="INSERT INTO produk(gambar)VALUES (?)";
$query=$this->db->query($sql,array($nama_file_unik));
if($query)
{
echo "BERHASIL";
}
else
{
echo "GAGAL";
}
}
UPDATE NEW
I finally able to use $_FILES, I need to load the library inside the controller Constructor.
now the new problem is there is no file uploaded even using do_upload() function inside my own custom made function
this is my custom made function
function UploadImage($fupload_name){
// SET DATA FILE NYA
$config['file_name'] = $fupload_name;
$config['upload_path'] = 'http://localhost/mobileapp/assets/gambar/';
var_dump($config['upload_path']);
//load the upload library
$CI =& get_instance();
$CI->load->library('upload',$config);
//Upload the file
if( !($CI->upload->do_upload('fupload'))){
$error = $CI->upload->display_errors();
}else{
$file_data = $CI->upload->data();
}
}
now as you can see above, I'm trying to change the file_name to a new randomly-generated name (done in the model) using $config['file_name'] = $fupload_name; and making a new object because obviously I need to do this to load library and use the do_upload() method.
but I still cannot use it. now I'm stuck again
Pass $_FILES array from controller to model function, add new file name in config array and use do_upload() directly like,
Controller Function:
public function input_file(){
$this->load->model('input_model');
$this->input_model->put_file($_FILES); // pass $_FILES Array
}
Model Function:
public function put_file($files){
$config['allowed_types'] = '*';
$acak = rand(1,99);
$config['file_name'] = $acak.$files['fupload']['name'];
$this->load->library('upload',$config);
$data = array(
'location_file' => $files['fupload']['tmp_name'],
'type_file' => $files['fupload']['type'],
'name_file' => $files['fupload']['name']
);
if (!$this->upload->do_upload('fupload')) { // pass field name here
$this->upload->display_errors('<p>', '</p>');
} else {
$sql="INSERT INTO produk(gambar)VALUES (?)";
$query=$this->db->query($sql,array($nama_file_unik));
if($query) {
echo "BERHASIL";
} else {
echo "GAGAL";
}
}
}
upload_path in configs must be absolute or relative path and not an url.
So you can something like this():
$config['upload_path'] = './uploads/';
OR this:
$config['upload_path'] = FCPATH . 'uploads/';
Note: FCPATH is absolute path of your index.php folder.

How to Display uploaded files (PDF, DOC etc) as downloadable link in CodeIgniter

I am relatively new to CodeIgniter but I can grasp a thing or two.
I am currenly facing a problem where I want the admin to view or download uploaded files (pdf and doc) submitted upon user registration.
Everything is working well but I would wish the admin to download the files not just see the filename.
Here is my Controller:
<?php
class NominationController extends CI_Controller {
public function __construct() {
parent::__construct();
$this->load->model('Nomination');
$this->load->library('session');
}
/*
function for manage Nomination.
return all Nominations.
created by your name
created at 25-07-16.
*/
public function manageNomination() {
$data["Nominations"] = $this->Nomination->getAll();
$this->load->view('Nomination/manage-Nomination', $data);
}
/*
function for add Nomination get
created by your name
created at 25-07-16.
*/
public function addNomination() {
$this->load->view('Nomination/add-Nomination');
}
/*
function for add Nomination post
created by your name
created at 25-07-16.
*/
public function addNominationPost() {
$data['title'] = $this->input->post('title');
$data['first_name'] = $this->input->post('first_name');
$data['last_name'] = $this->input->post('last_name');
$data['email_id'] = $this->input->post('email_id');
$data['phone_no'] = $this->input->post('phone_no');
$data['company'] = $this->input->post('company');
$data['popularity'] = $this->input->post('popularity');
$data['nominee_type'] = $this->input->post('nominee_type');
$data['innovation_name'] = $this->input->post('innovation_name');
$data['organisation_type'] = $this->input->post('organisation_type');
$data['category'] = $this->input->post('category');
$data['date_of_innovation'] = $this->input->post('date_of_innovation');
$data['company_offices'] = $this->input->post('company_offices');
$data['need_addressed'] = $this->input->post('need_addressed');
$data['user_benefits'] = $this->input->post('user_benefits');
$data['uniqueness'] = $this->input->post('uniqueness');
if ($_FILES['supporting_documents']['name']) {
$data['supporting_documents'] = $this->doUpload('supporting_documents');
}
$this->Nomination->insert($data);
$this->session->set_flashdata('success', 'Nomination added Successfully');
redirect('success');
}
/*
function for edit Nomination get
returns Nomination by id.
created by your name
created at 25-07-16.
*/
public function editNomination($Nomination_id) {
$data['Nomination_id'] = $Nomination_id;
$data['Nomination'] = $this->Nomination->getDataById($Nomination_id);
$this->load->view('Nomination/edit-Nomination', $data);
}
/*
function for edit Nomination post
created by your name
created at 25-07-16.
*/
public function editNominationPost() {
$Nomination_id = $this->input->post('Nomination_id');
$Nomination = $this->Nomination->getDataById($Nomination_id);
$data['title'] = $this->input->post('title');
$data['first_name'] = $this->input->post('first_name');
$data['last_name'] = $this->input->post('last_name');
$data['email_id'] = $this->input->post('email_id');
$data['phone_no'] = $this->input->post('phone_no');
$data['company'] = $this->input->post('company');
$data['popularity'] = $this->input->post('popularity');
$data['nominee_type'] = $this->input->post('nominee_type');
$data['innovation_name'] = $this->input->post('innovation_name');
$data['organisation_type'] = $this->input->post('organisation_type');
$data['category'] = $this->input->post('category');
$data['date_of_innovation'] = $this->input->post('date_of_innovation');
$data['company_offices'] = $this->input->post('company_offices');
$data['need_addressed'] = $this->input->post('need_addressed');
$data['user_benefits'] = $this->input->post('user_benefits');
$data['uniqueness'] = $this->input->post('uniqueness');
if ($_FILES['supporting_documents']['name']) {
$data['supporting_documents'] = $this->doUpload('supporting_documents');
unlink('./uploads/Nomination/'.$Nomination[0]->supporting_documents);
}
$edit = $this->Nomination->update($Nomination_id,$data);
if ($edit) {
$this->session->set_flashdata('success', 'Nomination Updated');
redirect('success');
}
}
/*
function for view Nomination get
created by your name
created at 25-07-16.
*/
public function viewNomination($Nomination_id) {
$data['Nomination_id'] = $Nomination_id;
$data['Nomination'] = $this->Nomination->getDataById($Nomination_id);
$this->load->view('Nomination/view-Nomination', $data);
}
/*
function for delete Nomination created by your name
created at 25-07-16.
*/
public function deleteNomination($Nomination_id) {
$delete = $this->Nomination->delete($Nomination_id);
$this->session->set_flashdata('success', 'Nomination deleted');
redirect('manage-Nomination');
}
/*
function for activation and deactivation of Nomination.
created by your name
created at 25-07-16.
*/
public function changeStatusNomination($Nomination_id) {
$edit = $this->Nomination->changeStatus($Nomination_id);
$this->session->set_flashdata('success', 'Nomination '.$edit.' Successfully');
redirect('manage-Nomination');
}
/*
function for upload files
return uploaded file name.
created by your name
created at 25-07-16.
*/
public function doUpload($file) {
$config['upload_path'] = './uploads/Nomination';
$config['allowed_types'] = '*';
$this->load->library('upload', $config);
if ( ! $this->upload->do_upload($file))
{
$error = array('error' => $this->upload->display_errors());
$this->load->view('upload_form', $error);
}
else
{
$data = array('upload_data' => $this->upload->data());
return $data['upload_data']['file_name'];
}
}
}
Here is my View.php
<tr>
<td width='30%' class='label-view text-right'>
<?php echo SiteHelpers::activeLang('Uniqueness', (isset($fields['uniqueness']['language'])? $fields['uniqueness']['language'] : array())) ;?>
</td>
<td><?php echo $row['uniqueness'] ;?> </td>
</tr>
<tr>
<td width='30%' class='label-view text-right'>
<?php echo SiteHelpers::activeLang('Supporting Documents', (isset($fields['supporting_documents']['language'])? $fields['supporting_documents']['language'] : array())) ;?>
</td>
<td><?php echo $row['supporting_documents'] ;?></td>
</tr>
Here is the screenshot of my output:
I would like the pdf file or doc to be clickable
Here is how I have set my MySQL:
..just in case its not set correctly
I feel its something small but my eyeballs are sore for the past week and I think Google knows my name and where I sleep by now :)
Any help accorded will go a long away.
Thank you
According to the documentation:
https://www.codeigniter.com/user_guide/helpers/url_helper.html#base_url
You can import the Url Helper like this:
$this->load->helper('url');
Then, you can use
Download
The method base_url will return your site base URL, as specified in your config file. Plus, the argument will concatenate that URL with the string you supplied. That will be the complete URL of your file.

Categories