User submits email to download a file - php

 

Accept user details and allow downloading

 

This tutorial demonstrates how to let users submit contact details to download a file via link. When users clicks a download link, one popup form opened where user can submit his name, email-address and contact number. And automatically it starts file download within a browser.

 

index.php

 

 

<!DOCTYPE html>
<html lang="en">
	
	<head>
		

<!-- popupmodal JS Files -->

<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@4.6.2/dist/css/bootstrap.min.css">
<script src="https://cdn.jsdelivr.net/npm/jquery@3.6.1/dist/jquery.slim.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/popper.js@1.16.1/dist/umd/popper.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@4.6.2/dist/js/bootstrap.bundle.min.js"></script>
</head>
	
<body>
		
		

<div>
<br /><br /><br />
<h5> <a data-toggle="modal"   href="#myModal">Click here to Download </a></h5>

</div>


<!-------------------------Popup Form for Download  syllabus ----------------------------- -->

<div class="modal" id="myModal">
<div class="modal-dialog">
<div class="modal-content">
	
	<!-- Modal Header -->
	<div class="modal-header">   
		<h4 class="modal-title">Please Fill the Details</h4>
		<button type="button" class="close" data-dismiss="modal">&times;</button>
	</div>
	
	<!-- Modal body -->
	<div class="modal-body">
	<form action="downloadFile.php" method="post">							
			
	<input type="hidden" class="form-control" name="filename"  value="test-Doc1.pdf">							
			
	<div class="form-group">
	<input type="text" class="form-control" name="name" placeholder="Your Name" required>
	</div>
			
	<div class="form-group">
	input type="number" class="form-control" name="mobile" placeholder="Your Mobile" required>
	</div>
			
	<div class="form-group">     
	<input type="email" class="form-control" name="email"  placeholder="Your Email" required>
	</div>
			
	<div class="form-group"> 
	<input type="submit" class="btn btn-primary" name="download" value="Download Contents">
	</div> 
			
	</form>			
	
		
	<div class="modal-footer">
	<button type="button" class="btn btn-danger" data-dismiss="modal" >Close</button>
	</div>  
		
	</div>
</div>
</div>
</div> 

<!-------------------------End of the Popup Form for Download Syllabus ----------------------------- --> 

</body>

</html>	

 

 

 

downloadFile.php
 
<?php


if(isset($_POST['download']))
{

//Read form data
$filename =  $_POST['filename'];
$name =  $_POST['name'];
$email =  $_POST['email'];
$mobile =  $_POST['mobile']; 

/**  Handle  Form Data Store in database/ sent to email **/

chdir("pdfs");  // change director to pdfs to check file available in folder

//Check the file exists or not
if(file_exists($filename)) {
	
	//Define header information
	header('Content-Description: File Transfer');
	header('Content-Type: application/octet-stream');
	header("Cache-Control: no-cache, must-revalidate");
	header("Expires: 0");
	header('Content-Disposition: attachment; filename="'.basename($filename).'"');
	header('Content-Length: ' . filesize($filename));
	header('Pragma: public');
	
	//Clear system output buffer
	flush();
	
	//Read the size of the file
	readfile($filename);
	
	//Terminate from the script
	die();
	
}
else{
	echo "File does not exist.";
}
}
else
{
echo "Filename is not defined.";
}
?>

 

 

 

coding-zon-formsubmit-download

 

Comments

Popular posts from this blog

Using javascript pass form variables to iframe src

Creating a new PDF by Merging PDF documents using TCPDF

Import excel file into mysql in PHP