Here is my controller insertion code
This code inserts image into image path folder but the path is not saving in database.
function add_hotel() {
//validate form input
$this->form_validation->set_rules('hotelname', 'Hotel Name', 'required|xss_clean');
$this->form_validation->set_rules('hotellocation', 'Hotel Location', 'required|xss_clean');
$this->form_validation->set_rules('hotelphone', 'Hotel Phone', 'required|xss_clean');
$this->form_validation->set_rules('hotelimg', 'Hotel Image ', 'callback__image_upload');
$this->form_validation->set_rules('hotelabout', 'Hotel About', 'required|xss_clean');
if ($this->form_validation->run() == true)
{
$config['upload_path'] = './images/';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = '1000000';
$config['overwrite'] = TRUE;
$config['remove_spaces'] = TRUE;
$config['encrypt_name'] = FALSE;
$this->load->library('upload', $config);
$field_name = "hotelimg";
if ( ! $this->upload->do_upload($field_name))
{
$error = array('error' => $this->upload->display_errors());
$this->load->view('admin/add_hotel', $error);
}
else {
$data = array(
'hotelname' => $this->input->post('hotelname'),
'hotellocation' => $this->input->post('hotellocation'),
'hotelphone' => $this->input->post('hotelphone'),
'hotelimg' => $this->upload->data('hotelimg'),
'hotelabout' => $this->input->post('hotelabout')
);
print_r($data);
$this->db->insert('hotel_content', $data);
$this->session->set_flashdata('message', "<p>Hotel added successfully.</p>");
redirect(base_url().'index.php/admin/hotel_controller/index');
}
Error shown is:
A PHP Error was encountered
Severity: Notice
Message: Array to string conversion
Filename: mysql/mysql_driver.php
Line Number: 552
and
A Database Error Occurred:
Error Number: 1054
Unknown column 'Array' in 'field list'
INSERT INTO `hotel_content` (`hotelname`, `hotellocation`, `hotelphone`, `hotelimg`, `hotelabout`) VALUES ('hotel5', 'hyd', '0402365477', Array, 'welcome')
Filename: G:\wamp\www\CodeIgniter\system\database\DB_driver.php
Line Number: 330
I need path to be inserted in database. Can anyone help me?
replace else part with
else {
$data = array(
'hotelname' => $this->input->post('hotelname'),
'hotellocation' => $this->input->post('hotellocation'),
'hotelphone' => $this->input->post('hotelphone'),
'hotelimg' => $this->upload->data('hotelimg'),
'hotelabout' => $this->input->post('hotelabout')
);
print_r($data);
$this->db->insert('hotel_content', $data);
$this->session->set_flashdata('message', "<p>Hotel added successfully.</p>");
redirect(base_url().'index.php/admin/hotel_controller/index');
}
with this
else {
$image_path = $this->upload->data();
$data = array(
'hotelname' => $this->input->post('hotelname'),
'hotellocation' => $this->input->post('hotellocation'),
'hotelphone' => $this->input->post('hotelphone'),
'hotelimg' => $image_path[full_path],
'hotelabout' => $this->input->post('hotelabout')
);
print_r($data);
$this->db->insert('hotel_content', $data);
$this->session->set_flashdata('message', "<p>Hotel added successfully.</p>");
redirect(base_url().'index.php/admin/hotel_controller/index');
}
the line "$this->upload->data('hotelimg')" in else part returns an array.
You just need uploaded path from it which can be extracted as:
$temp = $this->upload->data('hotelimg');
$uploadedPath = $temp[full_path];
Related
I made insert data along upload images into the database , when the run was successful , but when the insert data and upload images contained errors.
The path to the image is not correct.
Your server does not support the GD function required to process this type of image.
how to cope if I insert the data without uploading images is not error ? and the image database went into default image ?
This my controllers
public function save(){
$this->load->library('image_lib');
//$nama_asli = $_FILES['userfile']['name'];
$id = $this->input->post('id',TRUE);
$config['file_name'] = $id ;//'_'.'_'.$nama_asli;
$config['upload_path'] = './uploads/';
$config['allowed_types'] = 'gif|jpg|png|jpeg|bmp';
$config['max_size'] = '100000';
$this->load->library('upload', $config);
$this->upload->initialize($config);
if ( ! $this->upload->do_upload('userfile'))
{
$files = $this->upload->data();
$fileNameResize = $config['upload_path'].$config['file_name'];
$size = array(
array('name' => 'thumb','width' => 100, 'height' => 100, 'quality' => '100%')
);
$resize = array();
foreach($size as $r){
$resize = array(
"width" => $r['width'],
"height" => $r['height'],
"quality" => $r['quality'],
"source_image" => $fileNameResize,
"new_image" => $url.$r['name'].'/'.$config['file_name']
);
$this->image_lib->initialize($resize);
if(!$this->image_lib->resize())
die($this->image_lib->display_errors());
}
}
else
{
$data = array('upload_data' => $this->upload->data());
$get_name = $this->upload->data();
$nama_foto = $get_name['file_name'];
$this->mcrud->savealat($nama_foto);
redirect('instrument/detailalat');
}
}
This my model
function savealat($nama_foto) {
$data = array(
'id' => $this->input->post('id'),
'namaalat' => $this->input->post('namaalat'),
'dayalistrik' => $this->input->post('dayalistrik'),
'merk' => $this->input->post('merk'),
'namasupplier' => $this->input->post('namasupplier'),
'nokatalog' => $this->input->post('nokatalog'),
'noseri' => $this->input->post('noseri'),
'category' => $this->input->post('category'),
'lokasi' => $this->input->post('lokasi'),
'pengguna' => $this->input->post('pengguna'),
'status' => $this->input->post('status'),
'jadwalkal' => $this->input->post('jadwalkal'),
'manual' => $this->input->post('manual'),
'dateinput' => $this->input->post('date'),
'foto' => $nama_foto
//'created' => $tanggal
);
$this->db->insert('tbdetail', $data);
}
public function save(){
$this->load->library('image_lib');
//$nama_asli = $_FILES['userfile']['name'];
$id = $this->input->post('id',TRUE);
$config['file_name'] = $id ;//'_'.'_'.$nama_asli;
$config['upload_path'] = './uploads/';
$config['allowed_types'] = 'gif|jpg|png|jpeg|bmp';
$config['max_size'] = '100000';
$this->load->library('upload', $config);
$this->upload->initialize($config);
if ( ! $this->upload->do_upload('userfile'))
{
$files = $this->upload->data();
$fileNameResize = $config['upload_path'].$config['file_name'];
$size = array(
array('name' => 'thumb','width' => 100, 'height' => 100, 'quality' => '100%')
);
$resize = array();
foreach($size as $r){
$resize = array(
"width" => $r['width'],
"height" => $r['height'],
"quality" => $r['quality'],
"source_image" => $fileNameResize,
"new_image" => base_url().$r['name'].'/'.$config['file_name']
);
$this->image_lib->initialize($resize);
if(!$this->image_lib->resize())
die($this->image_lib->display_errors());
}
$data = array('upload_data' => $this->upload->data());
$get_name = $this->upload->data();
$nama_foto = $get_name['file_name'];
$this->mcrud->savealat($nama_foto);
redirect('instrument/detailalat');
}
else
{
//Moved your code up there
}
}
If I'm right the problem is that you put the upload in else. Try to move the code and tell me if it works
MaY be this answer can help you out. There are few more suggestion in comment part which can figure some way out for you. Your server does not support the GD function required to process this type of image.Ci
Firstly can I request this is NOT marked as duplicate. I have read the other posts on SO regarding issues with the CodeIgniter upload library and sadly they do not cover this. I have also extensively read the CI documentation and everything suggests this should work correctly.
I am using a very simple form to grab the file, which is uploaded correctly to the images folder. The full_path of the file is also written successfully to a db table called images. The filename however is blank.
My form:
<?php echo form_open_multipart('image_upload/do_upload');?>
<input type="file" name="userfile" size="20" multiple="true" />
<br /><br />
<input type="submit" value="upload" />
</form>
My Controller function:
function do_upload()
{
$config['upload_path'] = 'c:/wamp/www/honest/images';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = '5000';
$config['overwrite'] = TRUE;
$config['remove_spaces'] = TRUE;
$this->load->library('upload', $config);
$image_data = $this->upload->data();
echo '<pre>'; print_r($image_data); echo '</pre>';
if ( ! $this->upload->do_upload())
{
$error = array('error' => $this->upload->display_errors());
$data['main_content'] = 'imageupload';
$this->load->view('includes/template', $data);
echo "FAILED";
}
else
{
$data = array(
'filename' => $image_data['file_name'],
'fullpath' => $image_data['full_path']
);
$this->db->insert('images', $data);
$this->load->view('imageupload');
}
}
I am using
echo print_r($image_data); echo;
To display the associated image data but this is all that is returned:
Array
(
[file_name] =>
[file_type] =>
[file_path] => c:/wamp/www/honest/images/
[full_path] => c:/wamp/www/honest/images/
[raw_name] =>
[orig_name] =>
[client_name] =>
[file_ext] =>
[file_size] =>
[is_image] =>
[image_width] =>
[image_height] =>
[image_type] =>
[image_size_str] =>
)
I can't work out why it is not grabbing the file name and other details - can someone help spot what is hopefully a simple error?
Many thanks,
DP.
You are requesting your uploaded data before you actually upload anything. That's why your $image_data only contains your configuration values. Move your $image_data = $this->upload->data(); call to after actually performing the do_upload() (into your else block):
function do_upload()
{
$config['upload_path'] = 'c:/wamp/www/honest/images';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = '5000';
$config['overwrite'] = TRUE;
$config['remove_spaces'] = TRUE;
$this->load->library('upload', $config);
if ( ! $this->upload->do_upload())
{
$error = array('error' => $this->upload->display_errors());
$data['main_content'] = 'imageupload';
$this->load->view('includes/template', $data);
echo "FAILED";
}
else
{
$image_data = $this->upload->data();
$data = array(
'filename' => $image_data['file_name'],
'fullpath' => $image_data['full_path']
);
$this->db->insert('images', $data);
$this->load->view('imageupload');
}
}
My $data array is not getting passed to the view per the code and errors shown below.
public function myAccount(){
$data = array(
'templateVersion' => 'template1',
'headerVersion' => 'header2',
'css' => '',
'navBarVersion' => 'navBar2',
'main_content' => 'myaccount/myAccount',
'page_title' => 'example.com - My Account',
'footerVersion' => 'footer2'
);
if (($this->session->userdata('is_logged_in')) == 1) {
$config['upload_path'] = $this->logo_path;
$config['allowed_types'] = 'gif|jpg|png|jpeg';
$config['encrypt_name'] = TRUE;
$this->load->library('upload', $config);
if ($this->input->post('upload')) {
if(!$this->upload->do_upload('userfile')){
echo $this->upload->display_errors('<p>', '</p>');
} else {
$w = $this->upload->data();
$data = array(
'field_input_name' => $w['file_name'],
);
//$this->db->insert('table_image', $data);
echo "here";
//exit;
}
}
//******************************************
// For some reason, $data is not passed in!
// Have posted the web screen error far below.
$this->load->view('includes/template1', $data);
}else{
redirect('site/restricted');
}
}
////////////////////////////////////////////////////////////////////
A PHP Error was encountered Severity: Notice Message: Undefined
variable: headerVersion Filename: includes/template1.php Line
Number: 4
An Error Was Encountered
Unable to load the requested file: includes/.php
You have initialised $data twice. Once in the beginning of the function and again in else condition of upload.Inside else condition of upload change like this
$data['field_input_name'] = $w['file_name'];
It will work.
$data array's keys are converted into variables
$data['someKey'] = array(
'templateVersion' => 'template1',
'headerVersion' => 'header2',
'css' => '',
'navBarVersion' => 'navBar2',
'main_content' => 'myaccount/myAccount',
'page_title' => 'example.com - My Account',
'footerVersion' => 'footer2'
);
In view you can access as print_r($someKey);
use die(var_dump($someKeys)); in view to see results
As you are trying to upload images in CI you can use
echo $this->image_lib->display_errors(); for some other error detection or debugging
I'm getting following Error:
The filetype you are attempting to upload is not allowed.
i want to upload csv file.
i'm using codeigniter file upload method do_upload
and i also supply allowed_types as csv
public function csvRateList(){
$redirectData=['error'=>'','success'=>''];
$type=$this->input->post('type');
date_default_timezone_set('Asia/Kolkata');
$config['upload_path'] ='./csv/';
$config['allowed_types'] = 'csv'; //type of file
$config['max_size'] = '100';
$this->load->library('upload',$config);
$query = $this->db->get_where('csv_rate_list', array('type' => $type));
if($query->num_rows()==0){
$query = $this->db->get_where('rate_list', array('type' => $type));
if($query->num_rows()==0){
if($this->upload->do_upload()){
$fdata=$this->upload->data();
$newName=$fdata['file_name'];
$origName=$fdata['orig_name'];
$data = array(
'type' => $type ,
'new_name' => $newName ,
'orig_name' => $origName,
'timestamp' =>time()
);
$this->db->insert('csv_rate_list', $data);
}else{
$redirectData['error']=$this->upload->display_errors();
redirect(base_url().'add_rate');
}
$redirectData['success']='Successfully inserted!';
$this->session->set_flashdata($redirectData);
redirect(base_url().'add_rate');
}else{
$redirectData['error']='Service type already exists. in old table';
$this->session->set_flashdata($redirectData);
redirect(base_url().'add_rate');
}
}else{
$record=$query->row_array();
$id=$record['id'];
$old_name=$record['new_name'];
if($this->upload->do_upload()){
$fdata=$this->upload->data();
$newName=$fdata['file_name'];
$origName=$fdata['orig_name'];
$data = array(
'type' => $type ,
'new_name' => $newName ,
'orig_name' => $origName,
'timestamp' =>time()
);
$this->db->where('id', $id);
$this->db->update('csv_rate_list', $data);
unlink('./csv/'.$old_name);
$redirectData['success']='Successfully updated!';
$this->session->set_flashdata($redirectData);
redirect(base_url().'add_rate');
}else{
$redirectData['error']=$this->upload->display_errors();
$this->session->set_flashdata($redirectData);
redirect(base_url().'add_rate');
}
}
}
You have to modify few line in (/system/libraries/Upload.php)
from:
$this->file_type = #mime_content_type($file['tmp_name']);
return;
to this:
$this->file_type = #mime_content_type($file['tmp_name']);
if (strlen($this->file_type) > 0) return;
Check the config/mimes.php have the correct value for "csv"
In $mimes array,find the 'csv' and check the following.
'csv' => array('text/x-comma-separated-values', 'text/comma-separated-values', 'application/octet-stream', 'application/vnd.ms-excel', 'text/x-csv', 'text/csv', 'application/csv', 'application/excel', 'application/vnd.msexcel'),
If not, add this into it.
I am using CodeIgniter file uploading class to upload a image. But if I try and select a pdf instead of image I do not get any errors on form submission.
relevant code
$config = array(
'allowed_types' => 'jpg|jpeg|gif|png',
'upload_path' => $this->article_path.'/magazine',
'max_size' => 2000
);
$this->load->library('upload', $config);
$this->upload->do_upload();
$this->upload->display_errors();
$image_data = $this->upload->data();
It is because you never went to the documentation (here), please do so in order to perform better in CodeIgniter framework.
this should give you heads up (please read comments)
$config = array(
'allowed_types' => 'jpg|jpeg|gif|png',
'upload_path' => $this->article_path.'/magazine',
'max_size' => 2000
);
$this->load->library('upload', $config);
if ( ! $this->upload->do_upload()) //important!
{
// something went really wrong show error page
$error = array('error' => $this->upload->display_errors()); //associate view variable $error with upload errors
$this->load->view('upload_form', $error); //show error page
}
else
{
//all is good we upload
$data = array('upload_data' => $this->upload->data());
$this->load->view('upload_success', $data);
}