eadc707 Posted July 10, 2009 Share Posted July 10, 2009 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 More sharing options...
The Eagle Posted July 10, 2009 Share Posted July 10, 2009 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. :-\ Link to comment https://forums.phpfreaks.com/topic/165424-solved-download-script/#findComment-872456 Share on other sites More sharing options...
seventheyejosh Posted July 10, 2009 Share Posted July 10, 2009 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. Link to comment https://forums.phpfreaks.com/topic/165424-solved-download-script/#findComment-872475 Share on other sites More sharing options...
xtopolis Posted July 10, 2009 Share Posted July 10, 2009 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. Link to comment https://forums.phpfreaks.com/topic/165424-solved-download-script/#findComment-872478 Share on other sites More sharing options...
seventheyejosh Posted July 10, 2009 Share Posted July 10, 2009 and is $dir on ~line 10 correct? Link to comment https://forums.phpfreaks.com/topic/165424-solved-download-script/#findComment-872481 Share on other sites More sharing options...
eadc707 Posted July 11, 2009 Author Share Posted July 11, 2009 Thanks so much guys. I figured it out. I tried some of your replies but the only thing that made it work was that I left out the "$dir/" in line 35. header('Content-Length: ' . filesize("$dir/$file")); However I don't understand why this makes the code fail. Link to comment https://forums.phpfreaks.com/topic/165424-solved-download-script/#findComment-873305 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.