Jump to content

Download file with PHP


TheDegree0

Recommended Posts

Hello,

I have a script that it makes a list of files that are in a folder, and some of those files are printed instead of download when I click on the link so I tried to make a code

that forces it to download but for some reason it just downloads a file with the name of it but without anything in it, 0KB

 

Here's the code that forces it to download:

<?php
$file = $_GET["FileName"];
$location = "admin/upload/".$file;
print($location);

$myDirectory = opendir("admin/upload/");
header('Content-disposition:attachment;filename='.$file.'');
readfile($file);
closedir($myDirectory);
?>

 

And here's the a href that makes the user go to download.php:

echo '<TR><TD><a href="download.php?FileName='.$dirArray[$index].'" target="_blank">'.$dirArray[$index].'</a></td>';

 

Thanks in advanced and sorry for my bad english.

Link to comment
https://forums.phpfreaks.com/topic/268562-download-file-with-php/
Share on other sites

There is no need for you to call opendir and closedir, those are only necessary if you are listing files in a folder.

 

Your readfile() call is failing because it can't find the file.  You need to include the path of the file there as well:

readfile("admin/upload/$file");

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.