i have problem when upload images in codeigniter, So i have controller that upload images like this :-
public function index()
{
$this->load->model('blog');
$type = "text";
if (isset($_POST['post'])) {
if (isset($_POST['type']) && $_POST['type'] == "image") {
$type = "image";
}
if (strlen($_FILES['inputUpProfile']['name']) > 0) {
$config['upload_path'] = './uploads/';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = '2048';
$config['encrypt_name'] = true;
$this->load->library('upload', $config);
if (!$this->upload->do_upload('inputUpProfile')) {
$error = $this->upload->display_errors();
if (is_array($error)) {
foreach ($error as $er) {
$this->errors[] = $er;
}
} else {
$this->errors[] = $error;
}
} else {
$updata = $this->upload->data();
$imagePath = './uploads/' . $eventpic;
if (file_exists($imagePath)) {
#unlink($imagePath);
}
$eventpic = $updata['raw_name'] . $updata['file_ext'];
}
}
$result = $this->blog->addPost($_SESSION['user_id'], $type, $this->input->post('post'), $eventpic);
}
$result = $this->blog->getPosts($_SESSION['user_id'], 0, 10);
$this->template->build("home_view", array("response" => $result));
}
and view is like this :-
<div class="textstatus">
<input id="inputUpProfile" name="inputUpProfile"
class="inputUpProfile hidefile" type="file"/>
<input type="button" id="PicUpProfile" class="sentpic" value="addpic">
<input name="post" type="text" id="text" placeholder="message ...">
<input type="submit" id="sent" value="Send">
</div>
</form>
</div>
when i upload thee images in my site the error is display like :-
Severity: Notice
Message: Undefined index: inputUpProfile
Filename: controllers/home.php
Line Number: 47
and
A PHP Error was encountered
Severity: Notice
Message: Undefined variable: eventpic
Filename: controllers/home.php
Line Number: 74
and
A PHP Error was encountered
Severity: Warning
Message: Cannot modify header information - headers already sent by (output started at D:\AppServ\www\sys\system\core\Exceptions.php:185)
Filename: core/Common.php
Line Number: 442
so where are the problem in my code.
/****************
edit :-
Array ( [upload_data] => Array ( [file_name] => 67429_133961013479569_306349156_n3.jpg [file_type] => image/jpeg [file_path] => D:/AppServ/www/d5n/rashaqa2/uploads/ [full_path] => D:/AppServ/www/d5n/rashaqa2/uploads/67429_133961013479569_306349156_n3.jpg [raw_name] => 67429_133961013479569_306349156_n3 [orig_name] => 67429_133961013479569_306349156_n.jpg [client_name] => 67429_133961013479569_306349156_n.jpg [file_ext] => .jpg [file_size] => 34.05 [is_image] => 1 [image_width] => 720 [image_height] => 540 [image_type] => jpeg [image_size_str] => width="720" height="540" ) )
i need from this array [file_name] to save in DB, how can i read this.
if($_FILES['nameofinputtype']['error'] == UPLOAD_ERR_OK)
{
// A file was uploaded
$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);
$imagen = $this->upload->do_upload();
}
Related
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');
}
}
I'm using codeIgniter and uploading image path along with form data in database. The problem that i'm facing is they're not displaying in table. Images are successfully uploaded in the folder and the path is also saved in db but it's not displaying the images.
Code that i've written is below.
In view file:
<?php echo form_open_multipart('welcome/insertion'); ?>
<input type="file" name="userfile" id="userfile" size="40" maxlength="90" tabindex="3"/>
<input name="saveForm" type="submit" value="Insert Record" class="iconSave" />
In controller i write:
public function insertion()
{
$this->load->model('data_maintenance','',TRUE);
$this->data_maintenance->insertion();
}
And finally in model:
public function insertion()
{
$config['upload_path'] = 'application/uploads';
$config['allowed_types'] = 'gif|jpg|jpeg|png|pdf';
$config['max_size'] = '5000';
$this->load->library('upload', $config);
$this->upload->initialize($config);
if ( ! $this->upload->do_upload('userfile'))
{
echo $this->upload->display_errors();
}
else
{
//$file_data = $this->upload->data('image');
$file_data = $this->upload->data();
$new_staff = array(
'agenda_id' => $this->input->post('agenda_id'),
't_name' => $this->input->post('t_name'),
'exp' => $this->input->post('exp'),
'leaves_allow_monthly' => $this->input->post('leaves_allow_monthly'),
'leaves_allow_annualy' => $this->input->post('leaves_allow_annualy'),
'gender' => $this->input->post('gender'),
'cell_no' => $this->input->post('cell_no'),
'dob' => $this->input->post('dob'),
'file' => $file_data['file_name']
);
}
$d="/SampleOOP/application/uploads/" . $new_staff['file'];
$this->db->set('image', $d);
//$this->db->insert('teachers_record');
//$d="/SampleOOP/application/uploads/".$new_staff['file'];
$this->db->set('teacher_name', $new_staff['t_name']);
//$this->db->set('image',$d);
$this->db->insert('teachers_record');
}
try print_r($file_data) and see what comes back, you can also try: $file_data['orig_name']
Or:
$file_name = $file_data['file_name'];
$new_staff = array(
'agenda_id' => $this->input->post('agenda_id'),
't_name' => $this->input->post('t_name'),
'exp' => $this->input->post('exp'),
'leaves_allow_monthly' => $this->input->post('leaves_allow_monthly'),
'leaves_allow_annualy' => $this->input->post('leaves_allow_annualy'),
'gender' => $this->input->post('gender'),
'cell_no' => $this->input->post('cell_no'),
'dob' => $this->input->post('dob'),
'file' => $file_name
);
UPDATE, Try:
$config['upload_path'] = 'application/uploads';
$config['allowed_types'] = 'gif|jpg|jpeg|png|pdf';
$config['max_size'] = '5000';
$this->load->library('upload', $config);
Or
$this->load->library('upload');
$config['upload_path'] = 'application/uploads';
$config['allowed_types'] = 'gif|jpg|jpeg|png|pdf';
$config['max_size'] = '5000';
$this->upload->initialize($config);
$this->upload->data() result is
Array
(
[file_name] => 72f59510f9bbf05933c89e4951acc29d
[file_type] =>
[file_path] => ./inst/public/uploads/
[full_path] => ./inst/public/uploads/72f59510f9bbf05933c89e4951acc29d
[raw_name] => 72f59510f9bbf05933c89e4951acc29d
[orig_name] =>
[client_name] =>
[file_ext] =>
[file_size] =>
[is_image] =>
[image_width] =>
[image_height] =>
[image_type] =>
[image_size_str] =>
)
error:
Array
(
[error] => You did not select a file to upload.
)
upload function
function upload(){
if(isset($_POST['userfile']) AND !empty($_POST['userfile']))
{
$Info = $this->login();
if(#$Info)
{
$config['upload_path'] = './inst/public/uploads/';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = '1000';
$config['max_width'] = '230';
$config['max_height'] = '280';
$config['min_width'] = '220';
$config['min_height'] = '270';
$config['remove_spaces'] = TRUE;
$config['overwrite'] = TRUE;
$config['file_name'] = md5(uniqid("100_ID", true));
$this->load->library('upload', $config);
$Setting = $this->Setting;
$this->load->view('header',$Setting);
if ( ! $this->upload->do_upload("userfile"))
{
$response['error'] = array('error' => $this->upload->display_errors());
echo '<pre>';
print_r( $this->upload->data());
$this->load->view('upload_done', $response);
}
else
{
$response['success'] = array('upload_data' => $this->upload->data());
$this->load->view('upload_done', $response);
}
}
}
}
form code
<?php
echo form_open('/Home/upload');
?>
<br><div class="form-group"><input class ='form-control' placeholder="<?php echo lang('fileu'); ?>" type="file" name="userfile" size="20" /></div>
<div class="alert alert-info"><?php echo lang('filetext'); ?></div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal"><?php echo lang('Close'); ?></button>
<button type="submit" class="btn btn-primary"><?php echo lang('uploadsub'); ?></button>
<?php echo form_close(); ?>
you have to use below code to upload files. You are missing multipart attribute in your form.
echo form_open_multipart('/Home/upload');
First change your helper function to form_open_multipart(). If you still get the error after changing to the correct function, it could be your maxsize property as well .If an uploaded file is larger than the allowable size, the FILES variable will be empty.
Try changing...
$config['max_size'] = '1000';
...to something like...
$config['max_size'] = '30000';
php.net File Upload
What I am trying to do is after uploading the file parse the cvs file to the screen with print_r so I can do some things with it. I cannot seem to figure out how you access the file name with the $data. I have tried several things like $file_name, $data['file_name], and $this->data->$file_name. The relevant part is on line 37.
Not sure what I am doing wrong. I have looked at the code igniter documentation for the uploader library but it doesn't be sufficient to answer my question. Thanks for any help!
<?php
class Upload extends CI_Controller {
function __construct()
{
parent::__construct();
$this->load->helper(array('form', 'url'));
}
function index()
{
$this->load->view('upload_form', array('error' => ' ' ));
}
function do_upload()
{
$config['upload_path'] = './uploads/';
$config['allowed_types'] = 'csv';
$config['max_size'] = '100';
$config['max_width'] = '1024';
$config['max_height'] = '768';
$this->load->library('upload', $config);
$this->load->library('getcsv');
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);
echo 'The file name is: '.$file_name;
}
}
}
?>
The filename can be anything you want. This is clearly in the documentation, you might've been reading the wrong page or something:
http://ellislab.com/codeigniter%20/user-guide/libraries/file_uploading.html
file_name: If set CodeIgniter will rename the uploaded file to this name. The extension provided in the file name must also be an allowed file type.
OR:
You can get it from $this->upload->data(), which is an array.
$this->upload->data()
This is a helper function that returns an array containing all of the data related to the file you uploaded. Here is the array prototype:
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"
)
Use the following code :
if($this->upload->do_upload($file)){
//Get uploaded file data here
$allData=$this->upload->data();
$myFile = $allData['file_name'];
}
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];