从目录下载\ delete的链接

时间:2016-08-05 15:09:32

标签: php

我上传和下载脚本。他们在这里:

上传

<html>
<head>
  <title> Result </title>
</head>
<body>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" >
<link rel="stylesheet" href="stylesheet.css">
<div class="container">
    <?php
    $uploaddir = '/var/www/uploads/';
    $uploadfile = $uploaddir . basename($_FILES['userfile']['name']);
    echo '<pre>';


    if (!empty($_FILES) && move_uploaded_file($_FILES['userfile']['tmp_name'], $uploadfile)) {
        echo "File was succesfully uploaded to ".$uploaddir;
    } else {
        echo "Something went wrong. Attach you file, please. \n";
    }


    //
    //if ($_FILES["userfile"]["size"] > 500000) {
    //    echo "Sorry, your file is too large.\n";
    //}

    $filelist = scandir($uploaddir, 1);

?>
    <p> Download your files: </p>

    <table>
        <?php foreach($filelist as $file): ?>
            <tr>
                <td> <?php echo $file; ?></td>
            </tr>
        <?php endforeach; ?>
    </table>

<?php
//debug
//   print_r ($filelist);
//   print_r($_FILES);
?>
</div>
</body>
</html>

下载

<?php 

// block any attempt to the filesystem
if (isset($_GET['file']) && basename($_GET['file']) == $_GET['file']) {
    $filename = $_GET['file'];
} else {
    $filename = NULL;
}


$error = 'Sorry, the file you are requesting is unavailable for ya.';

if (!$filename) {
// if  $filename is NULL or false display the message
    echo $error;
} else {
    $path = '/var/www/uploads/'.$filename;
    if (file_exists($path) && is_readable($path)) {
        $size = filesize($path);
        header('Content-Type: application/octet-stream');
        header('Content-Length: '.$size);
        header('Content-Disposition: attachment; filename='.$filename);
        header('Content-Transfer-Encoding: binary');
// display the error messages if the file can´t be opened
        $file = @ fopen($path, 'rb');
        if ($file) {
// stream the file and exit the script when complete
            fpassthru($file);
            exit;
        } else {
            echo $error;
        }
    } else {
        echo $error;
    }
}

现在我只能看到我上传的文件列表。

Like that

我想将它们设为 链接 ,以便我可以从目录中下载和删除(使用GET和取消链接)每个文件。我假设我必须使用foreach但我仍然无法弄明白。提前谢谢。

1 个答案:

答案 0 :(得分:0)

只需将它们链接起来:

<?php foreach($filelist as $file): ?>
    <tr>
        <td>
          <a href="download.php?file=<?php echo $file; ?>"><?php echo $file; ?></a>
        </td>
    </tr>
<?php endforeach; ?>