Log in

View Full Version : file renamed on download



Feckie
06-06-2009, 12:01 PM
I have a download link


echo "<a href=http://somewhere.com/{$row['Download']}>Download</a>";

is there a way I can rename the file when download is clicked..

ie: file name abc.mp3
changed to renamed.mp3

I have setup in the database $artist $title so would be nice to have it rename
to Artist-Title.mp3

hyde360
06-07-2009, 12:12 AM
It is possible and quite simple to be honest.

You will need a php file to fetch the data, such as artist name, track title, etc.
Let's say you have a file called "download.php" which will be the main target file to allow the end-user to download the file from your server to their local computer.

A simple download script would look like this (If you're getting data from MySQL):



<?php

$artist = $_GET['artist'];
$track = $_GET['track_title'];

//Include a connection to the MySQL Database

include("../dbconnect.inc.php"); //Connect File, also change location to the connect file

//We need to perform a simple MySQL Query

$query = mysql_query("SELECT * FROM music WHERE track_title = '$track' AND artist = '$artist' LIMIT 1") or die('Unable to perform the MySQL Query: ' . mysql_error());
$resul = mysql_query($query) or die('Unable to perform the MySQL Query: ' . mysql_error());
$numrows = mysql_num_rows($resul);

//We'll check to wherever the file actually exists in the db.

if ($numrows == 0){//failed
echo "Unable to find the file requested!";
}

//We need to extract some data from the db.

$rows = mysql_fetch_array($resul);

$artist = $rows["artist"]; //These values should be changed to the excate same name in the DB!
$track = $rows["track_title"];
$file_l = $rows["file_location"]; //This will be where the MP3 file is located either file system or DB.

//Now let's get our file.

header("Content-Type: application/force-download");
header("Content-Disposition: attachment; filename=".basename($file_l));
header("Content-Description: File Transfer");

?>


Now you say you want the file name renamed upon download? Here's a simple solution to that my friend.

Open up say Notepad and save this as ".htaccess" and upload it in the same folder where "download.php" is.



Options +FollowSymLinks
RewriteEngine on
RewriteRule (.*)-(.*).mp3$ download.php?artist=$1&track_title=$2


Then say a user clicked on the url below:

http://www.somesite.com/downloads/boo-hoo.mp3

It would download boo hoo song, jobs a good'n eh? :cool:

Hope this helps!

Feckie
11-21-2009, 03:12 PM
Sorry forgot to say thanks