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
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");

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.