{"id":4089,"date":"2019-04-03T18:55:12","date_gmt":"2019-04-03T13:25:12","guid":{"rendered":"https:\/\/pheonixsolutions.com\/blog\/?p=4089"},"modified":"2019-04-11T17:06:10","modified_gmt":"2019-04-11T11:36:10","slug":"upload-file-using-codeigniter-framework","status":"publish","type":"post","link":"https:\/\/pheonixsolutions.com\/blog\/upload-file-using-codeigniter-framework\/","title":{"rendered":"UPLOAD FILE USING CODEIGNITER FRAMEWORK"},"content":{"rendered":"\n<h3 class=\"wp-block-heading\">UPLOAD FILE Using CODEIGNITER FRAMEWORK<\/h3>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>Date posted :03\/04\/2019<\/strong><\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">Most importantly, in this tutorial we are going to upload the file using PHP- codeigniter framework. It has upload library for upload the file.<\/p>\n\n\n\n<figure class=\"wp-block-image is-resized\"><img fetchpriority=\"high\" decoding=\"async\" src=\"https:\/\/pheonixsolutions.com\/blog\/wp-content\/uploads\/2019\/04\/Screen-Shot-2019-04-04-at-5.24.39-PM-1.png\" alt=\"\" class=\"wp-image-4154\" width=\"422\" height=\"296\" srcset=\"https:\/\/pheonixsolutions.com\/blog\/wp-content\/uploads\/2019\/04\/Screen-Shot-2019-04-04-at-5.24.39-PM-1.png 504w, https:\/\/pheonixsolutions.com\/blog\/wp-content\/uploads\/2019\/04\/Screen-Shot-2019-04-04-at-5.24.39-PM-1-300x211.png 300w\" sizes=\"(max-width: 422px) 100vw, 422px\" \/><figcaption>File upload using codeigniter Framework<\/figcaption><\/figure>\n\n\n\n<h4 class=\"wp-block-heading\">pre-requesties<\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">   1)However create a uploads directory at the project root to store files.<br>   2)After that, we need to prepare a simple form. It allow us to select the  file.<br>   3)In addition to that generate the controller for include file details.<br>   4)Finally, create the model for database.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Step by step procedure:<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">The following steps are used to upload the file.<\/p>\n\n\n\n<h5 class=\"wp-block-heading\"><strong>step1:\ufeff<\/strong><\/h5>\n\n\n\n<p class=\"wp-block-paragraph\">Create a directory name as &#8216;uploads&#8217; And it should have the file permission of 777. Because, this is the most important step to allow permission on upload the file.<\/p>\n\n\n\n<h5 class=\"wp-block-heading\"><strong>step 2:\ufeff<\/strong><\/h5>\n\n\n\n<p class=\"wp-block-paragraph\">Using a text editor, create a form and name it as <strong>fileview.php(application\/views\/)<\/strong>. This will be used for rendering the html to user for allowing file upload functionality.<br><br>For example,<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">&lt;html&gt;\n   &lt;head&gt;\n      &lt;title&gt;My Form&lt;\/title&gt;\n   &lt;\/head&gt;\n   &lt;body&gt;\n      &lt;!-- &lt;form action = \"\"&gt; --&gt;\n      &lt;?php echo $error;?&gt;\n      &lt;?php echo form_open_multipart('Form\/do_upload');?&gt;\n      &lt;h5&gt;firstname&lt;\/h5&gt;\n      &lt;input type = \"text\" name = \"firstname\" value=\"\"&gt;\n      &lt;h5&gt;gender&lt;\/h5&gt;\n      &lt;input type = \"text\" name = \"gender\" value=\"\"&gt;  \n      &lt;h5&gt;hobbies&lt;\/h5&gt;\n      &lt;input type = \"text\" name = \"hobbies\" value=\"\"&gt;\n      &lt;h5&gt;district&lt;\/h5&gt;\n      &lt;input type = \"text\" name = \"district\" value=\"\"&gt;\n      &lt;h5&gt;discription&lt;\/h5&gt;\n      &lt;input type = \"text\" name = \"discription\" value=\"\"&gt;\n      &lt;h5&gt;userfile&lt;\/h5&gt;\n      &lt;input type = \"file\" name = \"userfile\"  value=\"\"&gt;\n      <div><\/div>\n      &lt;\/tr&gt;  \n      &lt;?php\n         echo form_close();\n         ?&gt;  \n   &lt;\/body&gt;\n&lt;\/html&gt;\n<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>step 3:<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Instantly, create a controller file and also name it as <strong>Form.php<\/strong><br><strong>File name :  (application\/controllers\/ Form.php).<\/strong><\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">public function upload_control() { \n          $this-&gt;load-&gt;library('form_validation');\n         $this-&gt;load-&gt;view('fileview', array('error' =&gt; ' ' )); \n      } \n\n   public function do_upload() { \n      #Used to load the model\n      $this-&gt;load-&gt;model('Insert_model');\n       #Used to load the form validation library  \n      $this-&gt;load-&gt;library('form_validation');\n       #Used to load the upload library\n       $this-&gt;load-&gt;library('upload');\n\n          #used to get the file details\n         $config['upload_path'] = '.\/uploads\/'; \n         $config['allowed_types'] = 'php|gif|jpg|png|txt|doc|docx'; \n         $config['max_size']      = '1000'; \n         $config['max_width']     = '1024'; \n         $config['max_height']    = '768';  \n         \n         $this-&gt;upload-&gt;initialize($config);\n\n           #used to upload the file name in to the folder(uploads)\n\n         if ( ! $this-&gt;upload-&gt;do_upload('userfile')) { \n              #$error used to display about error\n            $error = array('error' =&gt; $this-&gt;upload-&gt;display_errors());\n            $this-&gt;load-&gt;view('fileview', $error); \n\n         }\n         else\n          {         \n\n           $data = array('upload_data' =&gt; $this-&gt;upload-&gt;data());\n            $uploadData = $this-&gt;upload-&gt;data();\n            #used to get the file name\n            $userfile1 = $uploadData['file_name']; \n              \n             $postData = array(\n                'firstname' =&gt; $this-&gt;input-&gt;post('firstname'),\n                'gender' =&gt; $this-&gt;input-&gt;post('gender'),\n                  'hobbies' =&gt; $this-&gt;input-&gt;post('hobbies'),\n                    'district' =&gt; $this-&gt;input-&gt;post('district'),\n                      'discription' =&gt; $this-&gt;input-&gt;post('discription'),\n                      'userfile' =&gt; $userfile1,\n                      'discription' =&gt; $this-&gt;input-&gt;post('discription'),\n                 ); \n                 # used to insert the values in to database \n              $insert = $this-&gt;Insert_model-&gt;insert($postData); \n          echo \"Data inserted successfully\";\n           \n            \n         } \n        \n         }<\/pre>\n\n\n\n<ul class=\"wp-block-list\"><li>$this-&gt;load-&gt;model(&#8216;Insert_model&#8217;); = Used to load the form model(database).<\/li><li>$this-&gt;load-&gt;library(&#8216;form_validation&#8217;); = Used to load the form validation library.<\/li><li>$this-&gt;load-&gt;library(&#8216;upload&#8217;); = Used to load the upload library for file upload.<\/li><li>$this-&gt;upload-&gt;do_upload(&#8216;userfile&#8217;) =Used to upload the file name into the folder.<\/li><li>$error = array(&#8216;error&#8217; =&gt; $this-&gt;upload-&gt;display_errors()); = Used to diplay the error.<\/li><li>$this-&gt;load-&gt;view(&#8216;fileview&#8217;, $error); = This will load the form with the error.<\/li><\/ul>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>step4:<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">As a result of  create a model, name it as Insert_model.php<strong>(application\/controllers\/ Insert_model.php).<\/strong><\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">&lt;?php\nclass Insert_model extends CI_Model \n{\n\tpublic function insert($data = array()) \n\t{\n        $insert = $this-&gt;db-&gt;insert('form', $data);\n    }\n}\n?&gt;\n<\/pre>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>\ufeffconclusion:<\/strong><\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">Finally, The selected file name will show in the database and the file is uploaded to the folder with file name(uploads).<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"> Thanks for using pheonixsolutions. <\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\n\nYou find this tutorial helpful? Share with your friends to keep it alive.\n\n<\/p>\n","protected":false},"excerpt":{"rendered":"<p>UPLOAD FILE Using CODEIGNITER FRAMEWORK Date posted :03\/04\/2019 Most importantly, in this tutorial we are going to upload the file [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"site-sidebar-layout":"default","site-content-layout":"","ast-site-content-layout":"default","site-content-style":"default","site-sidebar-style":"default","ast-global-header-display":"","ast-banner-title-visibility":"","ast-main-header-display":"","ast-hfb-above-header-display":"","ast-hfb-below-header-display":"","ast-hfb-mobile-header-display":"","site-post-title":"","ast-breadcrumbs-content":"","ast-featured-img":"","footer-sml-layout":"","ast-disable-related-posts":"","theme-transparent-header-meta":"","adv-header-id-meta":"","stick-header-meta":"","header-above-stick-meta":"","header-main-stick-meta":"","header-below-stick-meta":"","astra-migrate-meta-layouts":"default","ast-page-background-enabled":"default","ast-page-background-meta":{"desktop":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"ast-content-background-meta":{"desktop":{"background-color":"var(--ast-global-color-4)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"var(--ast-global-color-4)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"var(--ast-global-color-4)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2},"jetpack_post_was_ever_published":false},"categories":[1022],"tags":[509,273],"class_list":["post-4089","post","type-post","status-publish","format-standard","hentry","category-web-architecture","tag-codeiginiter","tag-php","psol-cat-web-architecture"],"jetpack_publicize_connections":[],"jetpack_shortlink":"https:\/\/wp.me\/phn2x7-13X","jetpack_sharing_enabled":true,"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/posts\/4089","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/comments?post=4089"}],"version-history":[{"count":0,"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/posts\/4089\/revisions"}],"wp:attachment":[{"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/media?parent=4089"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/categories?post=4089"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/tags?post=4089"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}