Forum Moderators: coopster
//take a note I don't know what the mime type for an
//xls file is, you will have to look on your server
//and make sure it is supported.
//alternatively you could try this
//Header("Content-type: application/octet-stream");
Header("Content-type: application/xls");
Header("Content-Disposition: attachment; filename=\yourFileHere.xls\"");
readfile("yourFileHere.xls");
<?php
$fileName = $_GET['urlid'];
Header("Content-type: application/xls");
Header("Content-Disposition: attachment; filename=$fileName");
readfile("$fileName");
?>
Then on your page where you want the download now link just do it the way you have it posted in your first post. I would make a note though: you may want to give the file names a random name to download from because hackers could easily overload your site with download requests.
I have a folder called /files/ were I keep all files for download. Now all files have an extention .xls or .pdf (ex. purchase-order-template.xls).
Question is how do I need to give the file random name if the name of the file is purchase-order-template.xls and at the same time be able to use your script to enable download, and aslo do I have to use separate script for other files or I only have to define random name and do it this way?
//connect to your database
//get the original file name
//where your $_GET['urlid'] =
//RandomFileName and set it equal
//to $saveName
//this will be equal to the
//original file name you get from
//the db
$saveName = "something";
$fileName = $_GET['urlid'];
Header("Content-type: application/xls");
Header("Content-Disposition: attachment; filename=$saveName");
readfile("$fileName");
?>
Start on that and post back it you have any troubles.
I will explain myself:
I have created table in MySQL called it “files” that contains names of the original files on my server and the random names assigned to each of this files:
IDOriginalFileNameRandomFileName
001Purchase-Order-Template.xls file-purchaseorder
0025-year-financial-plan-manufact.xlsfile-businesplan
002Beverage-StockTake-Template.xlsfile-beveragestocktake
all original files are in the folder called /files/.
I have also used some PHP script to enable users to download each file and named it download.php
This is the script that I have used:
<?php
// Connects to your Database
mysql_connect("serveraddress", "username", "password") or die(mysql_error());
mysql_select_db("username") or die(mysql_error());
$data = mysql_query("SELECT * FROM files")
or die(mysql_error());
//get the original file name
//where your $_GET['urlid'] =
//RandomFileName and set it equal
//to $saveName
//this will be equal to the
//original file name you get from
//the db
$saveName = "OriginalFileName";
$fileName = $_GET['urlid'];
Header("Content-type: application/xls");
Header("Content-Disposition: attachment; filename=$saveName");
readfile("$fileName");
?>
Now when I am placing a simple link on the page were user can download the file from in this format:
<a href="http://www._____.com/download.php?urlid=file-purchaseorder" target="_blank" class="shadow">Download Now</a>
when you click on the Download Now link the process starts running but you cannot get the actual file at the end.
I think I do have an error some ware in the script but cannot figure it out. could you please help me a little and could you actually use the names that I have provided so I can see what goes were?
Thanks in advance