Note that there are some explanatory texts on larger screens.

plurals
  1. POCodeigniter: How to upload files path to database?
    primarykey
    data
    text
    <p>I am trying to put the file path to database, I already uploaded the file but I don't know how to get the path of the file to put it in the database?</p> <p>Here is the controller:</p> <pre><code> &lt;?php if ( ! defined('BASEPATH')) exit('No direct script access allowed'); class dogo extends CI_Controller { function __construct() { parent::__construct(); $this-&gt;load-&gt;model('insert_article'); } public function index() { $this-&gt;load-&gt;view('dogo/dashboard.php'); } //add new post to database including uploading image public function new_post() { //used for the dropdown menu (category) $this-&gt;load-&gt;model('dogo_cat'); $data['categores_dropdown'] = $this-&gt;dogo_cat-&gt;get_categories(); //form validation $this-&gt;load-&gt;library('form_validation');//load the validation library $this-&gt;form_validation-&gt;set_rules('title', 'title of the post', 'required'); $this-&gt;form_validation-&gt;set_rules('text', 'text of the post', 'required'); //$this-&gt;form_validation-&gt;set_rules('image', 'image of the post', 'required'); $this-&gt;form_validation-&gt;set_rules('category', 'category of the post', 'required'); //the form validation condition if($this-&gt;form_validation-&gt;run() == FALSE){ //error //$this-&gt;view_data $this-&gt;load-&gt;view('dogo/new_post.php', $data); }else{ //no error in the form $title = $this-&gt;input-&gt;post('title'); $text = $this-&gt;input-&gt;post('text'); $category = $this-&gt;input-&gt;post('category'); $publish = $this-&gt;input-&gt;post('publish'); //$img_nw = $this-&gt;input-&gt;post('img_nw'); //$img_nw = $this-&gt;input-&gt;post('img_nw'); $image_file = $this-&gt;input-&gt;post('image_file'); //uploading $this-&gt;load-&gt;model('upload_new_post'); if($this-&gt;input-&gt;post('upload')){ $this-&gt;upload_new_post-&gt;do_upload(); //$this-&gt;insert_article-&gt;insert_new_post($title, $category, $img_nw, $text, $source, $publish); $data['images'] = $this-&gt;upload_new_post-&gt;get_images(); echo "title of the post: " . $title . "&lt;br /&gt; and the text of the post " . $text . "&lt;br /&gt; and category is: " . $category . "&lt;br /&gt; and publish is: " .$publish . "&lt;br /&gt; and image: &lt;pre&gt;" . $do_upload ."&lt;/pre&gt;"; //echo $img_nw; $this-&gt;load-&gt;view('dogo/new_post.php', $data); } } } } </code></pre> <p>And here is the model to upload it:</p> <pre><code> &lt;?php class upload_new_post extends CI_Model{ // retreive categories var $file_path; var $file_path_url; function __construct() { parent::__construct(); $this-&gt;file_path = realpath(APPPATH . '../post_data/images'); $this-&gt;file_path_url = base_url().'post_data/images/'; } function do_upload(){ $config=array( 'allowed_types' =&gt; 'jpg|jpeg|gif|png', 'upload_path' =&gt; $this-&gt;file_path, 'max_size' =&gt; 2000 ); $this-&gt;load-&gt;library('upload', $config); $this-&gt;upload-&gt;do_upload(); $image_data = $this-&gt;upload-&gt;data(); $config = array( 'source_image' =&gt; $image_data['full_path'], 'new_image' =&gt; $this-&gt;file_path . '/thumbs', 'maintain_ration' =&gt; true, 'width' =&gt; 150, 'height' =&gt; 150 ); $this-&gt;load-&gt;library('image_lib', $config); $this-&gt;image_lib-&gt;resize(); } function get_images(){ $files = scandir($this-&gt;file_path); $files = array_diff($files, array('.', '..', 'thumbs')); $images = array(); foreach ($files as $file){ $images [] = array( 'url' =&gt; $this-&gt;file_path_url . $file, 'thumb_url' =&gt; $this-&gt;file_path_url . 'thumbs/' .$file ); } return $images; } } </code></pre> <p>And here the model to insert query:</p> <pre><code> &lt;?php class Insert_article extends CI_Model{ //insert new post function __construct() { parent::__construct(); } function insert_new_post($title, $category, $img_nw, $text, $source, $publish) { $query_insert = "INSERT INTO hs_news_nw (idcat_nw, idsc_nw, idusr_nw, title_nw, img_nw, text_nw, active_nw, totalview_nw, date_nw) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)"; $this-&gt;db-&gt;query($query_insert, array($category, $source, 1, $title, $img_nw, $text, 1, 1000, '2011-10-12 02:01:24')); } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload