So I am creating a small file manager with download,delete and rename options. The thing is when i click on the download button next to the file i want to download, the whole webpage(html file) gets downloaded instead of the file. Note that i am sure that all files exist in my directory on my server. I tried to recognize the error in my code but i failed. Any help is greatly appreciated and that you in advance.
HTML code
<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>
