I'd like some help please. I have created a form with CodeIgniter where I can write posts and what I 'd like to do is to add the functionality to upload image(s) for this post.
At the beginning I thought to add an input field in my form where I can upload files, so I can display them above the content (text) at the front end, but although this is a very quick and easy solution, may be not so flexible. So I thought to use the 'insert/edit image' button that tinymce has.
I haven’t done this before so how can I upload an image or images through tinymce so that they appear within the text at the front-end??
EDIT Here is my code.
This is my view:
<?php echo form_open_multipart(); ?>
<div>
<label for="title">Title *</label>
<?php echo form_input('title', html_escape(set_value('title', $article->title))); ?>
</div>
<div>
<label for="file">Upload image</label>
<?php echo form_upload('file'); ?>
</div>
<div>
<label for="body">Body *</label> // has TinyMce
<?php echo form_textarea('body', strip(set_value('body', $article->body))); ?>
</div>
<div>
<?php echo form_submit('save', 'Save'); ?>
</div>
<?php echo form_close(); ?>
This is my model:
public function save($id = null){
$post_data = array(
'title' => $this->input->post('title'),
'file' => $this->input->post('file'),
'body' => $this->input->post('body'),
);
return parent::save($post_data, $id);
}
This is the controller:
public function article($id = null){
....
$this->form_validation->set_rules($this->article_model->rules);
// Process the form
if ($this->form_validation->run() == TRUE) {
$this->article_model->save($id);
redirect('admin/article');
}
}
There are two approaches to this.
You either keep an upload field outside TinyMCE and use ajax/iframe file upload.
OR
Write a TinyMCE plugin with an ability to upload an image.
Please note that both of these approaches take some effort to implement.
As far as handling the file on the server side is concerned, you can go through the Codeigniter Docs to get you started.
In my opinion , I think that you should use separate field to upload images to server as this helps you control them , resize , determine file name and upload path
you can use form_open_multipart() Codeigniter function to upload files
then you can make a DB table field called image and insert the uploaded file name to it
then retrieving the post you can
<img src="<?php echo $post->image; ?>" />
<p><?php echo $post->content ?></p>