Current location: Hot Scripts Forums » Programming Languages » PHP » [SOLVED] PHP Download file w/ directories


[SOLVED] PHP Download file w/ directories

Reply
  #1 (permalink)  
Old 04-26-09, 11:11 PM
captainsquid captainsquid is offline
Newbie Coder
 
Join Date: Jan 2009
Posts: 14
Thanks: 0
Thanked 0 Times in 0 Posts
[SOLVED] PHP Download file w/ directories

Hello,
I am using a php script that will download a file so that the file location will not be seen....
download.php?filename=something.mp3
since multilple files are located in multiple folders, i want to make something like this:
download.php?artist=something&album=something&song =song.mp3
the script will then extract what ever was set for the artist, album, and song and find the file according to the the folders that are named that.
here is what i have so far, but there is something wrong with it..instead, the actual download.php file downloads, not the mp3. please help.
PHP Code:

<?PHP

    
// File Download Script
    // by James Warkentin
    
    // This script is provided without warrenty of any kind, nor may the author be held liable for 
    // for any problems which arise as a result of the use of this script.
    
    
    // Modify this line to indicate the location of the files you want people to be able to download
    // This path must not contain a trailing slash.  ie.  /temp/files/download
    
    
$download_path $_SERVER['DOCUMENT_ROOT'] . "/music/";
    
    
    
    
/* ###################################################################################*/
    /*                     DO NOT MODIFY THE SCRIPT BEYOND THIS POINT                     */
    /* ###################################################################################*/
    
    
$artist $_GET['artist'];
    
$album $_GET['album'];
    
$song $_GET['song'];
    
// Detect missing artist
    
if(!$artist) die("I'm sorry, you must specify a file name to download.");
    
    
// Make sure we can't download files above the current directory location.
    
if(eregi("\.\."$song)) die("I'm sorry, you may not download that file.");
    
$file str_replace(".."""$song);
    
    
// Make sure we can't download .ht control files.
    
if(eregi("\.ht.+"$artist)) die("I'm sorry, you may not download that file.");
    
    
// Combine the download path and the artist to create the full path to the file.
    
$file "$download_path/$artist/$album/$song";
    
    
// Test to ensure that the file exists.
    
if(!file_exists($file)) die("I'm sorry, the file doesn't seem to exist.");
    
    
// Extract the type of file which will be sent to the browser as a header
    
$type filetype($file);
    
    
// Get a date and timestamp
    
$today date("F j, Y, g:i a");
    
$time time();

    
// Send file headers
    
header("Content-type: $type");
    
header("Content-Disposition: attachment;song=$song");
    
header('Pragma: no-cache');
    
header('Expires: 0');

    
// Send the file contents.
    
readfile($file);
    

?>
thanks in advance.

Last edited by Nico; 04-27-09 at 03:41 AM.
Reply With Quote
  #2 (permalink)  
Old 04-27-09, 11:21 PM
captainsquid captainsquid is offline
Newbie Coder
 
Join Date: Jan 2009
Posts: 14
Thanks: 0
Thanked 0 Times in 0 Posts
got it...made some minor adjustments.
first off, the call would be something like this:

Code:
download.php?artist=sample&album=sample&song=something.mp3
now, the actual code:
PHP Code:

<?PHP


    
// File Download Script
    // by James Warkentin
    
    // This script is provided without warrenty of any kind, nor may the author be held liable 

for 
    
// for any problems which arise as a result of the use of this script.
    
    
    // Modify this line to indicate the location of the files you want people to be able to 

download
    
// This path must not contain a trailing slash.  ie.  /temp/files/download
    
    
$download_path $_SERVER['DOCUMENT_ROOT'] . "/music";
    
    
    
    
/* ###################################################################################*/
    /*                     DO NOT MODIFY THE SCRIPT BEYOND THIS POINT                     */
    /* ###################################################################################*/
    
    
$artist $_GET['artist'];
    
$album $_GET['album'];
    
$song $_GET['song'];
    
// Detect missing artist
    
if(!$artist) die("I'm sorry, you must specify a file name to download.");
    
    
// Make sure we can't download files above the current directory location.
    
if(eregi("\.\."$song)) die("I'm sorry, you may not download that file.");
    
$file str_replace(".."""$song);
    
    
// Make sure we can't download .ht control files.
    
if(eregi("\.ht.+"$artist)) die("I'm sorry, you may not download that file.");
    
    
// Combine the download path and the artist to create the full path to the file.
    
$file "$download_path/$artist/$album/$song";
    
    
// Test to ensure that the file exists.
    
if(!file_exists($file)) die("I'm sorry, the file doesn't seem to exist.");
    
    
// Extract the type of file which will be sent to the browser as a header
    
$type filetype($file);
    
    
// Get a date and timestamp
    
$today date("F j, Y, g:i a");
    
$time time();

    
// Send file headers
    
header("Content-type: $type");
    
header("Content-Disposition: attachment;filename=$song");
    
header('Pragma: no-cache');
    
header('Expires: 0');

    
// Send the file contents.
    
readfile($file);
    

?>
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
download jpg file in php tamil PHP 2 01-30-07 03:26 AM
Looking for a download manager PHP script bhxtyrant Script Requests 3 07-20-05 02:58 PM
Saving php generated file as html file GS300 PHP 0 12-29-04 03:34 AM
PHP download... niceguyonline Script Requests 3 03-06-04 05:41 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 08:32 AM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.