且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

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

更新时间:2023-12-04 20:44:16

这将在onclick上执行,并且带有确认提示框,

 <?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>您的文件列在< / h3>
while($ file = readdir($ handle)){
if($ file!='。&& $ file!='..'){
echo'&lt ; target =_ blankhref ='。$ directory。'/'。$ file。'>'。 $文件'< a href =javascript:deletedata('。$ file。')>删除< / a>< br>';
}
}
}

if(isset($ _ REQUEST ['msg'])){
$ Message = $ _REQUEST ['msg'];
if($ Message == 1)echo文件删除成功;
else if($ Message == 1)echoFile not found;
}
?>
< script type =text / javascript > function deletedata(FileName){if(window.confirm(希望删除(按OK)或取消))window.location =SamePageURL.php?DelFile =+ FileName;}< / script>


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>';    
        }
    }
    }

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>