Hi, I'm driving myself mad trying to work out what is wrong!
I'm trying to generate a filename, save text from a <textarea> into the file, then offer it for download to the user.
This works, except the text file is EMPTY when I download it! It's also 0 bytes.
The code I'm using is below. Any ideas why this is happening? It must be something simple I'm overlooking!
<?php
if ($action == "save") {
//CREATE FILENAME
srand((double) microtime() * 1000000);
$RandomExt = rand(0, 133);
$StaticFileName = "getURLcode";
$StaticExtension = ".txt";
$FilenameForDownload = $StaticFileName.$RandomExt.$StaticExtension;
//this means the file name will be e.g. getURLcode23.txt
//CODE FOR FILE CREATION
//$buffer is the textarea name which contains the text to be written
$pageValue = $buffer;
$fp = fopen($FilenameForDownload,'w');
$new = fwrite($fp,$pageValue);
fclose($fp);
//DOWNLOAD TO USER
//if ($action == "save") {
$download_size = filesize($FilenameForDownload);
header("Content-type: text/plain");
header("Content-Disposition: attachment; filename=$FilenameForDownload;");
header("Accept-Ranges: bytes");
header("Content-Length: $download_size");
@readfile($FilenameForDownload);
//}
//DELETE FILE FROM SERVER
unlink($FilenameForDownload);
}
?>