PHP not submitting HTML form *file* correctly

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:

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;

It looks as if you are not doing anything with the file that is uploaded, just putting the user’s file path into an email.
You need to save the uploaded file to a location on your server, or attach it to the email.

“You need to save the uploaded file to a location on your server, or attach it to the email.”

That’s is what I am struggling with mate. I want it to be attached to the email, but I just have no clue how to, I tried doing $_FILES instead of putting $_POST and it still did not work.

When generating the email you need to generate a multipart/attachment section in the content that contains the encoded file content. It has nothing whatever to do with the upload of the file, it is entirely a matter of how you generate the content of the email.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.