Forum Moderators: coopster

Message Too Old, No Replies

files download by using php scripting

how to make a link to enable files download with php script?

         

AlexB77

9:25 pm on Jul 20, 2009 (gmt 0)

10+ Year Member Top Contributors Of The Month



<a href="http://www._____.com/Links/go.php?urlid=file-homemortgage" target="_blank" class="shadow">Download Now</a>

what should I have in go.php file and how to create it to work with normal excel spreadsheets that I need to put on my website.

Thanks to all

andrewsmd

10:16 pm on Jul 20, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I'm really for sure what you want to do. If you want to make it prompt a download file box then it's this.

//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");

AlexB77

10:39 pm on Jul 20, 2009 (gmt 0)

10+ Year Member Top Contributors Of The Month



do I have to create a file go.php? and do I have to put this code in to this file?

andrewsmd

1:53 pm on Jul 21, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Yea if you are going to be passing multiple file names to it I would do something like this. Create a file called download.php and put this code in it

<?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.

AlexB77

2:42 pm on Jul 21, 2009 (gmt 0)

10+ Year Member Top Contributors Of The Month



Thanks for you reply,

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?

andrewsmd

3:24 pm on Jul 21, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Your logic should work something like this. If you only have one template file they need to download, then giving it a random name would do you no good because users could download that file as often as possible. If you have multiple different files that all need to be available for download then you can give them a random name for download using a database. You would need a table that would be structured something like
¦ id ¦ OriginalFileName ¦ RandomFileName ¦
Now when you save the file you would save it and name it with something random. Just google rand and then the php ascii characters list. Now when you save that file, use the functions I showed you earlier to get the original file name, then in the table you would insert the original file name and also the random file name, but the random file name is what you would save it as. When a user goes to download the file you append the url with the random file name
<a href="asioduiwefawthaoweo.pdf">Download Here</a>
Now in the php file that prompts the download. You would need code like this (pseudo)
<?php

//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.

AlexB77

2:16 pm on Jul 22, 2009 (gmt 0)

10+ Year Member Top Contributors Of The Month



Hi thanks for your reply!

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

andrewsmd

4:51 pm on Aug 3, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Try doing a var_dump on the isFile method and see if it returns true. That way you know if you have a valid path to your file. Like this
var_dump(isfile($fileName)); If that returns false then your file path is not valid.