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
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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.