Page 1 of 1

PHP not submitting HTML form *file* correctly

Posted: Fri Mar 25, 2016 7:58 am
by thomfar44
Alright, pretty much everything is working with the HTML and PHP form.

However, when a user submits a file in the HTML form, the PHP sends it to my email but It is not downloadable, I can only view the file name they submitted. I want to be able to download the file they sent.

HTML code:

<form action="" method=POST id=uploadform autocomplete=off enctype="multipart/form-data">
<div class=top-row>
<div class=field-wrap>
<input id="sender" type="text" value="<?php echo !empty($name)?$name:''; ?>" placeholder="Your name" name="name" required>
</div>
<div class=field-wrap>
<input id=senderEmail type="email" value="<?php echo !empty($email)?$email:''; ?>" placeholder="Email@domain.com" name="email" required>
</div>
</div>
<div class=top-row>
<div class=field-wrap>
<input id="sender" type="text" value="<?php echo !empty($videolink)?$videolink:''; ?>" placeholder="Video Link" name="videolink">
</div>
##Look around here (Just a little guidance)##
<div class=field-wrap>
<input id="file" type="file" value="<?php echo !empty($file)?$file:''; ?>" name="file">
</div>
</div>
<textarea id=message type="text" placeholder=Description name=message required><?php echo !empty($message)?$message:''; ?></textarea>
<div class="g-recaptcha" data-sitekey="My google site verification would be here"></div>
<div class=boxcheckauth>
<span class=checkboxdescription>By uploading, you agree to the TOS and privacy policy.</span>
</div>
<?php if(!empty($errMsg)): ?><div class="errMsg"><?php echo $errMsg; ?></div><?php endif; ?>
<?php if(!empty($succMsg)): ?><div class="succMsg"><?php echo $succMsg; ?></div><?php endif; ?>
<center><input type=submit name=submit class="button button-block" value="Upload"/></center>
</form>

PHP code:

Code: Select all

if(isset($_POST['submit'])):
if(isset($_POST['g-recaptcha-response']) && !empty($_POST['g-recaptcha-response'])):
    //your site secret key
    $secret = 'My google site verification would be here';
    //get verify response data
    $verifyResponse = file_get_contents('https://www.google.com/recaptcha/api/siteverify?secret='.$secret.'&response='.$_POST['g-recaptcha-response']);
    $responseData = json_decode($verifyResponse);

    $name = !empty($_POST['name'])?$_POST['name']:'';
    $email = !empty($_POST['email'])?$_POST['email']:'';
    $videolink = !empty($_POST['videolink'])?$_POST['videolink']:'';
    $file = !empty($_POST['file'])?$_POST['file']:''; ##Look around here (Just a little guidance)##
    $message = !empty($_POST['message'])?$_POST['message']:'';
    if($responseData->success):
        //contact form submission code
        $to = 'My email would be here';
        $subject = 'Upload form submission';
        $htmlContent = "
            <h1>Upload form submission</h1>
            <p><b>Name: </b>".$name."</p>
            <p><b>Email: </b>".$email."</p>
            <p><b>Video link: </b>".$videolink."</p>
            <p><b>File: </b>".$file."</p> ##Look around here (Just a little guidance)##
            <p><b>Message: </b>".$message."</p>
        ";
        // Always set content-type when sending HTML email
        $headers = "MIME-Version: 1.0" . "\r\n";
        $headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";
        // More headers
        $headers .= 'From:'.$name.' <'.$email.'>' . "\r\n";
        //send email
        @mail($to,$subject,$htmlContent,$headers);

        $succMsg = 'Your mail request have submitted successfully.';
        $name = '';
        $email = '';
        $videolink = '';
        $file = ''; ##Look around here (Just a little guidance)##
        $message = '';
    else:
        $errMsg = 'Robot verification failed, please try again.';
    endif;
else:
    $errMsg = 'Please complete the reCAPTCHA form.';
endif;
else:
    $errMsg = '';
    $succMsg = '';
    $name = '';
    $email = '';
    $videolink = '';
    $file = ''; ##Look around here (Just a little guidance)##
    $message = '';
endif;

Re: PHP not submitting HTML form *file* correctly

Posted: Fri Mar 25, 2016 12:06 pm
by requinix
You can't use $_POST to access the file data.

POST method uploads