基本上我有输入框,用户输入文件名并点击下载。我想要的是如果用户输入“.txt”没有引号,那么我需要下载文件具有确切的文件扩展名,但在我的代码中它创建了具有给定扩展名的文件,如“txt”filename。我想要文件名“.txt”
<?php
$folname = 'createfile';
$filepath = $_SERVER['DOCUMENT_ROOT'].$folname.'/';
if(isset($_POST['filename']) && !empty($_POST['filename'])){
fopen($_POST['filename'],'w');
downloadfile($filepath.$_POST['filename'],$_POST['filename']);
}else{
echo '
<h1>Enter Filename Or Extension, Download the empty file</h1>
<form method="post" action="">
<input type="text" name="filename" id="filename" />
<input type="submit" name="download" id="download" value="download"/>
</form>
';
}
function downloadfile($filepath,$filename){
header("Pragma: public");
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Content-Type: application/force-download");
header("Content-Disposition: attachment; filename=".$filename);
header("Content-Description: File Transfer");
@readfile($filepath);
unlink($filepath);
}?>