...

View Full Version : Download File



Cerby
11-14-2007, 07:52 PM
Hello! The following code, uploads an MP3 file to a folder located in the root directory of the server, whilst adding the files path to a database.

For each record in the database, how do I display the path as a link and upon a link being clicked, download the file?


if (($_FILES["file"]["type"] == "audio/mpeg"))
{
if ($_FILES["file"]["error"] > 0)
{
echo "Return Code: " . $_FILES["file"]["error"] . "<br />";
}
else
{
echo "Upload: " . $_FILES["file"]["name"] . "<br />";
echo "Type: " . $_FILES["file"]["type"] . "<br />";
echo "Size: " . ($_FILES["file"]["size"] / 1024) . " Kb<br />";
echo "Temp file: " . $_FILES["file"]["tmp_name"] . "<br />";

if (file_exists("upload/" . $_FILES["file"]["name"]))
{
echo $_FILES["file"]["name"] . " already exists. ";
}
else
{
$file = ("upload/" . $_FILES["file"]["name"]);
move_uploaded_file($_FILES["file"]["tmp_name"],
"upload/" . $_FILES["file"]["name"]);
echo "Stored in: " . "upload/" . $_FILES["file"]["name"];

mysql_query("INSERT INTO Music (Path)
VALUES('$file')");
}
}
}
else
{
echo "Invalid file";
}

mic2100
11-15-2007, 07:50 PM
you need to create a recordset and output that into the link




//dbconnection
$hostname_conn = "localhost";
$database_conn = "database";
$username_conn = "username";
$password_conn = "password";
$conn = mysql_pconnect($hostname_conn, $username_conn, $password_conn) or trigger_error(mysql_error(),E_USER_ERROR);

mysql_select_db($database_conn, $conn);

$sql = "SELECT * FROM Music";
$rs_music = mysql_query($sql, $conn);


do
{

print("<a href='".$rows_rs_music['file']."'>".$rows_rs_music['file']."</a><br>")

}
while($rows_rs_music = mysql_fetch_assoc($rs_music));

Inigoesdr
11-15-2007, 08:41 PM
That do/while is going to cause an error in the first interation because $rows_rs_music isn't set yet. Use a while loop:

while($rows_rs_music = mysql_fetch_assoc($rs_music))
{
print("<a href='".$rows_rs_music['file']."'>".$rows_rs_music['file']."</a><br>");
}



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum