Reputation: 1535
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
Upvotes: 0
Views: 900
Reputation: 1535
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');
}
Upvotes: 0
Reputation:
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
Upvotes: 2
Reputation: 3856
$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"
)
Upvotes: 0