Is it possible to create file.txt on the fly download it?
The file should be stored in backup/process/_pro_links_date.txt
but when I download the file, it has no .txt extension and it was store on my folder.
HTML
<a href="?down=load">File</a>
PHP
if ( $_GET['down'] == 'load' ) {
$date = date( 'Y-m-d H:i:s' );
$myFile = "backup/process/_pro_links_$date.txt";
$fh = fopen($myFile, 'w') or die("can't open file");
$stringData = "Title - Url - Live Date - Process Date \r";
fwrite($fh, $stringData);
$sql = mysql_query("SELECT * FROM k_addlinks WHERE user_code = '".$_SESSION['user_code']."' ") or die (mysql_error());
while ($row = mysql_fetch_array($sql)){
$dated = date("m/j/y g:i",strtotime( $row["date"] ));
if($row["spider_date"] == 'never'){
$sdate = $row["spider_date"];
}else{
$sdate = date("m/j/y g:i",strtotime( $row["spider_date"] ));
}
$stringData = $row['pro_name'] . $row['customDomain'] . $dated . $sdate . '\r';
}
fwrite($fh, $stringData);
fclose($fh);
$path = "backup/process/_pro_links_$date.txt";
$name = "_pro_links_$date.txt";
header("Content-Type: text/plain");
header("Content-Length: " . filesize($path));
header('Content-Disposition: attachment; filename='.$name);
readfile($path);
}
what is wrong with my codes?
I see the issue. In your
Content-Dispositionheader, you set the filename, but your filename contains spaces since you created it usingdate("Y-m-d H:i:s"). So you need to surround the filename with quotes in that header:Alternatively, remove the spaces from
$datebefore using it in the filename: