My download script downloads the source code of the page + the actual file

I’ve got a problem with a downloading script… So I made an uploading script -> it adds the files in a folder called Uploads and inserts id, filename and link into the database.But I can’t find a way to download the uploaded file. Here’s my download script:

[php] <?php
include(‘config.php’);
$dwquery = mysqli_query($db, “SELECT id, filename FROM files”);

     $id = intval($_GET['id']); 
         while ($row = mysqli_fetch_array($dwquery)) {
         	echo '<a href="download.php?id='.$row['id'].'">'.$row['filename'].'</a><br />';
        }
 
       $download = mysqli_query($db, "SELECT link FROM files WHERE id=$id");
        $link = mysqli_fetch_array($download);
    
        if($id != '') {
    	header('Content-disposition: attachment; filename='.$link[0]);
    	readfile($_SERVER['DOCUMENT_ROOT'] . '/project/' .$link[0]);
    	}
    
    ?>[/php]

It actually downloads the source code of download.php + the text / code that is inside the file I want to download. So it gets the echo:

[php] echo ’ Index / Upload / Download / Logout
';[/php]

And the downloaded file is:

[php] Index / Upload /
Download / Logout

test1.php
test2.txt

and here's the actual code of the file[/php]

The simplest way to download a file is to just give a url to the file, BUT that depends on the tyoe of file you are trying to download. What file types are you allowing to download?

php, html, txt, sql, css and rar files…

That is a bit odd to do. The rar extension will work how I stated. For the others, I would compress them and download them as zip, tar, or rar. The PHP files can be downloaded if you add the application type into a .htaccess file.

Well the php files can still be downloaded but as i stated it also adds the source of download.php to them ;/

Looks like the first query just gets a listing of all files.

[php] <?php
include(‘config.php’);

     $id = intval($_GET['id']); 
        if($id != '') {
       $download = mysqli_query($db, "SELECT link FROM files WHERE id=$id");
        $link = mysqli_fetch_array($download);
    
    	header('Content-disposition: attachment; filename='.$link[0]);
    	readfile($_SERVER['DOCUMENT_ROOT'] . '/project/' .$link[0]);
      } else {
          echo "Unknown file";
     }     
    
    ?>[/php]

So do away with that first query and the while loop

And where to list the uploaded files?

In another script, certainly not in the download script.

So there’s no way to do this in the download script?

[php] <?php
include(‘config.php’);

       $id = intval($_GET['id']); 
          if($id != '') {
         $download = mysqli_query($db, "SELECT link FROM files WHERE id=$id");
          $link = mysqli_fetch_array($download);

header(‘Content-disposition: attachment; filename=’.$link[0]);

readfile($_SERVER[‘DOCUMENT_ROOT’] . ‘/project/’ .$link[0]);
} else {
$dwquery = mysqli_query($db, “SELECT id, filename FROM files”);

           while ($row = mysqli_fetch_array($dwquery)) {
               echo '<a href="download.php?id='.$row['id'].'">'.$row['filename'].'</a><br />';
            }
      }

[/php]

That should do it

It shows blank page , but I’ve done it with the other way you suggested… Thanks a lot!

Sponsor our Newsletter | Privacy Policy | Terms of Service