title: attach a file from directory and send to email with codeigniter
body: <p>I am a newbie with codeigniter and I want to build a website using codeigniter framework. From first, it look fine I can use database, validation, email, session and then I try to attach a file and send with email :</p>
  <blockquote>
  <p>$this-&gt;email-&gt;attach('/path/ofyour/constan/file.anything');</p>
  </blockquote>
  <p>thats work too.</p>
  <p>since that is a website I want my client to choose file they want to upload.
  I try many method, and many of them tell to upload a file to server root and get the file_data, use file_data[file_patch]</p>
  <blockquote>
  <p>$this-&gt;email-&gt;attach('file_data[file_path]');</p>
  </blockquote>
  <p>the problem is:</p>
  <ol>
  <li><p>since code igniter cant upload multiple data I must use plugin. I tried and its PAIN</p>
  </li>
  <li><p>I thing its not effective, upload data to server root and then to email?</p>
  </li>
  <li><p>it better to just get file_path of upload file and send them to email, how?</p>
  </li>
  </ol>
  <p>I build it with jquery mobile, what must I do?</p>
  <h1>Update</h1>
  <p>ok i decide to use uploadify i search every website and then i found <a href="http://ellislab.com/forums/viewthread/109751/" rel="nofollow noreferrer">here</a> and my code is</p>
  <p>uploadify.php</p>
  <pre><code>&lt;?php
  /*
  *   Functions taken from CI_Upload Class
  *
  */
      
      function set_filename($path, $filename, $file_ext, $encrypt_name = FALSE)
      {
          if ($encrypt_name == TRUE)
          {       
              mt_srand();
              $filename = md5(uniqid(mt_rand())).$file_ext;   
          }
      
          if ( ! file_exists($path.$filename))
          {
              return $filename;
          }
      
          $filename = str_replace($file_ext, '', $filename);
          
          $new_filename = '';
          for ($i = 1; $i &lt; 100; $i++)
          {           
              if ( ! file_exists($path.$filename.$i.$file_ext))
              {
                  $new_filename = $filename.$i.$file_ext;
                  break;
              }
          }
  
          if ($new_filename == '')
          {
              return FALSE;
          }
          else
          {
              return $new_filename;
          }
      }
      
      function prep_filename($filename) {
         if (strpos($filename, '.') === FALSE) {
            return $filename;
         }
         $parts = explode('.', $filename);
         $ext = array_pop($parts);
         $filename    = array_shift($parts);
         foreach ($parts as $part) {
            $filename .= '.'.$part;
         }
         $filename .= '.'.$ext;
         return $filename;
      }
      
      function get_extension($filename) {
         $x = explode('.', $filename);
         return '.'.end($x);
      } 
  
  
  // Uploadify v1.6.2
  // Copyright (C) 2009 by Ronnie Garcia
  // Co-developed by Travis Nickels
  if (!empty($_FILES)) {
      $path = $_SERVER['DOCUMENT_ROOT'] . $_REQUEST['folder'] . '/';
     //$client_id = $_GET['client_id'];
     $file_temp = $_FILES['Filedata']['tmp_name'];
     $file_name = prep_filename($_FILES['Filedata']['name']);
     $file_ext = get_extension($_FILES['Filedata']['name']);
     $real_name = $file_name;
     $newf_name = set_filename($path, $file_name, $file_ext);
     $file_size = round($_FILES['Filedata']['size']/1024, 2);
     $file_type = preg_replace(&quot;/^(.+?);.*$/&quot;, &quot;\\1&quot;, $_FILES['Filedata']['type']);
     $file_type = strtolower($file_type);
     $targetFile =  str_replace('//','/',$path) . $newf_name;
     move_uploaded_file($file_temp,$targetFile);
  
     $filearray = array();
     $filearray['file_name'] = $newf_name;
     $filearray['real_name'] = $real_name;
     $filearray['file_ext'] = $file_ext;
     $filearray['file_size'] = $file_size;
     $filearray['file_path'] = $targetFile;
     $filearray['file_temp'] = $file_temp;
     //$filearray['client_id'] = $client_id;
  
     $json_array = json_encode($filearray);
     echo $json_array;
  }else{
      echo &quot;1&quot;;   
  }
  </code></pre>
  <p>i dont relly know what is going on here, like i said i am a newbie but i know something that $json_array, that array hold my data $filearray, that is data file uploaded. mission one complete</p>
  <p>now my controller: upload.php</p>
  <pre><code>&lt;?php
  class Upload extends CI_Controller
  {
  public function __construct()
      {
          parent::__construct();
          
                  $this-&gt;load-&gt;helper('form');
                  $this-&gt;load-&gt;helper('url');
  
      }
      /*
      *   Display upload form
      */
      function index()
      {
          
          $this-&gt;load-&gt;view('view');
      }
      
      
      /*
      *   Handles JSON returned from /js/uploadify/upload.php
      */
      function uploadify()
      {
          
          //Decode JSON returned by /js/uploadify/upload.php
          $file = $this-&gt;input-&gt;post('filearray');
          $data['json'] = json_decode($file);
          
          $this-&gt;load-&gt;view('uploadify',$data);
      }
      
  }
  /* End of File /application/controllers/upload.php */
  </code></pre>
  <p>my plan is send the data in onComplete function</p>
  <p>my view :view.php</p>
  <pre><code>&lt;!DOCTYPE html PUBLIC &quot;-//W3C//DTD XHTML 1.0 Transitional//EN&quot; &quot;http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd&quot;&gt;
  &lt;html xmlns=&quot;http://www.w3.org/1999/xhtml&quot;&gt;
  &lt;&lt;head&gt;
      &lt;meta charset=&quot;UTF-8&quot;&gt;
      &lt;title&gt;Uploadify and Codeigniter Tutorial&lt;/title&gt;
  &lt;?php
     $this-&gt;load-&gt;helper('html');
     echo link_tag('http://uploadify_tutorial/uploadify/uploadify.css');
     echo '&lt;script src=&quot;http://ajax.googleapis.com/ajax/libs/jquery/1.6/jquery.min.js&quot; type=&quot;text/javascript&quot;&gt;&lt;/script&gt;';
     echo '&lt;script src=&quot;http://localhost/uploadify_tutorial/uploadify/swfobject.js&quot; type=&quot;text/javascript&quot;&gt;&lt;/script&gt;';
     echo '&lt;script src=&quot;http://localhost/uploadify_tutorial/uploadify/jquery.uploadify.v2.1.4.min.js&quot; type=&quot;text/javascript&quot;&gt;&lt;/script&gt;';
  $uploadpath=&quot;&quot;;
  $uploadpath=str_ireplace($_SERVER['DOCUMENT_ROOT'],&quot;&quot;, realpath($_SERVER['SCRIPT_FILENAME']));
  $uploadpath=str_ireplace(&quot;index.php&quot;,&quot;&quot;,$uploadpath);
  ?&gt;
  
  
      &lt;script type=&quot;text/javascript&quot; language=&quot;javascript&quot;&gt;
          $(document).ready(function(){
                                          
                      $(&quot;#upload&quot;).uploadify({
                              uploader: '&lt;?php echo base_url();?&gt;uploadify/uploadify.swf',
                              script: '&lt;?php echo base_url();?&gt;uploadify/uploadify.php',
                              cancelImg: '&lt;?php echo base_url();?&gt;uploadify/cancel.png',
                              folder: '/uploads',
                              scriptAccess: 'always',
                              multi: true,
                              'onError' : function (a, b, c, d) {
                                   if (d.status == 404)
                                      alert('Could not find upload script.');
                                   else if (d.type === &quot;HTTP&quot;)
                                      alert('error '+d.type+&quot;: &quot;+d.status);
                                   else if (d.type ===&quot;File Size&quot;)
                                      alert(c.name+' '+d.type+' Limit: '+Math.round(d.sizeLimit/1024)+'KB');
                                   else
                                      alert('error '+d.type+&quot;: &quot;+d.text);
                                  },
                              'onComplete'   : function (event, queueID, fileObj, response, data) {
                                                  //Post response back to controller
                                                  $.post('&lt;?php echo site_url('upload/uploadify');?&gt;',{filearray: response},function(info){
                                                      $(&quot;#target&quot;).append(info);  //Add response returned by controller                                                                         
                                                  });                                         
                              }
                      });             
          });
      &lt;/script&gt;
  &lt;/head&gt;
  
  &lt;body&gt;
  &lt;h1&gt;Uploadify Example&lt;/h1&gt;
      
      &lt;?php echo form_open_multipart('upload/index');?&gt;
      
      &lt;p&gt;
          &lt;label for=&quot;Filedata&quot;&gt;Choose a File&lt;/label&gt;&lt;br/&gt;
          &lt;?php echo form_upload(array('name' =&gt; 'Filedata', 'id' =&gt; 'upload'));?&gt;
          &lt;a href=&quot;javascript:$('#upload').uploadifyUpload();&quot;&gt;Upload File(s)&lt;/a&gt;
      &lt;/p&gt;
      
      
      &lt;?php echo form_close();?&gt;
      
      &lt;div id=&quot;target&quot;&gt;
      
      &lt;/div&gt;
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  <p>my view : uploadify</p>
  <pre><code>&lt;html&gt;
      &lt;ul&gt;
      
          &lt;li&gt;Extension: &lt;?php echo $json-&gt;{'file_ext'};?&gt;&lt;/li&gt;
          &lt;li&gt;File Size: &lt;?php echo $json-&gt;{'file_size'};?&gt;&lt;/li&gt;
          &lt;li&gt;File Path: &lt;?php echo $json-&gt;{'file_path'};?&gt;&lt;/li&gt;
      &lt;/ul&gt;
      &lt;/html&gt;
  </code></pre>
  <p>and then parsing that json_array variable to my view, that is the plans, but in reality that code doesn work the data is undefined
  an error Trying to get property of non-object i use this code <a href="https://bitbucket.org/pickupman/codeigniter-uploadify" rel="nofollow noreferrer">here</a>, I suppose the problem is with json</p>
  <p>i just want to use the data file uploaded if anyone can solve that problem please share it or send me CI+uploadify program to my email, if anyone expert about CI and Uploadify plugin please make the tutorial step by step how to use it, step by step, i think it would be great help for newbie like me</p>
  <p>thanks....</p>
  <p>my email :saya.dean@gmail.com</p>