Making $_POST and $_FILES play together nicely

View: New views
3 Messages — Rating Filter:   Alert me  

Making $_POST and $_FILES play together nicely

by Mike Potter-2 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Hi,

I have a PHP5 .class file that validates form inputs and sends
notification emails from contact pages. Recently a client wanted to
add a file upload function. No sweat, I thought.

Well, I can't get the $_FILES portion to validate properly in my
.class file, since it apparently only registers the $_POST vars. This
is the section of code, *currently functional as-is* that I need to
modify so that $_FILES is also processed:

        public function validateForm() {
                $requiredFields = array( "name" => "My Name", "phone" => "My
Telephone", "email" => "My Email",
                 "file" => "My Upload File", "comments" => "My Comments" );
               
                if( isset( $_POST ) && is_array( $_POST ) && count( $_POST ) > 0 ) {
                        $reqFieldsPresent = true;
                        $this->msg = "<p>The following required fields were not filled out:</p><ul>";
                        foreach( $requiredFields as $field => $value ) {
                                if( !isset( $_POST[ $field ] ) ||
                                                         $_POST[ $field ] == "" ||
                                                         $_POST[ $field ] == $value ) {
                                        $reqFieldsPresent = false;
                                        $this->msg .= "<li>" . $value . "</li>";

                        }

How can I modify this so that if $_FILES['userfile']['name'] is
present the fileUpload() function can be invoked and the file uploaded
to the client's server, but if the upload file has not been specified
the absence will be reported same as if $_POST['name'] is absent?

Ski

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


Re: Making $_POST and $_FILES play together nicely

by Nilesh Govindrajan-3 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

On Tuesday 30 September 2008 09:41:33 pm Mike Potter wrote:

> Hi,
>
> I have a PHP5 .class file that validates form inputs and sends
> notification emails from contact pages. Recently a client wanted to
> add a file upload function. No sweat, I thought.
>
> Well, I can't get the $_FILES portion to validate properly in my
> .class file, since it apparently only registers the $_POST vars. This
> is the section of code, *currently functional as-is* that I need to
> modify so that $_FILES is also processed:
>
> public function validateForm() {
> $requiredFields = array( "name" => "My Name", "phone" => "My
> Telephone", "email" => "My Email",
> "file" => "My Upload File", "comments" => "My Comments" );
>
> if( isset( $_POST ) && is_array( $_POST ) && count( $_POST ) > 0 ) {
> $reqFieldsPresent = true;
> $this->msg = "<p>The following required fields were not filled
> out:</p><ul>"; foreach( $requiredFields as $field => $value ) {
> if( !isset( $_POST[ $field ] ) ||
> $_POST[ $field ] == "" ||
> $_POST[ $field ] == $value ) {
> $reqFieldsPresent = false;
> $this->msg .= "<li>" . $value . "</li>";
>
> }
>
> How can I modify this so that if $_FILES['userfile']['name'] is
> present the fileUpload() function can be invoked and the file uploaded
> to the client's server, but if the upload file has not been specified
> the absence will be reported same as if $_POST['name'] is absent?
>
> Ski
How about using -

if(is_uploaded_file($_FILES['userfile']['tmp_name'])) {

//success code

} else {

//failure code

}


signature.asc (204 bytes) Download Attachment

Re: Making $_POST and $_FILES play together nicely

by Ross McKay :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

On Tue, 30 Sep 2008 11:11:33 -0500, Mike Potter wrote:

>I have a PHP5 .class file that validates form inputs and sends
>notification emails from contact pages. Recently a client wanted to
>add a file upload function. No sweat, I thought.
>
>Well, I can't get the $_FILES portion to validate properly in my
>.class file, since it apparently only registers the $_POST vars. This
>is the section of code, *currently functional as-is* that I need to
>modify so that $_FILES is also processed:
>[...]

Not wanting to comment on your formfield validation, here's what I do
with file upload validation:

if (!isset($_FILES[$inputName])) {
    // handle form error (e.g. forgot form encoding type!)
}
else
switch ($_FILES[$inputName]['error']) {
    case UPLOAD_ERR_OK:
        // process as per nicely uploaded file
        break;

    case UPLOAD_ERR_NO_FILE:
        // file wasn't attached; if mandatory, complain!
        break;

    case UPLOAD_ERR_INI_SIZE:
    case UPLOAD_ERR_FORM_SIZE:
        $errmsg .= "# error uploading file: file too big.<br/>\n";
        break;

    default:
        $errmsg .= "# error uploading file: "
            . $_FILES[$inputName]['error'] . ".<br/>\n";
        break;
}
--
Ross McKay, Toronto, NSW Australia
"Let the laddie play wi the knife - he'll learn"
- The Wee Book of Calvin

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

LightInTheBox - Buy quality products at wholesale price!