How Can I Download A File With Php Or Html?
I am making a file hosting website and am about 95% done but have one issue. When the user clicks their file to download it, the file just appears in the browser. I need to know a
Solution 1:
Usually every non-text file is downloaded automatically. For textfiles, you need to specify the header at the beginning of your script:
header("Content-Disposition: attachment; filename=".$file);
Solution 2:
This will performs on onclick, with confirmation alert box,
<?php$directory = 'uploads/' . $_SESSION['user'] . '/';
if(isset($_REQUEST['DelFile'])) {
$DeleteFile = $_REQUEST['DelFile'];
if(file_exists($directory.$DeleteFile)) {
@unlink($directory.$DeleteFile);
header("location:SamePageURL.php?msg=1");
} else header("location:SamePageURL.php?msg=2");
}
if ($handle = opendir($directory)) {
echo'<h3>Your files are listed below</h3>';
while ($file = readdir($handle)) {
if ($file != '.' && $file != '..') {
echo'<a target="_blank" href="'.$directory.'/'.$file.'">' . $file.' <a href="javascript:deletedata('.$file.')>Delete</a> <br>';
}
}
}
if(isset($_REQUEST['msg'])) {
$Message = $_REQUEST['msg'];
if($Message == 1) echo"File deleted sucessfully";
elseif($Message == 1) echo"File not found";
}
?><scripttype="text/javascript">functiondeletedata(FileName){if(window.confirm("Wish to Delete (Press OK) or Cancel")) window.location="SamePageURL.php?DelFile="+FileName;}</script>
Solution 3:
Solution 4:
You need redirect your users to a second script to handle request:
download.php?file=somepath/somefile.ext
<?php$file_url = $_GET['file'];
header("Content-disposition: attachment; filename=\"" . basename($file_url) . "\"");
readfile($file_url);
?>
Your link will be:
echo'<a href="download.php?file='.$directory.'/'.$file.'">' . $file.'<br>';
Post a Comment for "How Can I Download A File With Php Or Html?"