MyBB Community Forums

Full Version: File download
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Does anyone have a simple script in which downloads a certain file for example.

example.com?downloadscript=FILENAME

I have googled and just found really long ways, i am sure i found one with only a few lines of code, just downloading zip folders.

I know you can do <a href="example.com/asjfh.zip"> and it would still download it but i need to add a mysql query into it as well.

Thanks in advance.
(2011-04-02, 01:43 AM)itheme Wrote: [ -> ]Does anyone have a simple script in which downloads a certain file for example...

Some of these are simple, and most are free:
http://www.hotscripts.com/category/php/s...ad-systems
  • Is it faster to evaluate and test those, or just write your own? Toungue

...need to add a mysql query into it as well...
I would probably just write my own with each possible result building a dynamic link
(i.e Simple example:
<?php
 // ... query here ...
while($row = mysql_fetch_array($result, MYSQL_ASSOC))
{
// ! needs to be checked for 'simple' mistakes !!
$file_name = $row['my_var'];
echo "<a href=\"example.com/$file_name.zip\"><br />" ;
}

?>




Just threw this together in notepad, not tested:
<?php

$sqlconnect = mysql_connect("localhost","username","password");
if (!$sqlconnect)
  {
  die("Could not connect to MySQL: " . mysql_error());
  }

mysql_select_db("database", $sqlconnect);


$download = $_GET["download"];

if (!$download) {
	die("No file listed!");
} else {
	mysql_query("UPDATE table SET field = 'value' WHERE file = '$download'");
	mysql_close($con);
	header('Location: ../files/'.$download.');
}

?>

Put this code into download.php and then put the files you want to download into the /files/ folder. To access "asjfh.zip" you would go to: example.com/download.php?download=asjfh.zip
(2011-04-02, 01:43 AM)itheme Wrote: [ -> ]...mysql query into it as well....

What kind of query?
Joe's example will work great depending on what the query is for.


/ Congrats on your new addition to the family KuJoe. Smile
Just a simple UPDATE query nothing special thanks guys, i will test joes out Big Grin
I just wanted to follow-up and mention I tested the script I posted above and have altered it to do exactly what I was looking for here.
I use this:
function download( $file )
{
    $path = './storage/files/';
    
    header( 'Content-disposition: attachment; filename='.$file );
    header( 'Content-type: application/zip' );
    header( 'Cache-Control: no-cache, must-revalidate' );
    header( 'Content-length: '.filesize( $path.$file ) );
    
    // Get IP
    $ip = $_SERVER['REMOTE_ADDR'];
    
    // Write to log
    @file_put_contents( './something.txt', date(DATE_RFC850).' - '.$file.' - '.$ip."\n", FILE_APPEND );
    
    readfile( $path.$file );
    
    exit;
}

I've been meaning to add pause/resume support but I've been busy with other things. Toungue