doa24uk Posted September 20, 2007 Share Posted September 20, 2007 I cannot get my head around this. A user wishes to download a file (file.txt) from a directory. http://www.mysite.com/files/file.txt However an HTACCESS file in the /files/ directory contains the following info RewriteEngine on RewriteRule (.*)(txt|txt2)$ /files/dl.php?file=$1$2 [QSA] So it redirects the user to the dl.php script. The script (below) works perfectly HOWEVER, the resulting download is of whatever text is on the page at the time of downloading (ie. whatever echo's are spat out by dl.php) I can only think that when the download is forced via the script, it goes into a loop because the HTACCESS is telling it to go back to the dl.php page. Is there a way to fix it or use sessions or something so that it only goes to the dl.php page once? <?php $lol = $_GET[file]; $max_downloads = '1000'; // $max_downloads = '10000000'; $ip = addslashes($_SERVER['REMOTE_ADDR']); $todaysdate = date("Y-m-d"); $filex = filesize($lol); $path = addslashes($_SERVER['REQUEST_URI']); // DB connect mysql_connect("localhost", "USERNAME", "PASSWORD") or die(mysql_error()); mysql_select_db("DB") or die(mysql_error()); // Retrieve all data $query1 = mysql_query("SELECT * FROM table WHERE IP='$ip' AND DATE='$todaysdate'") or die(mysql_error()); $data=mysql_fetch_array($query1); if($data[iP]==''){ // store the record into $row // If no results, input new IP & DATE mysql_query("INSERT INTO table (IP, DATE, DOWNLOADED, EXEMPT) VALUES('$ip','$todaysdate','','0') ") or die(mysql_error()); echo 'no previous data - data now inputted'; }elseif ( $data[EXEMPT]=='1' ) { header('Content-type: application/octet-stream'); header('Content-Disposition: filename="'.$lol.'"'); header('Content-length: '.$lol); header('Cache-control: private'); exit(); }else{ // Do Nothing & carry on } // Calculate total downloads $total = $data[DOWNLOADED] + $filex; if ( $total >= $max_downloads ) { // This file will take them over their limit echo 'Run out of downloads & not EXEMPT - no download for you!'; // REDIRECT TO INFO PAGE } else { header('Content-type: application/octet-stream'); header('Content-Disposition: filename="'.$lol.'"'); header('Content-length: '.$lol); header('Cache-control: private'); // REDIRECT TO DOWNLOAD } ?> I'm so close to getting this working!!! Many thanks for all the help in advance! DoA Quote Link to comment Share on other sites More sharing options...
delirious1230 Posted September 29, 2008 Share Posted September 29, 2008 Possible to add sql code for this script??? Please Quote Link to comment Share on other sites More sharing options...
apulmca2k4 Posted September 29, 2008 Share Posted September 29, 2008 Hi, In http://www.mysite.com/files/file.txt, Have u access to files subdirectory. I meant if u have acces to that subdirectory, I can do it for u in just 2 mins. Quote Link to comment Share on other sites More sharing options...
delirious1230 Posted September 29, 2008 Share Posted September 29, 2008 Apul i added you on MSN can u accept me and we can solve problem?! Quote Link to comment Share on other sites More sharing options...
The Little Guy Posted September 29, 2008 Share Posted September 29, 2008 I use this: http://phpsnips.com/snippet.php?id=55 Quote Link to comment Share on other sites More sharing options...
delirious1230 Posted September 29, 2008 Share Posted September 29, 2008 Well this script doesnt limit downloads... Quote Link to comment Share on other sites More sharing options...
The Little Guy Posted September 29, 2008 Share Posted September 29, 2008 No, you would have to add that part, It just downloads the whole file, which is what I believe you were having troubles with right? Quote Link to comment Share on other sites More sharing options...
delirious1230 Posted September 29, 2008 Share Posted September 29, 2008 Yeah that needed apulmca2k4, i wanted to get his sql code than i would need that part too i gues Quote Link to comment Share on other sites More sharing options...
The Little Guy Posted September 29, 2008 Share Posted September 29, 2008 Give this a try: $lol = $_GET[file]; $max_downloads = '1000'; $ip = addslashes($_SERVER['REMOTE_ADDR']); $todaysdate = date("Y-m-d"); $filex = filesize($lol); $path = addslashes($_SERVER['REQUEST_URI']); // DB connect mysql_connect("localhost", "USERNAME", "PASSWORD") or die(mysql_error()); mysql_select_db("DB") or die(mysql_error()); // Retrieve all data $query1 = mysql_query("SELECT * FROM table WHERE `IP`='$ip' AND `DATE`='$todaysdate'") or die(mysql_error()); $data=mysql_fetch_array($query1); if(mysql_num_rows($query1) < $max_downloads){ mysql_query("INSERT INTO table (`IP`, `DATE`, `DOWNLOADED`, `EXEMPT`) VALUES('$ip','$todaysdate','','0') ") or die(mysql_error()); echo 'no previous data - data now inputted'; header("Pragma: public"); header("Expires: 0"); header("Cache-Control: must-revalidate, post-check=0, pre-check=0"); header("Content-Type: application/force-download"); header("Content-Type: application/octet-stream"); header("Content-Type: application/download"); header("Content-Disposition: attachment; filename=".basename($lol).";"); header("Content-Transfer-Encoding: binary"); header("Content-Length: ".filesize($lol)); @readfile($row['FileName']); exit(0); } Quote Link to comment Share on other sites More sharing options...
delirious1230 Posted September 29, 2008 Share Posted September 29, 2008 I get this: Duplicate entry '127.0.0.1' for key 1 what is wrong And second in previous post i meant sql code for phpmyadmin ... that is needed for script Quote Link to comment Share on other sites More sharing options...
The Little Guy Posted September 29, 2008 Share Posted September 29, 2008 my guess is that you have ip set to "Unique". is that correct? Quote Link to comment Share on other sites More sharing options...
delirious1230 Posted September 29, 2008 Share Posted September 29, 2008 Hmmm this is my current database... CREATE TABLE `downloaded` ( `filepath` varchar(255) NOT NULL, `IP` varchar(15) NOT NULL, `DATE` datetime NOT NULL, `DOWNLOADED` varchar(120) NOT NULL, `EXEMPT` varchar(120) NOT NULL, UNIQUE KEY `filepath` (`filepath`,`IP`), KEY `ipadres` (`IP`) ) ENGINE=MyISAM DEFAULT CHARSET=latin1; i have it under unique yes.... but i thought that is correct.... Quote Link to comment Share on other sites More sharing options...
thebadbad Posted September 29, 2008 Share Posted September 29, 2008 Sorry to run off topic, but what's up with you guys asking for his MySQL password? :-\ Quote Link to comment Share on other sites More sharing options...
delirious1230 Posted September 29, 2008 Share Posted September 29, 2008 none asked for his SQL password -.- i asked for code like that (CREATE TABLE `downloaded` ( `filepath` varchar(255) NOT NULL, `IP` varchar(15) NOT NULL,.............) he has different one... original one. I just modified it but it might be incorrect :S Quote Link to comment Share on other sites More sharing options...
The Little Guy Posted September 29, 2008 Share Posted September 29, 2008 DROP INDEX filepath ON downloaded Quote Link to comment Share on other sites More sharing options...
thebadbad Posted September 29, 2008 Share Posted September 29, 2008 Oh man, I'm sorry. Guess I was confused by Have u access to files subdirectory. I meant if u have acces to that subdirectory, I can do it for u in just 2 mins. Quote Link to comment Share on other sites More sharing options...
delirious1230 Posted September 29, 2008 Share Posted September 29, 2008 where to put this ??? Quote Link to comment Share on other sites More sharing options...
The Little Guy Posted September 29, 2008 Share Posted September 29, 2008 run it the same way you created that table. Quote Link to comment Share on other sites More sharing options...
delirious1230 Posted September 29, 2008 Share Posted September 29, 2008 I deleted filepath but its still same :S Duplicate entry '127.0.0.1' for key 1 Quote Link to comment Share on other sites More sharing options...
The Little Guy Posted September 29, 2008 Share Posted September 29, 2008 DROP INDEX IP ON downloaded Quote Link to comment Share on other sites More sharing options...
delirious1230 Posted September 29, 2008 Share Posted September 29, 2008 now i get Unknown column 'IP' in 'where clause' ...... i knew that ip must be in sql.... Maybe you got MSN or something that we can talk a lil bit faster ? Quote Link to comment Share on other sites More sharing options...
The Little Guy Posted September 29, 2008 Share Posted September 29, 2008 ALTER TABLE `downloaded` ADD `filepath` VARCHAR(255) NOT NULL ; ALTER TABLE `downloaded` ADD `IP` VARCHAR(15) NOT NULL ; Quote Link to comment Share on other sites More sharing options...
delirious1230 Posted September 29, 2008 Share Posted September 29, 2008 Ok now its okay, is it possible to contact over MSN, there is still something to fix ??? Quote Link to comment Share on other sites More sharing options...
delirious1230 Posted September 29, 2008 Share Posted September 29, 2008 Currently what i have in dl.php <?php // change this value below $cs_conn = mysql_connect('localhost', '****', '****'); mysql_select_db('*****', $cs_conn); $lol = $_GET[file]; $max_downloads = '10'; $ip = addslashes($_SERVER['REMOTE_ADDR']); $todaysdate = date("d-m-Y"); $filex = filesize($lol); $path = addslashes($_SERVER['REQUEST_URI']); // Retrieve all data $query1 = mysql_query("SELECT * FROM downloaded WHERE `IP`='$ip' AND `DATE`='$todaysdate' AND `EXEMPT`='$exempt AND `downloaded`='$downloaded''") or die(mysql_error()); $data=mysql_fetch_array($query1); if(mysql_num_rows($query1) < $max_downloads){ mysql_query("INSERT INTO downloaded (`IP`, `DATE`, `DOWNLOADED`, `EXEMPT`) VALUES('$ip','$todaysdate','','0') ") or die(mysql_error()); echo 'no previous data - data now inputted'; header("Pragma: public"); header("Expires: 0"); header("Cache-Control: must-revalidate, post-check=0, pre-check=0"); header("Content-Type: application/force-download"); header("Content-Type: application/octet-stream"); header("Content-Type: application/download"); header("Content-Disposition: attachment; filename=".basename($lol).";"); header("Content-Transfer-Encoding: binary"); header("Content-Length: ".filesize($lol)); @readfile($row['FileName']); exit(0); }else{ // Do Nothing & carry on } // Calculate total downloads $total = $data[DOWNLOADED] + $filex; if ( $total >= $max_downloads ) { // This file will take them over their limit echo 'Run out of downloads & not EXEMPT - no download for you!'; // REDIRECT TO INFO PAGE } else { header('Content-type: application/octet-stream'); header('Content-Disposition: filename="'.$lol.'"'); header('Content-length: '.$lol); header('Cache-control: private'); // REDIRECT TO DOWNLOAD } ?> and htacces RewriteEngine on RewriteRule (.*)(ace|avi|bin|bmp|doc|exe|gif|iso|jpg|mid|mp3|mpg|pdf|png|ppt|rar|txt|ttf|wav|xls|zip)$ /downloads/dl.php [QSA] and sqlbase code CREATE TABLE `downloaded` ( `DATE` datetime NOT NULL, `DOWNLOADED` varchar(120) NOT NULL, `EXEMPT` varchar(120) NOT NULL, `ip` varchar(15) NOT NULL ) ENGINE=MyISAM DEFAULT CHARSET=latin1; script is working in half way every time i download file it write this in database... I would like that every time that something is downloaded that update field downloaded or exempt... and that date would be correct... Quote Link to comment 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.