Jump to content

[SOLVED] Download script


eadc707

Recommended Posts

Hello,

I have a download script in PHP that works just fine under a directory in my website but when I use the same script in a level higher it wants to download the php file containing the script instead of the actual file. The file's name is docs_download.php and so it tries to download this file instead of the file in my database. Here is the code:

 

<?php
/*
  * use this script to enable your visitors to download
  * your files.
  */
  require('config.php');
  require('admin/global.php');
  
if (isset($_GET['file']) || isset($_GET['filename']) ){
     $dir = 'files';
     if ( isset($_GET['file']) ){
         //Download by file ID
         $fid = $_GET['file'];
         $results = mysql_query("SELECT filename,filesize,dcount,dmonth FROM files WHERE id=$fid");
         if (mysql_numrows($results) == 0){
             echo "<b>File not found</b>";
             return;
         }
         $file = stripslashes(mysql_result($results,0,"filename"));
         // Combine the download path and the filename to create the full path to the file.
         $file = "$dir/$file";
         // Test to ensure that the file exists.
         if(!file_exists($file)) die("I'm sorry, the file doesn't seem to exist. It may have been removed.");
  }
addToDownloadCount($fid);
  }
else{
     echo "<b>File not found</b>";
     return;
}
  
  
     header('Content-Description: File Transfer');
     header('Content-Type: application/force-download');
     header('Content-Length: ' . filesize("$dir/$file"));
     header('Content-Disposition: attachment; filename=' . $file);
     readfile("$dir/$file");
     exit;

?>

 

Help please. Thanks

Link to comment
https://forums.phpfreaks.com/topic/165424-solved-download-script/
Share on other sites

Seems like a minor mistake made by the developer / author of that script, the tag before the ending PHP "<?" has an exit statement, which is displayed as

 

exit;

 

Try replacing the above code with

 

exit();

 

That's the right sequence used to be followed. :-\

 

Insert Quote

Seems like a minor mistake made by the developer / author of that script, the tag before the ending PHP "<?" has an exit statement, which is displayed as

 

Code: [select]

exit;

Try replacing the above code with

 

Code: [select]

exit();

That's the right sequence used to be followed. :-\

 

[/code]

 

that is incorrect.

 

 

 

from php.net's exit();

 


<?php

//exit program normally
exit;
exit();
exit(0);

//exit with an error code
exit(1);
exit(0376); //octal

?>

 

is your $dir correctly set? ~line 10.

Hello,

I have a download script in PHP that works just fine under a directory in my website but when I use the same script in a level higher it wants to download the php file containing the script instead of the actual file.

 

Did you modify it's locations for dependent scripts accordingly after you moved it?

  require('config.php');
  require('admin/global.php');

Do those paths still point to the right place now relative to it's new location?

 

@The Eagle, nothing will change with your modification.

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.