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 <span class="highlighted">form</span> to <span class="highlighted">upload</span> a file and <span class="highlighted">email</span> it to a specific <span class="highlighted">email</span> address as an <span class="highlighted">attachment</span>. First, create a folder inc/app/myapp/forms/<span class="highlighted">upload</span> which we'll use to contain our <span class="highlighted">form</span>. We'll then need to create 3 files in this folder:

access.<span class="highlighted">php</span>

sitellite_action = on
sitellite_access = public
sitellite_status = approved

This lets us call the <span class="highlighted">form</span> via the URL:

http://www.example.com/index/myapp-<span class="highlighted">upload</span>-<span class="highlighted">form</span>

settings.<span class="highlighted">php</span>

[<span class="highlighted">Form</span>]

uploadFiles = no
extra = "enctype='multipart/<span class="highlighted">form</span>-data'"

[<span class="highlighted">upload</span>]

type = file

[submit_button]

type = submit
setValues = <span class="highlighted">Upload</span>

This defines which fields the <span class="highlighted">form</span> 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 <span class="highlighted">upload</span> the file with the <span class="highlighted">form</span> submission.

index.<span class="highlighted">php</span>


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

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

        // here we add the file as an <span class="highlighted">attachment</span> to the <span class="highlighted">email</span>.
        // 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 <span class="highlighted">from</span> the user's browser.
        $m->AddAttachment ($vals['<span class="highlighted">upload</span>']->tmp_name, $vals['<span class="highlighted">upload</span>']->name);

        // the rest is all PHPMailer basics
        $m->Body = 'The attached file has been uploaded <span class="highlighted">from</span> user ' . $_SERVER['REMOTE_ADDR'];
        $m->isMail ();
        if (! $m->Send ()) {
            // <span class="highlighted">display</span> an error if the mail failed
            page_title ('Error');
            echo 'Error sending <span class="highlighted">email</span>';
            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['<span class="highlighted">upload</span>'] value, which corresponds to the file widget in the {{settings.<span class="highlighted">php</span> 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.

Revision from August 1, 2007 1:15 PM by anonymous

Forward in time (1 more) | Back in time (2 more) | See current | See changes | Linked from: Forms