Jump to content

BLOBS, Mysql and PHP


Jumparound

Recommended Posts

Hello, first of, this is my first post.

 

I'm pretty new to PHP and MySQL but I'm learning (at least, I hope ;-) )

 

What I aim to do with my page is download a mediumblob from my database...

Uploading is no problem, but downloading either gives me the blobfield as text back in my browser or tries to download is as download.php (which file is empty).

 

What I aim to learn is: How do I get the page to download the file and what did I do wrong :)

 

Hope somebody can help!

Cheers, sjoerd

 

Code to upload comes down to:

 

<?php
if(isset($_POST['upload']) && $_FILES['userfile']['size'] > 0)
{
$fileName = $_FILES['userfile']['name'];
$tmpName  = $_FILES['userfile']['tmp_name'];
$fileSize = $_FILES['userfile']['size'];
$fileType = $_FILES['userfile']['type'];
$email = $_POST['email'];
$voornaam = $_POST['voornaam'];
$achternaam = $_POST['achternaam'];
$fp      = fopen($tmpName, 'r');
$content = fread($fp, filesize($tmpName));
$content = addslashes($content);
fclose($fp);

if(!get_magic_quotes_gpc())
{
    $fileName = addslashes($fileName);
}

include 'library/config.php';
include 'library/opendb.php';

$query = "INSERT INTO upload (name, size, type, content, email, voornaam, achternaam ) ".
"VALUES ('$fileName', '$fileSize', '$fileType', '$content', '$email', '$voornaam', '$achternaam' )";

mysql_query($query) or die('Error, query failed');
include 'library/closedb.php';

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

 

Code for download

<?php
if(isset($_GET['id'])) 
{
// if id is set then get the file with the id from database
include 'library/config.php';
include 'library/opendb.php'; 
$id    = $_GET['id'];
$query = "SELECT name, type, size, content " .
         "FROM upload WHERE id = '$id'";

$result = mysql_query($query) or die('Error, query failed');
list($name, $type, $size, $content) = mysql_fetch_array($result);

header("Content-Disposition: attachment; filename=\"$name\"\n");
header("Content-Length: .filesize($size)");
header("Accept-Ranges: bytes");
header("Pragma: no-cache");
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Content-transfer-encoding: binary");

echo $content;

include 'library/closedb.php'; 
exit;
}

?> 

 

 

Link to comment
https://forums.phpfreaks.com/topic/212445-blobs-mysql-and-php/
Share on other sites

Hi Jumparound,

 

I would think that the problem with your download being blank is being caused by this line:

header("Content-Length: .filesize($size)");

 

You haven't closed your string before the filesize function (Also, are you not getting the size out of the database directly so don't need the filesize function?) and so it won't be a correct header.  It needs to be changed to:

header("Content-Length: ".$size);

 

Hope that helps!

Link to comment
https://forums.phpfreaks.com/topic/212445-blobs-mysql-and-php/#findComment-1106901
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.