Jump to content

PHP download from my sql database


colm.88

Recommended Posts

Hi Guys,

 

I'm putting together a project which requires members of a site to upload files(mostly word files) into a mysql db.

I can get the files uploaded successfully but when it comes to downloading them i do have a bit of trouble. When i click on the download link i just get a file called 'download.php' instead of the actual file in the database.

 

I have been working on this link for the past two weeks and think maybe its time for some help, here's my code if you wouldn't mind having a look?

 

 

 

 

upload.php:

 

<?php

 

session_start();

 

?>

 

<html>

<head>

 

<link href="default.css" rel="stylesheet" type="text/css" media="screen" />

</head>

<body>

<!-- start header -->

<div id="header">

 

</div>

 

<div id="menu">

<ul>

<li><a href="#">Home</a></li>

<li><a href="member.php">Member</a></li>

<li><a href="logout.php">Log Out</a></li>

</ul>

</div>

 

 

<div id = "content">

 

<form method="post" enctype="multipart/form-data"/>

<input type="hidden" name="MAX_FILE_SIZE" value="2000000"/>

<input name="userfile" type="file" id="userfile"/>

<input type="submit" name="upload1"  id="upload1" value=" Upload "/>

</form>

</div>

<?php

if(isset($_POST['upload1']) && $_FILES['userfile']['size'] > 0)

{

 

$connect = mysql_connect ("localhost","root","") or die ("Couldn't connect!");

mysql_select_db("upload") or die ("Couldn't find db");

 

$fileName = $_FILES['userfile']['name'];

$tmpName  = $_FILES['userfile']['tmp_name'];

$fileSize = $_FILES['userfile']['size'];

$fileType = $_FILES['userfile']['type'];

 

$fp      = fopen($tmpName, 'r');

$content = fread($fp, filesize($tmpName));

$content = addslashes($content);

fclose($fp);

 

if(!get_magic_quotes_gpc())

{

    $fileName = addslashes($fileName);

}

 

$query = "INSERT INTO upload (name, size, type, content ) ".

"VALUES ('$fileName', '$fileSize', '$fileType', '$content')";

 

mysql_query($query) or die('Error, query failed');

 

 

echo "<br>File $fileName uploaded<br>";

}

?>

 

<div id = "nav">

</br>

<a href = "upload.php"><img src = "images\add-itemGreen.gif"/></a><b>Upload</b></br>

</br>

<a href = "download.php"><img src = "images\download.gif"/></a><b>Download</b></br>

</br>

<a href = "discussion.php"><img src = "images\chat-.gif"/></a><b>Discussion Board</b></br>

</div>

 

 

<div id="footer">

 

</div>

</body>

</html>

 

 

 

 

View.php:

<?php

ini_set('display_errors', E_ALL);

//include "open_db.inc";

 

$connect = mysql_connect ("localhost","root","") or die ("Couldn't connect!");

mysql_select_db("upload") or die ("Couldn't find db");

 

$sql = "SELECT * FROM upload ";

$sql .= "ORDER BY name ASC";

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

$rows = mysql_num_rows($result);

 

echo "<table>\n";

echo " <tr>\n";

echo "  <td>id</td>\n";

echo "  <td>name</td>\n";

echo "  <td>type</td>\n";

echo "  <td>size</td>\n";

echo "  <td>Description</td>\n";

echo "  <td> </td>\n";

echo " </tr>\n";

 

while ($rows = mysql_fetch_object($result))

  {

  echo " <tr>\n";

  echo "  <td>$rows->id</td>\n";

  echo "  <td>$rows->name</td>\n";

  echo "  <td>$rows->type</td>\n";

  echo "  <td>$rows->size</td>\n";

  echo "  <td>" . stripslashes($rows->description) . "</td>\n";

  echo "  <td>( <a href='download.php?id=$rows->id'>Download</a> )</td>\n";

  echo " </tr>\n";

 

  }

 

mysql_free_result($result);

mysql_close($connect);

?>

 

 

 

download.php:

<?php

ini_set('display_errors', E_ALL);

if (isset($_GET['id']))

{

$id_files = $_GET['id'];

// include "open_db.inc";

$connect = mysql_connect ("localhost","root","") or die ("Couldn't connect!");

mysql_select_db("upload") or die ("Couldn't find db");

 

  $sql = "SELECT id, name, type, size, content FROM upload WHERE id=$id";

 

  $result = @mysql_query($sql, $connect);

  $dataT = @mysql_result($result, 0, "bin_data");

  $name = @mysql_result($result, 0, "name");

  $size = @mysql_result($result, 0, "type");

  $type = @mysql_result($result, 0, "size");

 

  header("Content-type: $type");

  header("Content-length: $size");

  header("Content-Disposition: attachment; name=$name");

  header("Content-Description: PHP Generated Data");

  header("Content-transfer-encoding: binary");

  echo $dataT;

}

else

{

echo 'id_files not set';

 

}

?>

 

 

I'm thinking that it could be something to do with the bin_data on the download.php page.

Any help would be great.

 

Thanks

Link to comment
https://forums.phpfreaks.com/topic/224614-php-download-from-my-sql-database/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.