Page 1 of 1

My SQL and PHP code problems

Posted: Wed Jul 22, 2009 5:09 pm
by Alex-B
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

Re: My SQL and PHP code problems

Posted: Wed Jul 22, 2009 11:12 pm
by iamngk
i hope problem with following line.
Header("Content-Disposition: attachment; filename=$saveName");
readfile("$fileName");

$saveName should be the file name which downloading user will get [like download.xls]

$fileName should be the original file which exists in your server[if file exist in different folder mention filename with root path].


buy you given it wrong in your coding.. plz change this and try..

Re: My SQL and PHP code problems

Posted: Thu Jul 23, 2009 2:37 am
by Alex-B
Thanks for you reply !!

But I have more than 1 file in the /files/ folder. and therefore I needed to assign the random names to this files. But how to do it I do not have any idea.

Thanks again