TheDegree0 Posted September 19, 2012 Share Posted September 19, 2012 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. Quote Link to comment https://forums.phpfreaks.com/topic/268562-download-file-with-php/ Share on other sites More sharing options...
kicken Posted September 19, 2012 Share Posted September 19, 2012 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"); Quote Link to comment https://forums.phpfreaks.com/topic/268562-download-file-with-php/#findComment-1379317 Share on other sites More sharing options...
TheDegree0 Posted September 19, 2012 Author Share Posted September 19, 2012 I have changed it like u sayed but it stills downloads a file with nothing in it. <?php $file = $_GET["FileName"]; header('Content-disposition:attachment;filename='.$file.''); readfile("admin/upload/$file"); ?> Quote Link to comment https://forums.phpfreaks.com/topic/268562-download-file-with-php/#findComment-1379341 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.