Skip to content

Instantly share code, notes, and snippets.

@Sachinart
Last active February 15, 2017 10:20
Show Gist options
  • Save Sachinart/f5093cb8051234912a02a6f0c677215b to your computer and use it in GitHub Desktop.
Save Sachinart/f5093cb8051234912a02a6f0c677215b to your computer and use it in GitHub Desktop.
These codes may help you to make a text (or HTML, CSS, XML or any textual document) content to download in a desired format online. It is very helpful when you want to create a file with desired format on your mobile.
<html>
<head>
</head>
<body>
<form action="fileDload.php" method="POST">
<table>
<tr>
<td><textarea name="textarea" cols=50 rows=10 placeholder="Enter your content here"></textarea></td>
<td><tr><td><input type="text" name="ext" placeholder="Type Extension here"/></td></tr><tr><td><input type="submit" value="Download" "></td></tr></td>
<input type="text" name="filename" placeholder="Enter file name"/>
</tr>
</form>
</body>
</html>
<?php
$content = $_POST['textarea']; // Get content
$extension = $_POST['ext']; // Get extension name
$fname = $_POST['filename']; //Get file name
$dl_file = preg_replace("([^\w\s\d\-_~,;:\[\]\(\).]|[\.]{2,})", '', $content); // simple file name validation
$dl_file = filter_var($dl_file, FILTER_SANITIZE_URL); // Remove (more) invalid characters
$file =fopen('download.txt',"w"); // Create a file with name download.txt
fwrite($file,$content); // Write all content to download.txt
$ext = strtolower($extension); // Make extension lowercase
header('Content-Description: File Transfer'); // Optional Detail
header('Content-type: application/'.$ext); // Optional Detail
header('Content-length: '. filesize('download.txt')); // Optional Detail
//header('Content-Type: application/force-download');
header("Content-Disposition: attachment; filename=".$fname.'.'.$ext); //Replace download.txt with download.(Your extension type) file
readfile("download.txt"); // Make file downloadable
fclose($file); // Close the file
exit();
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment