How to catch the form data associated with the file upload?

I have managed to make a simple webpage. A user can upload a file. I just want to add 3 data fields and save them too.

File Upload Tutorial
<form action="php/upload1file.php" method="post" enctype="multipart/form-data">
<p>
<label for="my_upload">Select the file to upload:</label>
<input id="my_upload" name="my_upload" type="file">
</p>
<input type="submit" value="Upload Now">
</form>

The php file I got from here:

<?php 
if ($_SERVER['REQUEST_METHOD'] == 'POST') 
{
  if (is_uploaded_file($_FILES['my_upload']['tmp_name'])) 
  { 
  	//First, Validate the file name
  	if(empty($_FILES['my_upload']['name']))
  	{
  		echo " File name is empty! ";
  		exit;
  	}
 
  	$upload_file_name = $_FILES['my_upload']['name'];
  	//Too long file name?
  	if (strlen($upload_file_name)>100)
  	{
  		echo " too long file name ";
  		exit;
  	}
 
  	//replace any non-alpha-numeric cracters in th file name
  	$upload_file_name = preg_replace("/[^A-Za-z0-9 \.\-_]/", '', $upload_file_name);
 
  	//set a limit to the file upload size
  	if ($_FILES['my_upload']['size'] > 1000000) 
  	{
		echo " too big file ";
  		exit;        
    }
 
    //Save the file
    $dest=__DIR__.'/uploads/'.$upload_file_name;
    if (move_uploaded_file($_FILES['my_upload']['tmp_name'], $dest)) 
    {
    	echo 'File Has Been Uploaded !';
    }
  }
}
?>

This works fine. It saves to /home/mywebpage/public_html/php/uploads. I will download with Python, because the PHP to do that is too complicated for me.

Now I want to modify to catch the User’s name email and any message. So I need 3 more <input type=‘text’ fields.

How do I get PHP to save the <input type=‘text’ fields in /home/mywebpage/public_html/php/uploads

Normally, I use something like this and send myself an email, but with a file involved, seems more complex:

<?php

$q1 = $_POST['surname'];
$q2 = $_POST['name'];
$q3 = $_POST['email'];
$q4 = $_POST['message'];

for ($i=1; $i <= 4; $i++) {

    if (${"q$i"} == '') ${"q$i"} = 'X'; 
}

$body = "

".$q1."
".$q2."
".$q3."
".$q4."
";

$to1 = "[email protected]";
$subject = $q2 . $q1 . "Application";
$headers = "From: [email protected]\r\n";
$headers .= 'Content-Type: multipart/form-data; charset=utf-8';
mail($to1, $subject, $body, $headers);
header("Location: email_success.php");
?>

How can I save $q1 to $q4 with the uploaded file in the same path? Best would be save them under the file name + ‘data’

you can save text files with file_put_contents, just lookup the documentation.

Thanks, I made some progress. I can now send a file + the data fields to a folder on the host.

Now I just need to get the next stage: put all that in an email and send it to me!

All seems a bit like reinventing the wheel, as everyone is doing this all the time, but I’ve gotta learn it I suppose!

For a beginner i would recommend using PHPMailer or Swiftmailer, as implementing the requirements involves reading a lot of RFCs.

Sponsor our Newsletter | Privacy Policy | Terms of Service