I want to add a download link on my form when values are entered and submitted, then a link should create to download .docx file.
I've used unlink function before. It was working fine on local host but on live server it is displaying .docx file below the form with unreadable symbols.
I want to replace {{Name}}, {{last name}} and {{previews name}} with values entered by the user in the form. and then create a download link that will download .docx file with the particular values entered by the user. Also if the user will write previews name it will show in the file: (born: previews name)
if the user doesn't fill the previews name - it will just show first name and last name.
<!DOCTYPE html>
<html lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Employee Details</title>
</head>
<body>
<form method="post" action="#">
<input placeholder="First Name" type="text" name="firstname" />
<input placeholder="Last Name" type="text" name="lastname" />
<input placeholder="Previews Name" type="text" name="prevname" />
<input type="submit" name="submit" />
</form>
</body>
<?php
if(isset($_POST["submit"])) {
$fname = (string)$_POST["firstname"];
$lname = (string)$_POST["lastname"];
$prevname = (string)$_POST["prevname"];
$source = 'template.docx';
$full_path = $fname.'output.docx';
$file = $full_path;
//Copy the Template file to the Result Directory
copy($source, $full_path);
// add calss Zip Archive
$zip_val = new ZipArchive;
//Docx file is nothing but a zip file. Open this Zip File
if($zip_val->open($full_path) == true)
{
// In the Open XML Wordprocessing format content is stored.
// In the document.xml file located in the word directory.
$key_file_name = 'word/document.xml';
$message = $zip_val->getFromName($key_file_name);
// this data Replace the placeholders with actual values
$message = str_replace("{{Name}}", $fname, $message);
$message = str_replace("{{last name}}", $lname, $message);
$message = str_replace("{{previews name}}", '(born:'.$prevname.')', $message);
//Replace the content with the new content created above.
$zip_val->addFromString($key_file_name, $message);
$zip_val->close();
header("Content-Type: application/vnd.openxmlformats-officedocument.wordprocessingml.document");
header("Content-Disposition: attachment; filename=".$file);
readfile($file);
unlink($file);
exit();
}
}
?>
</html>
This code is saving .docx file in the same folder but I want a download link for it