codeigniter form - php

i was tired after 3 day searching.. can somebody tell me or show me an example how to upload text with image?
my table:
name -> varchar
lastname -> varchar
position -> varchar
pass_id -> int
image_url -> varchar
this is a form_view file
<h2>Create</h2>
<?php echo form_open('main/add'); ?>
<p>
<label for="name">Name:</label>
<input type="text" name="name" id="name" />
</p>
<p>
<label for="lastname">Lastname:</label>
<input type="text" name="lastname" id="lastname" />
</p>
<p>
<label for="position">Position:</label>
<input type="text" name="position" id="position" />
</p>
<p>
<label for="name">Passport ID:</label>
<input type="text" name="pass_id" id="pass_id" />
</p>
<input type="file" name="userfile" size="20" />
<input type="submit" value="Submit" />
<?php echo form_close(); ?>
this is a controller file:
public function add()
{
$subject = array(
'name' => $this->input->post('name'),
'lastname' => $this->input->post('lastname'),
'position' => $this->input->post('position'),
'pass_id' => $this->input->post('pass_id')
);
$this->main_model->add($subject);
$this->index();
}
and a model file:
public function add($object)
{
$this->db->insert('stuf', $object);
return;
}
how to upload image url in the db.
i'm newbie in CI. thanks

use form_open_multipart("main/add") instead of form_open("main/add").
And modify your controller a bit:
$this->load->library('upload'); // See File Uploading in Code Igniter Documentation
$this->load->helper(array('form', 'url')); // See form, and url helper
$my_path = "your_uploads_folder"
$config['upload_path'] = './'. $my_path .'/';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = '100';
$config['max_width'] = '1024';
$config['max_height'] = '768';
$this->upload->do_upload('userfile', $config);
$subject = array(
'url' => $url . '/' . $my_path // Dont forget to create the table field
'name' => $this->input->post('name'),
'lastname' => $this->input->post('lastname'),
'position' => $this->input->post('position'),
'pass_id' => $this->input->post('pass_id'),
);
$this->main_model->add($subject);
redirect('/main/index/', 'refresh'); // Use redirect instead
Note: It has not been tested yet

$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"
)
Source

ok this is a solution :)
public function new_record()
{
$config['upload_path'] = './uploads/';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = '100';
$config['max_width'] = '1024';
$config['max_height'] = '768';
$this->upload->initialize($config);
if (!$this->upload->do_upload('userfile'))
{
echo $this->upload->display_errors();
}
else
{
$data = $this->upload->data();
$full_path = 'uploads/' . $data['file_name'];
$subject = array(
'name' => $this->input->post('name'),
'lastname' => $this->input->post('lastname'),
'position' => $this->input->post('position'),
'pass_id' => $this->input->post('pass_id'),
'image_url' => $full_path
);
$this->main_model->add($subject);
redirect('/main/index/', 'refresh');
}

Related

File uploaded successfully but not showing in upload_path folder

I successfully uploaded the file based on the "$this->upload->data()" message. However, the file doesn't show up in D:/Drive/webcode/uploads folder. Can anyone help out? Thank you.
***move_uploaded_file() works, but not do_upload
[Codeigniter 3]
View(upload_form.php)
<form method="post" enctype="multipart/form-data">
<input type="file" name="userfile">
<input type="submit" name="submit">
</form>
Controller(Upload)
function __construct()
{
parent::__construct();
$this->load->helper('form');
$this->load->helper('url');
}
public function jobapply()
{
//Show view form
$this->load->view('upload_form');
if($this->input->post('submit')){ //Check submit button
//Confirm file is transfered here
echo basename($_FILES["userfile"]["name"]).'<br>';
//Upload Process
$config['upload_path'] = './upload/';
$this->load->library('upload', $config);
$this->upload->do_upload('userfile');
var_dump(is_dir($config['upload_path'])); //Check if this path exist
//return the uploaded data after executing do_upload()
echo "<br><pre>";
print_r($this->upload->data());
echo "</pre>";
}
}
Output
1969.png
bool(true)
Array
(
[file_name] => 1969.png
[file_type] => image/png
[file_path] => D:/Drive/webcode/uploads/
[full_path] => D:/Drive/webcode/uploads/1969.png
[raw_name] => 1969
[orig_name] =>
[client_name] => 1969.png
[file_ext] => .png
[file_size] => 1217976
[is_image] => 1
[image_width] =>
[image_height] =>
[image_type] =>
[image_size_str] =>
)
Image: Folder is empty
Add $config['allowed_types'] = '*'; and check.
Solved without using CI upload
Just use below in Controller:
$path = './upload/';
$temp = $_FILES['userfile']['tmp_name'];
move_uploaded_file($temp, $path . $name);
echo 'Success';
Thats it, I wouldn't bother if using CI Upload library or not, as long as it works. Cheers.

How to send multiple file name to database and move file to folder? Codeigniter

I want to upload multiple images. But I don't know what I send data to database and how to move files to folder.
This is my view :
<div class="form-group">
<div class="custom-file">
<input type="file" class="form-control custom-file-input" name="uploadFile[]" id="uploadFile" multiple>
</div>
</div>
And this is my controller :
public function add_event() {
// Our calendar data
$judul = $this->input->post('judul', TRUE);
$waktu_mulai = $this->input->post('mulai', TRUE);
$waktu_berakhir = $this->input->post('berakhir', TRUE);
$lokasi = $this->input->post('lokasi', TRUE);
$scope = $this->input->post('scope', TRUE);
$kategori = $this->input->post('kategori', TRUE);
$satuan_kerja = $this->input->post('satuan_kerja', TRUE);
$keterangan = $this->input->post('keterangan', TRUE);
$agenda_pimpinan = $this->input->post('agenda_pimpinan', TRUE);
// var_dump (count($upload_file)); die;
for($i = 0; $i < count($upload_file); $i++) {
$config['upload_path'] = './uploads/';
$config['allowed_types'] = 'jpg|jpeg|png';
$config['max_size'] = '500';
$this->load->library('upload', $config);
$this->KalenderModel->addEvent(array(
'judul' => $judul,
'mulai' => $waktu_mulai,
'berakhir' => $waktu_berakhir,
'lokasi' => $lokasi,
'scope' => $scope,
'satuan_kerja' => $satuan_kerja,
'keterangan' => $keterangan,
'kategori' => $kategori,
'lampiran' => $uploadfile,
'tampilkan_agenda_pimpinan' => $agenda_pimpinan
));
}
redirect('agendakerja/kalender');
} //end function
Can somebody help?
To get all infos you need about files, you can use
$_FILES;
Here is how you can use it.
Then to move your file, there is the function :
bool move_uploaded_file ( string $filename , string $destination );
Here is more infos.
if you want to upload the file in particular folder then use
$this->upload->do_upload($upload_file[0])
and define the path in $config
$config['upload_path'] = './uploads/';
$config['allowed_types'] = 'jpg|jpeg|png';
$config['max_size'] = '500';
$this->load->library('upload', $config);
$this->upload->initialize($config);
for($i = 0; $i < count($upload_file); $i++) {
if($this->upload->do_upload('bot_logo'))
{
$this->KalenderModel->addEvent(array(
'judul' => $judul,
'mulai' => $waktu_mulai,
'berakhir' => $waktu_berakhir,
'lokasi' => $lokasi,
'scope' => $scope,
'satuan_kerja' => $satuan_kerja,
'keterangan' => $keterangan,
'kategori' => $kategori,
'lampiran' => $uploadfile,
'tampilkan_agenda_pimpinan' => $agenda_pimpinan
));
}
else
{
echo this->upload->display_errors();
}
}

File name not grabbed when using CodeIgniter upload library

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

CodeIgniter And Image Display in Table

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

You did not select a file to upload CodeIgniter

$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

Categories