Welcome to WebmasterWorld Guest from

Forum Moderators: coopster & jatar k

Message Too Old, No Replies

Force Download and File Rename

12:12 pm on May 8, 2007 (gmt 0)

Junior Member

10+ Year Member

joined:July 21, 2006
votes: 0

hello to all
i have the php function for force download which also update the total number of downloads to MYSQL DB, the problem is i want to rename the orignal file name to new name, for example orignal name is file.mp3 (on the server) must be change to file-blah-blah.mp3 (when user want to download). here is my function below. i will realy love if someone solved my problem

$sql = "select * from table where tableID = $tableId";

$result = mysql_query($sql ,$db);

if ($myrow = mysql_fetch_array($result)) {

do {

$downloads = $myrow["downloads"] + 1;

} while ($myrow = mysql_fetch_array($result));


$sql = "update wallpaper set downloads = $downloads where tableID = $tableID";

$result = mysql_query($sql ,$db);

$newurl = "images/" . $size . "/" . $wallpaperid . ".jpg";

Header("Location: $newurl");

1:19 pm on May 8, 2007 (gmt 0)

Full Member

10+ Year Member

joined:Aug 3, 2005
votes: 0

Instead of actually redirecting to the actual file, you could just stream the file out using the appropriate headers.

Use fread() to read the file and write the binary content to the page. Use the appropiate headers to then force the download / display of this file.

something like
$myvar = contents of file chosen (fread())

header("Content-type: application/octet-stream");
header("Content-Disposition: attachment; filename=\"file.csv\"");

echo $myvar;

you could then change the filename to whatever you wanted.

check this link out for mimes
and this for fread()