CodeIgniter Forums
Uploading Multiple Files As Email Attachment - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23)
+--- Thread: Uploading Multiple Files As Email Attachment (/showthread.php?tid=57626)

Pages: 1 2 3


Uploading Multiple Files As Email Attachment - El Forum - 03-28-2013

[eluser]RaGe10940[/eluser]
Hello CI Community,

I have a email system that I need multiple file attachments to be sent in from one upload input.

The system does :

1) Use student ID, First and last name,
2) pulls the email from the DB with the 3 fields the staff member typed
3) an email will be sent to a student with the attachments

I have it working (sorta)... however for only one document though...

What I have done :

1) The emailing works perfectly.
2) The problem arises when I go do the uploading
3) I have taken a look at my upload directory, however... ONLY ONE form is uploaded not both.


This is my view :

Code:
<div id="attachment">
&lt;input type="file" name="userfile" multiple="" /&gt;
    </div>
    <div id="sendemail">
&lt;?php
echo form_submit('submit', 'Send Email');
echo anchor('staff_controller/index', 'Return');
?&gt;
    </div>

This is just the uploading part the rest of the view is not needed for this question :

my upload part of my controller :

Code:
$config['upload_path'] = '/usr/local/var/www/Test/ci/uploads/';
      $config['allowed_types'] = 'txt|pdf';
      $config['overwrite'] = TRUE;
      $config['encrypt_name'] = TRUE;

      $this->load->library('upload');

      foreach ($_FILES as $key => $value) {
   if (!empty($key['userfile'])) {
       $this->upload->initialize($config);
       if (!$this->upload->do_upload($key)) {
    $data['main_content'] = 'emailsystem/emails_view';
    $data['error'] = $this->upload->display_errors('<p class="error">', '</p>');
    $this->load->view('includes/no_js/template', $data);
       } else {

After the else part of my controller (the email stuff) :

Code:
$return = $this->upload->data();

    $this->load->library('email');
    $this->load->library('encrypt');

    $anum = $this->input->post('anum');
    $first = $this->input->post('fname');
    $last = $this->input->post('lname');

    $email = $this->email_model->emails($anum, $first, $last);
    $subject = $this->input->post('subject');
    $body = $this->input->post('message');
    $file = $return['full_path'];

    $this->email->from('[email protected]');
    $this->email->to($email);
    $this->email->subject($subject);
    $this->email->message($body);
    $this->email->attach($file);

    if (!$this->email->send()) {
        $this->emailview();
    } else {
        $this->session->set_flashdata('emailview', 'Email Sent To ' . $email . '. With A Number ' . $anum . '');
        redirect('email_controller/emailview', 'location');
    }
       }

Again to reidirate my email function IS WORKING

The problem occurs when I try to upload. Only one file is uploaded not the other (i am testing with .txt files, and I am getting no errors)

Any help would be awesome.

Regards,


Uploading Multiple Files As Email Attachment - El Forum - 03-28-2013

[eluser]TheFuzzy0ne[/eluser]
Have you tried outputting the $_FILES array to make sure it's in the format you expect it to be in?

Your code sends two emails (one for each file), but after the first one is sent successfully you're redirecting - D'OH!


Uploading Multiple Files As Email Attachment - El Forum - 03-28-2013

[eluser]RaGe10940[/eluser]
[quote author="TheFuzzy0ne" date="1364483332"]Have you tried outputting the $_FILES array to make sure it's in the format you expect it to be in?

Your code sends two emails (one for each file), but after the first one is sent successfully you're redirecting - D'OH![/quote]

I'm sorry I don't understand? My code sends to two emails? It shouldn't... only one email can be queried due to how I have my WHERE clause set up.

Can you elaborate?


Uploading Multiple Files As Email Attachment - El Forum - 03-28-2013

[eluser]RaGe10940[/eluser]
Code:
array(1) { ["userfile"]=> array(5) { ["name"]=> string(9) "test2.txt" ["type"]=> string(10) "text/plain" ["tmp_name"]=> string(14) "/tmp/phpuuimFq" ["error"]=> int(0) ["size"]=> int(13) } }

Is what the vardump of the $_FILES is outputting


Uploading Multiple Files As Email Attachment - El Forum - 03-28-2013

[eluser]TheFuzzy0ne[/eluser]
Indeed, I can.

Code:
$config['upload_path'] = '/usr/local/var/www/Test/ci/uploads/';
$config['allowed_types'] = 'txt|pdf';
$config['overwrite'] = TRUE;
$config['encrypt_name'] = TRUE;

$this->load->library('upload');

foreach ($_FILES as $key => $value) { // Loop through each file in the $_FILES array.
    if (!empty($key['userfile'])) {
        $this->upload->initialize($config); // Set up the upload library configuration (probably best done BEFORE the loop).
        
        if (!$this->upload->do_upload($key)) { // Show a view if the upload of a SINGLE file fails. Is this a partial, or a full page?
            $data['main_content'] = 'emailsystem/emails_view';
            $data['error'] = $this->upload->display_errors('<p class="error">', '</p>');
            $this->load->view('includes/no_js/template', $data);
        } else { // We made it thus far, all is good. Yippee!
            $return = $this->upload->data();

            // Blah, blah, blah
            
            $this->email->attach($file); Attach the CURRENT FILE ONLY

            if (!$this->email->send()) { // If sending the mail fails, load a view. Is this a partial, or an entire page? I don't know...
                $this->emailview();
            } else {
                $this->session->set_flashdata('emailview', 'Email Sent To ' . $email . '. With A Number ' . $anum . '');
                redirect('email_controller/emailview', 'location'); // Redirect after sending a single file. Are we still in the loop? I think so!
                // This should be OUTSIDE of the loop.
            }
        }
    }
}



Uploading Multiple Files As Email Attachment - El Forum - 03-28-2013

[eluser]TheFuzzy0ne[/eluser]
[quote author="RaGe10940" date="1364484794"]
Code:
array(1) { ["userfile"]=> array(5) { ["name"]=> string(9) "test2.txt" ["type"]=> string(10) "text/plain" ["tmp_name"]=> string(14) "/tmp/phpuuimFq" ["error"]=> int(0) ["size"]=> int(13) } }

Is what the vardump of the $_FILES is outputting[/quote]

Now that's odd, don't ya think? There's only one file there. Maybe you need to upload an array?
Code:
<div id="attachment">
    &lt;input type="file" name="userfile[]" multiple="" /&gt;
</div>
<div id="sendemail">&lt;?php
    echo form_submit('submit', 'Send Email');
    echo anchor('staff_controller/index', 'Return');
?&gt;</div>

Ta-da! Smile Now you just need to modify your code to process the $_FILES array properly.


Uploading Multiple Files As Email Attachment - El Forum - 03-28-2013

[eluser]RaGe10940[/eluser]
Code:
function sendemail() {
$this->load->library('form_validation');

$anum = $this->input->post('anum');
$first = $this->input->post('fname');
$last = $this->input->post('lname');

if ((empty($anum)) || (empty($first)) || (empty($last))) {
     $this->session->set_flashdata('emailview', 'You Must Use A Number, First Name And Last Name To Find Student');
     redirect('email_controller/emailview', 'location');
} else {

     $this->form_validation->set_rules('anum', 'A Number', 'required|alpha_numeric|exact_length[9]');
     $this->form_validation->set_rules('fname', 'First Name', 'required|alpha|max_length[20]');
     $this->form_validation->set_rules('lname', 'Last Name', 'required|alpha|max_length[20]');
     $this->form_validation->set_rules('subject', 'Subject', 'max_length[30]|min_length[5]|required');
     $this->form_validation->set_rules('message', 'Message', 'max_length[200]|min_length[5]');

     if ($this->form_validation->run() == FALSE) {
  $this->emailview();
     } else {
  $this->load->model('email_model');

  $anum = $this->input->post('anum');
  $first = $this->input->post('fname');
  $last = $this->input->post('lname');

  if ($this->email_model->checkstudent($anum, $first, $last)) {

      $config['upload_path'] = '/usr/local/var/www/Test/ci/uploads/';
      $config['allowed_types'] = 'txt|pdf';
      $config['overwrite'] = TRUE;
      $config['encrypt_name'] = TRUE;

      $this->load->library('upload', $config);

      foreach ($_FILES as $key => $value) {
   var_dump($value);
    if (!$this->upload->do_upload($key)) {
       $data['main_content'] = 'emailsystem/emails_view';
       $data['error'] = $this->upload->display_errors('<p class="error">', '</p>');
       $this->load->view('includes/no_js/template', $data);
   } else {
       $return = $this->upload->data();

       $this->load->library('email');
       $this->load->library('encrypt');

       $anum = $this->input->post('anum');
       $first = $this->input->post('fname');
       $last = $this->input->post('lname');

       $email = $this->email_model->emails($anum, $first, $last);
       $subject = $this->input->post('subject');
       $body = $this->input->post('message');
       $file = $return['full_path'];

       $this->email->from('[email protected]');
       $this->email->to($email);
       $this->email->subject($subject);
       $this->email->message($body);
       $this->email->attach($file);

       if (!$this->email->send()) {
    $this->emailview();
       } else {
    $this->session->set_flashdata('emailview', 'Email Sent To ' . $email . '. With A Number ' . $anum . '');
    redirect('email_controller/emailview', 'location');
       }
   }
      }
  } else {
      $data['main_content'] = 'emailsystem/emails_view';
      $data['error'] = '<p class="error">Student With A Number ' . $anum . ' Does Not Exist</p>';
      $this->load->view('includes/no_js/template', $data);
  }
     }
}
    }

with the current controller, and this view :

<div id="attachment">
&lt;input type="file" name="userfile[]" multiple="" /&gt;
</div>

I keep getting this error : You did not select a file to upload.

I do though however get this when I dump $value :

Code:
rray(5) { ["name"]=> array(2) { [0]=> string(8) "test.txt" [1]=> string(9) "test2.txt" } ["type"]=> array(2) { [0]=> string(10) "text/plain" [1]=> string(10) "text/plain" } ["tmp_name"]=> array(2) { [0]=> string(14) "/tmp/phpG4y0OF" [1]=> string(14) "/tmp/phpoG9lSJ" } ["error"]=> array(2) { [0]=> int(0) [1]=> int(0) } ["size"]=> array(2) { [0]=> int(11) [1]=> int(13) } }

both files are now in the array


Uploading Multiple Files As Email Attachment - El Forum - 03-28-2013

[eluser]TheFuzzy0ne[/eluser]
Oh yes, I forgot. Sorry!

The uploads library doesn't support multiple uploads. You'll need to extend it to add that functionality. It shouldn't be too difficult to get working.

I'm going to try and find the time to submit that as a feature to CodeIgniter 3. Until now, I've wanted to contribute, but I've been at a loss as to what to contribute. Now I have a decent contribution to make, thanks to you! Thanks! Smile


Uploading Multiple Files As Email Attachment - El Forum - 03-28-2013

[eluser]RaGe10940[/eluser]
There actually is extension to the library on GitHub https://github.com/nicdev/CodeIgniter-Multiple-File-Upload I'ma come back with my results. Thanks for letting me know and yes that is a very big constraint of CodeIgniter. It forces one to do a upload with the input (x amount of times rather then just holding ctrl + mouse click)...

fail on CI's part. Thanks for the help though.

And if PHP the right way is your website then I love you <3 Tongue


Uploading Multiple Files As Email Attachment - El Forum - 03-28-2013

[eluser]RaGe10940[/eluser]
Using this library : https://github.com/nicdev/CodeIgniter-Multiple-File-Upload

I am able to upload multiple files to my directory that works fine and dandy and even the email works

However now only one email is sending an attachment (again)

however I noticed a problem when trying to send and array...

I am getting this :

Code:
array(14) { ["file_name"]=> string(9) "test2.txt" ["file_type"]=> string(10) "text/plain" ["file_path"]=> string(35) "/usr/local/var/www/Test/ci/uploads/" ["full_path"]=> string(44) "/usr/local/var/www/Test/ci/uploads/test2.txt" ["raw_name"]=> string(5) "test2" ["orig_name"]=> string(9) "test2.txt" ["client_name"]=> string(9) "test2.txt" ["file_ext"]=> string(4) ".txt" ["file_size"]=> float(0.01) ["is_image"]=> bool(false) ["image_width"]=> string(0) "" ["image_height"]=> string(0) "" ["image_type"]=> string(0) "" ["image_size_str"]=> string(0) "" }

which is the $this->upload->data()

now I run this through :
Code:
foreach ($this->upload->data() as $fileData) {
       $this->email->attach($fileData['full_path']);
   }


However the problem is now that the array is not adding the second, third, fourth, fifth file... it only adds one file.

in my view :

div id="attachment">
&lt;input type="file" name="userfile[]" multiple /&gt;
</div>

and still nothing..

I tried using this library as well :

https://github.com/stvnthomas/CodeIgniter-Multi-Upload

and still the same problem. Any tips?