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">
<label for="my_upload">Select the file to upload:</label>
<input id="my_upload" name="my_upload" type="file">
<input type="submit" value="Upload Now">

The php file I got from here:

  if (is_uploaded_file($_FILES['my_upload']['tmp_name'])) 
  	//First, Validate the file name
  		echo " File name is empty! ";
  	$upload_file_name = $_FILES['my_upload']['name'];
  	//Too long file name?
  	if (strlen($upload_file_name)>100)
  		echo " too long file name ";
  	//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 ";
    //Save the file
    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:


$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 = "


$to1 = "";
$subject = $q2 . $q1 . "Application";
$headers = "From:\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.