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 Windows 系统cmd后提示“加载用户设置时遇到错误”
  • ¥30 vue router 动态路由问题
  • ¥15 关于#.net#的问题:End Function
  • ¥15 无法import pycausal
  • ¥15 VS2022创建MVC framework提示:预安装的程序包具有对缺少的注册表值的引用
  • ¥15 weditor无法连接模拟器Local server not started, start with?
  • ¥20 6-3 String类定义
  • ¥15 嵌入式--定时器使用
  • ¥20 51单片机学习中的问题
  • ¥30 Windows Server 2016利用兩張網卡處理兩個不同網絡