2016-08-05 92 views
0

我有上傳和下載腳本。在這裏,他們是:鏈接從目錄下載刪除

上傳

<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,但我仍然無法弄清楚。提前致謝。

+0

有些東西似乎離你的代碼。您標記爲「上傳」的第一個片段不包含用於上傳文件的表單,但它包含由「下載您的文件:」並由「foreach」循環生成的文件列表。第二個標記爲「下載」的代碼片段似乎與下載有關,但與用戶的交互方式並不明顯。這兩個片段都不清楚。也許你可以簡化你的代碼並顯示更多的上下文? – Julian

回答

0

只是讓他們鏈接:

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

謝謝,這很容易:) – r0uder