dongrenzheng1619 2013-12-16 07:54
浏览 171
已采纳

如何使用PHP或HTML下载文件?

I am making a file hosting website and am about 95% done but have one issue. When the user clicks their file to download it, the file just appears in the browser. I need to know a way where I can define the $file variable in the while loop to be downloadable. The variable that I need to make downloadable is surrounded by asterisks(*)

LOOP:

$directory = 'uploads/' . $_SESSION['user'] . '/';

    if ($handle = opendir($directory)) {
    echo '<h3>Your files are listed below</h3>';    

    while ($file = readdir($handle)) {
        if ($file != '.' && $file != '..') {
        echo '<a href="'.$directory.'/'.$file.'">' . *$file*.'<br>';    
        }
    }
    }
  • 写回答

4条回答 默认 最新

  • dosin84644 2013-12-19 05:16
    关注

    This will performs on onclick, with confirmation alert box,

    <?php
    
    $directory = 'uploads/' . $_SESSION['user'] . '/';
    
    if(isset($_REQUEST['DelFile'])) {
        $DeleteFile = $_REQUEST['DelFile'];
        if(file_exists($directory.$DeleteFile)) {
            @unlink($directory.$DeleteFile);
            header("location:SamePageURL.php?msg=1");
        } else header("location:SamePageURL.php?msg=2");
    }
    
    if ($handle = opendir($directory)) {
    echo '<h3>Your files are listed below</h3>';    
        while ($file = readdir($handle)) {
            if ($file != '.' && $file != '..') {
                echo '<a target="_blank" href="'.$directory.'/'.$file.'">' . $file.' <a href="javascript:deletedata('.$file.')>Delete</a> <br>';  
            }
        }
    }
    
    if(isset($_REQUEST['msg'])) {
        $Message = $_REQUEST['msg'];
        if($Message == 1) echo "File deleted sucessfully";
        else if($Message == 1) echo "File not found";
    }
    ?>
    <script type="text/javascript">function deletedata(FileName){if(window.confirm("Wish to Delete (Press OK) or Cancel"))  window.location="SamePageURL.php?DelFile="+FileName;}</script>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?