So I am creating a small file manager to manage uploaded files into a certain directory on my sever. I have created the attached code, but the issue is that when I click the download button, the webpage (html file) itself gets downloaded instead of the file in the directory that is supposed to be downloaded.
Please note that there is no upload file type restrictions, so there are any file type you can imagine in this directory.
I can't recognize the error in my code.
Any help will be highly appreciated and that you in advance
<html>
<head>
<title>My first PHP Page</title>
</head>
<body>
<table border="1">
<?php
$dir = 'uploads';
$files = scandir($dir);
sort($files);
$count = -1 ;
foreach ($files as $file) {
$v_download = "download_".$count;
$v_delete = "delete_".$count;
$v_rename = "rename_".$count;
$fileName = $file;
if ($file != '.' && $file != '..') {
echo "<tr>";
echo "<td>";
echo $count;
echo "</td>";
echo "<td>";
echo $file;
echo "</td>";
echo "<td>";
echo "<form action='' method='post'><input type='submit' value='Download' name='".$v_download."'/></form>";
if(isset($_POST[$v_download])) {
$filename = $_POST[$file];
header('Content-type: '.filetype($filename).'/'.pathinfo($filename, PATHINFO_EXTENSION));
header('Content-Disposition: attachment; filename="'.$filename.'"');
readfile('uploads/'.$filename);
exit();
}
echo "</td>";
echo "<td>";
echo "<form action='' method='post'><input type='submit' value='Delete' name='".$v_delete."'/></form>";
if(isset($_POST[$v_delete])) {
// Your php delete code here
echo "delete file : ".$file;
}
echo "</td>";
echo "<td>";
echo "<form action='' method='post'><input type='submit' value='Rename' name='".$v_rename."'/></form>";
if(isset($_POST[$v_rename])) {
// Your php rename code here
echo "rename file : ".$file;
}
echo "</td>";
echo "</tr>";
}
$count++;
}
?>
</table>
</body>
</html>
list.html