Members

Note: You can use your Sitellite.org account here and vice versa.

Username

Password

Remember Login

Forgot your password?

Not a member? Click here to register

Mail Form File Uploads

Home Page | All Pages | Recently Revised | Authors | Feeds |

Here's an example of using a custom form to upload a file and email it to a specific email address as an attachment. First, create a folder inc/app/myapp/forms/upload which we'll use to contain our form. We'll then need to create 3 files in this folder:

access.php

sitellite_action = on
sitellite_access = public
sitellite_status = approved

This lets us call the form via the URL:

http://www.example.com/index/myapp-upload-form

settings.php

[Form]

uploadFiles = no
extra = "enctype='multipart/form-data'"

[upload]

type = file

[submit_button]

type = submit
setValues = Upload

This defines which fields the form should have and how it should behave. Here we've told it we don't want MailForm to handle moving the uploaded file for us (the line uploadFiles = no). We've also passed it an extra enctype parameter so the user's browser knows to upload the file with the form submission.

index.php


<?php

class MyappUploadForm extends MailForm {
    function MyappUploadForm () {
        parent::MailForm ();
        $this->parseSettings ('inc/app/myapp/forms/upload/settings.php');
        page_title ('Upload Form');
    }

    function onSubmit ($vals) {
        // import the PHPMailer package, which we'll use to send the file
        loader_import ('ext.phpmailer');
        $m = new PHPMailer ();
        $m->From = 'noreply@' . str_replace ('www.', '', site_domain ());
        $m->Subject = 'Uploaded File';
        $m->AddAddress ('you@example.com');

        // here we add the file as an attachment to the email.
        // the ->tmp_name property points to the location of the
        // file on the server, and ->name contains the name of
        // the file as it was sent from the user's browser.
        $m->AddAttachment ($vals['upload']->tmp_name, $vals['upload']->name);

        // the rest is all PHPMailer basics
        $m->Body = 'The attached file has been uploaded from user ' . $_SERVER['REMOTE_ADDR'];
        $m->isMail ();
        if (! $m->Send ()) {
            // display an error if the mail failed
            page_title ('Error');
            echo 'Error sending email';
            return;
        }
        // show a positive response, the mail was sent with the file
        page_title ('File Sent Off');
        echo 'Your file has been sent, thanks.';
    }
}

?>

The $vals['upload'] value, which corresponds to the file widget in the {{settings.php file, is an object of the type saf.CGI.UploadedFile. Take a look at that object in the SAF API references for more information about using that object.

Revised on February 27, 2007 1:04 PM by anonymous

Back in time (3 more) | Linked from: Forms