Re: [PHP] Making $_POST and $_FILES play together nicely



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

}

Attachment: signature.asc
Description: This is a digitally signed message part.