Current location: Hot Scripts Forums » Programming Languages » PHP » Code for Download Files


Code for Download Files

Reply
  #1 (permalink)  
Old 04-04-06, 05:58 PM
Moises Moises is offline
Newbie Coder
 
Join Date: Apr 2006
Posts: 9
Thanks: 0
Thanked 0 Times in 0 Posts
Question Code for Download Files

Hey Guys!..Thanks a lot for helping me!!...Now i've a new question for ya!!!

What can i do to make a little code for download files..i mean every single (not all, but most of them) website has the old mode like this:
http://thesite.com/downloads/the-file-name.mp3
And what i want is:
http://thesite.com/downloads.php?file=FILENAME.MP3

how can i do that???

Sorry for all those silly questions but, i'm new with PHP..hahaha!!
Reply With Quote
  #2 (permalink)  
Old 04-05-06, 02:07 AM
Nico's Avatar
Nico Nico is offline
Community Leader
 
Join Date: Sep 2005
Location: Spain
Posts: 8,075
Thanks: 11
Thanked 88 Times in 83 Posts
Reply With Quote
  #3 (permalink)  
Old 04-05-06, 01:17 PM
Moises Moises is offline
Newbie Coder
 
Join Date: Apr 2006
Posts: 9
Thanks: 0
Thanked 0 Times in 0 Posts
Thank u!!
Reply With Quote
  #4 (permalink)  
Old 04-05-06, 01:56 PM
Moises Moises is offline
Newbie Coder
 
Join Date: Apr 2006
Posts: 9
Thanks: 0
Thanked 0 Times in 0 Posts
Hey Nico...i've one more question...i don't get this... in this code

PHP Code:

<?

   
# Protect Script against SQL-Injections
   
$fileid=intval($_GET[id]);
   
# setup SQL statement
   
$sql " SELECT id, fileurl, filename, filesize FROM ibf_movies WHERE id=' $fileid' ";

   
# execute SQL statement
   
$res mysql_query($sql);

       
# display results
       
while ($row mysql_fetch_array($res)) {
       
$fileurl $row['fileurl'];
       
$filename$row['filename'];
       
$filesize$row['filesize'];

           
$file_extension strtolower(substr(strrchr($filename,"."),1));

           switch (
$file_extension) {
               case 
"wmv"$ctype="video/x-ms-wmv"; break;
               default: 
$ctype="application/force-download";
           }

// required for IE, otherwise Content-disposition is ignored
if(ini_get('zlib.output_compression'))
ini_set('zlib.output_compression''Off');

           
header("Pragma: public");
           
header("Expires: 0");
           
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
           
header("Cache-Control: private",false);
           
header("Content-Type: video/x-ms-wmv");
           
header("Content-Type: $ctype");
           
header("Content-Disposition: attachment; filename=\"".basename($filename)."\";");
           
header("Content-Transfer-Encoding: binary");
           
header("Content-Length: ".@filesize($filename));
           
set_time_limit(0);
           @
readfile("$fileurl") or die("File not found.");

}

$donwloaded "downloads + 1";

   if (
$_GET["hit"]) {
       
mysql_query("UPDATE ibf_movies SET downloads = $donwloaded WHERE id=' $fileid'");

}

?>
where I can specify the downloading directory 'cause i have my files in a diferent host!

and BTW that code is only for WMV files...and i have MP3, MPG and WMV... i found another one, but, i don't get it either:
PHP Code:

<?

$filename 
'dummy.zip';
           
$filename realpath($filename);

           
$file_extension strtolower(substr(strrchr($filename,"."),1));

           switch (
$file_extension) {
               case 
"pdf"$ctype="application/pdf"; break;
               case 
"exe"$ctype="application/octet-stream"; break;
               case 
"zip"$ctype="application/zip"; break;
               case 
"doc"$ctype="application/msword"; break;
               case 
"xls"$ctype="application/vnd.ms-excel"; break;
               case 
"ppt"$ctype="application/vnd.ms-powerpoint"; break;
               case 
"gif"$ctype="image/gif"; break;
               case 
"png"$ctype="image/png"; break;
               case 
"jpe": case "jpeg":
               case 
"jpg"$ctype="image/jpg"; break;
               default: 
$ctype="application/force-download";
           }

           if (!
file_exists($filename)) {
               die(
"NO FILE HERE");
           }

           
header("Pragma: public");
           
header("Expires: 0");
           
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
           
header("Cache-Control: private",false);
           
header("Content-Type: $ctype");
           
header("Content-Disposition: attachment; filename=\"".basename($filename)."\";");
           
header("Content-Transfer-Encoding: binary");
           
header("Content-Length: ".@filesize($filename));
           
set_time_limit(0);
           @
readfile("$filename") or die("File not found.");
?>
and...do i have to create a MYSQL DB ??? ... I don't understand that code...Sorry!

Last edited by Moises; 04-05-06 at 02:22 PM.
Reply With Quote
  #5 (permalink)  
Old 04-05-06, 03:12 PM
Nico's Avatar
Nico Nico is offline
Community Leader
 
Join Date: Sep 2005
Location: Spain
Posts: 8,075
Thanks: 11
Thanked 88 Times in 83 Posts
On a different host? I don't think PHP forced download is possible in this case.
Reply With Quote
  #6 (permalink)  
Old 04-05-06, 03:15 PM
Moises Moises is offline
Newbie Coder
 
Join Date: Apr 2006
Posts: 9
Thanks: 0
Thanked 0 Times in 0 Posts
Ok...i'll try to put them into the same host...but...where i can specify the Directory (in the first script)

and DO i have to create a SQL DB ???

Last edited by Moises; 04-05-06 at 03:18 PM.
Reply With Quote
  #7 (permalink)  
Old 04-05-06, 03:24 PM
Nico's Avatar
Nico Nico is offline
Community Leader
 
Join Date: Sep 2005
Location: Spain
Posts: 8,075
Thanks: 11
Thanked 88 Times in 83 Posts
Try this

PHP Code:

<?php


    $filename 
$_GET["file"];
    
    
$path_to_files "./wherever/";
    
    
$filename realpath($path_to_files $filename);
    
    
$file_extension strtolower(substr(strrchr($filename,"."),1));
    
    switch (
$file_extension
    {
        case 
"pdf"$ctype="application/pdf"; break;
        case 
"exe"$ctype="application/octet-stream"; break;
        case 
"zip"$ctype="application/zip"; break;
        case 
"doc"$ctype="application/msword"; break;
        case 
"xls"$ctype="application/vnd.ms-excel"; break;
        case 
"ppt"$ctype="application/vnd.ms-powerpoint"; break;
        case 
"gif"$ctype="image/gif"; break;
        case 
"png"$ctype="image/png"; break;
        case 
"jpe": case "jpeg":
        case 
"jpg"$ctype="image/jpg"; break;
        default: 
$ctype="application/force-download";
    }
    
    if (!
file_exists($filename)) 
    {
        die(
"NO FILE HERE");
    }
    
    
// required for IE, otherwise Content-disposition is ignored
    
if(ini_get('zlib.output_compression'))
    
ini_set('zlib.output_compression''Off'); 
    
    
header("Pragma: public");
    
header("Expires: 0");
    
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
    
header("Cache-Control: private"false);
    
header("Content-Type:"$ctype);
    
header("Content-Disposition: attachment; filename=\""basename($filename) ."\";");
    
header("Content-Transfer-Encoding: binary");
    
header("Content-Length: ".@filesize($filename));
    
set_time_limit(0);
    @
readfile($filename) or die("File not found."); 
    

?>
EDIT: No, you don't need a database.
Reply With Quote
  #8 (permalink)  
Old 04-05-06, 03:56 PM
Moises Moises is offline
Newbie Coder
 
Join Date: Apr 2006
Posts: 9
Thanks: 0
Thanked 0 Times in 0 Posts
OK thank u....but..hahaha..sorry for bothering u but...i've one more question...I promise this is the last question!!

with this:
PHP Code:

    {

        case 
"pdf"$ctype="application/pdf"; break;
        case 
"exe"$ctype="application/octet-stream"; break;
        case 
"zip"$ctype="application/zip"; break;
        case 
"doc"$ctype="application/msword"; break;
        case 
"xls"$ctype="application/vnd.ms-excel"; break;
        case 
"ppt"$ctype="application/vnd.ms-powerpoint"; break;
        case 
"gif"$ctype="image/gif"; break;
        case 
"png"$ctype="image/png"; break;
        case 
"jpe": case "jpeg":
        case 
"jpg"$ctype="image/jpg"; break;
        default: 
$ctype="application/force-download";
    } 
what if i want to put a MPG, MP3, AND WMV Extension???
Reply With Quote
  #9 (permalink)  
Old 04-05-06, 04:00 PM
Nico's Avatar
Nico Nico is offline
Community Leader
 
Join Date: Sep 2005
Location: Spain
Posts: 8,075
Thanks: 11
Thanked 88 Times in 83 Posts
Try adding this:

PHP Code:

case "mp3"$ctype="audio/mp3"; break;

case 
"wmv"$ctype="video/wmv"; break;
case 
"mpg"$ctype="video/mpg"; break; 
Reply With Quote
  #10 (permalink)  
Old 04-05-06, 04:30 PM
Moises Moises is offline
Newbie Coder
 
Join Date: Apr 2006
Posts: 9
Thanks: 0
Thanked 0 Times in 0 Posts
Thank u so much!!!!..It works now!!!...Thank so so so much!!
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Using PHP Echo in ASX Media Files sixflagsga PHP 8 02-16-06 01:56 PM
Zip Code search range errors Merovingian PHP 2 02-09-06 03:33 PM
PHP Code files single lined Kobra PHP 3 01-07-06 09:02 AM
How to sale php code to customer without giving him code pradeep_soft PHP 4 03-12-04 12:10 PM
Upload file to table so ONLY files tied to primary key are displayed in record? grafixDummy PHP 4 12-20-03 04:28 PM


All times are GMT -5. The time now is 07:07 AM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.