0

基本上我有输入框,用户提供文件名并单击下载。我想要的是如果用户输入不带引号的“.txt”,那么我需要下载的文件具有确切的文件扩展名,但在我的代码中它创建了具有给定扩展名的文件,如“txt”文件名。我想要文件名“.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);      
}?>
4

0 回答 0