php Upload and Download File
Upload.php
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>upload</title>
</head>
<body>
<?php
$conn = mysqli_connect('localhost','root','','example');
if(isset($_POST['submit'])){
$fileName = $_FILES['file']['name'];
$fileTmpName = $_FILES['file']['tmp_name'];
$path = "files/".$fileName;
$query = "INSERT INTO filedownload(filename) VALUES ('$fileName')";
$run = mysqli_query($conn,$query);
if($run){
move_uploaded_file($fileTmpName,$path);
echo "success";
}
else{
echo "error".mysqli_error($conn);
}
}
?>
<table border="1px" align="center">
<tr>
<td>
<form action="upload.php" method="post" enctype="multipart/form-data">
<input type="file" name="file">
<button type="submit" name="submit"> Upload</button>
</form>
</td>
</tr>
<tr>
<td>
<?php
$query2 = "SELECT * FROM filedownload ";
$run2 = mysqli_query($conn,$query2);
while($rows = mysqli_fetch_assoc($run2)){
?>
<a href="download.php?file=<?php echo $rows['filename'] ?>">Download</a><br>
<?php
}
?>
</td>
</tr>
</table>
</body>
</html>
Download.php
<?php
if(!empty($_GET['file'])){
$fileName = basename($_GET['file']);
$filePath = "files/".$fileName;
if(!empty($fileName) && file_exists($filePath)){
//define header
header("Cache-Control: public");
header("Content-Description: File Transfer");
header("Content-Disposition: attachment; filename=$fileName");
header("Content-Type: application/zip");
header("Content-Transfer-Encoding: binary");
//read file
readfile($filePath);
exit;
}
else{
echo "file not exit";
}
}
File db.sql?
ReplyDeleteIt's simple 2 column table, you create as well
DeleteWhat's the table name for?
DeleteThis comment has been removed by the author.
ReplyDeletewhat is $_GET['file'] from
ReplyDeletewhen i download pdf file ... i need a password to open pdf .. help me please
ReplyDelete