If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > MySQL > unknown format while downloading pdf or images or compressed files from mysql

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 09-19-09, 06:18
praveendussa praveendussa is offline
Registered User
 
Join Date: Jul 2009
Posts: 6
unknown format while downloading pdf or images or compressed files from mysql

hello friends,

this is praveen.I am new to this forum.

I had one problem while downloading images or pf or compressed files
It display unknown format while downloading pdf or images or compressed files from mysql databse.i am using php to upload to database.

this is my table structure.

CREATE TABLE upload_download (
id INT NOT NULL AUTO_INCREMENT,
name VARCHAR(50) NOT NULL,
type VARCHAR(50) NOT NULL,
size INT NOT NULL,
content MEDIUMBLOB NOT NULL,
PRIMARY KEY(id)
);


is there any modification needed in table structure.?
waiting for ur reply.
thanks
Reply With Quote
  #2 (permalink)  
Old 09-21-09, 01:17
mnirwan mnirwan is offline
Registered User
 
Join Date: Sep 2009
Posts: 64
Not sure where you download it from. My assumption is that you have some script accessed from browser. The script selects from the database and return the output to browser with Content-Disposition header set somewhere.

If the above is correct, you may want to add Content-type header depending on the content.

If you already did, then there may be some corruption when inserting the data to database.

PS. Any reasons why you want to put the file in the database? It may be better to just keep it as a file and just put a reference to the file in database. DBMS was created to handle data and file system was created to handle file. I think performance wise, it's better to stick with what's best for each system.
Reply With Quote
  #3 (permalink)  
Old 09-21-09, 02:04
praveendussa praveendussa is offline
Registered User
 
Join Date: Jul 2009
Posts: 6
thanks for ur answer .
i am developing an application (using php) which downloads images or pdf or rar files from mysql database which is on my system(localhost).

while uploading is sucessfull ,it fails during download i,e images,pdf files are downloaded sucessfully but when try to open these files it shows corrupted.
Reply With Quote
  #4 (permalink)  
Old 09-21-09, 06:16
praveendussa praveendussa is offline
Registered User
 
Join Date: Jul 2009
Posts: 6
thanks mnirwan,

i didnt get u exactly what u mean . do u know about php.i will send php script if u know u can suggest me any modification needed.
Reply With Quote
  #5 (permalink)  
Old 09-21-09, 10:57
healdem healdem is online now
Jaded Developer
 
Join Date: Nov 2004
Location: out on a limb
Posts: 9,258
personally I wouldn't store stuff like PDF's in the db in the first place
im just wondering if there is a character in the data stream which has a different meaning between the db and the application and this is causing trouble. it could also be that the resource is too big for the db so the data is getting truncated

personally I'd store a URL to the resource in the db and then get the browser to download the resource itself.
__________________
I'd rather be riding my Versys or my Tiger 800 let alone the Norton
Reply With Quote
  #6 (permalink)  
Old 09-22-09, 09:50
mnirwan mnirwan is offline
Registered User
 
Join Date: Sep 2009
Posts: 64
Praveendussa : What I meant is exactly what healdem meant. Why would you want to store PDF or other files in DB?

DB was designed to store / retrieve data fast and easily. Although you can store files as data, it's not really what it was designed for.

File system was designed to store files. So why would you want to use DB as a storage for files, while file system was exactly designed for it?

If you insist on storing it on DB (eventhough it's inferior vs storing it as regular file), do paste the script. Maybe I'll be able to see what's wrong with it.
Reply With Quote
  #7 (permalink)  
Old 09-24-09, 00:54
praveendussa praveendussa is offline
Registered User
 
Join Date: Jul 2009
Posts: 6
thanks mnirwan,

i will try to use file system..thanks for ur reply.
Reply With Quote
  #8 (permalink)  
Old 09-24-09, 01:08
praveendussa praveendussa is offline
Registered User
 
Join Date: Jul 2009
Posts: 6
hello mnirwan,

before trying file system i want to make it workout with database,providing small application for users to download from my database .

i am sending php script for uploading and downloading files.

pasting my php script here.please go throw it.

below was my table structure

CREATE TABLE upload_download (
id INT NOT NULL AUTO_INCREMENT,
name VARCHAR(50) NOT NULL,
type VARCHAR(50) NOT NULL,
size INT NOT NULL,
content MEDIUMBLOB NOT NULL,
PRIMARY KEY(id)
);

below was the script for uploading files

<?php

include 'functions.inc';

$links = "<A HREF='upload.html'>Click here to proceed to the upload page</A>";

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'];

$fp = fopen($tmpName, 'r');
$content = fread($fp, filesize($tmpName));
$content = addslashes($content);

fclose($fp);

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

Connect_mysqldb();

$query = "INSERT INTO upload_download(name, size, type, content ) VALUES('$fileName', '$fileSize', '$fileType', '$content')";

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


echo "<br>File $fileName uploaded<br>";
echo '"<a href="upload.html">click here to go back to upload page </a>"';

}else{

echo "please select the file to download <br /> ";
echo $links;
}


?>

below was the script for downloading files

<?php

require "functions.inc";



// if id is set then get the file with the id from database

$downloadid = $_GET['id'];

connect_mysqldb();

echo $id;

$query = "SELECT name, type, size, content FROM upload_download WHERE id = '".$id."' ";

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

list($name, $type, $size, $content) = mysql_fetch_array($result);



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

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

header("Content-Type: application/octet-stream");

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

header("Content-Transfer-Encoding: binary\n");

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

echo $content;

exit;


?>

is there any modification needed in script.?
if u have any script send to this mail
praveen8632@gmail.com
waiting fro ur reply.thanks in advance
Reply With Quote
  #9 (permalink)  
Old 09-24-09, 03:27
mnirwan mnirwan is offline
Registered User
 
Join Date: Sep 2009
Posts: 64
If you're reading a binary file, why would you want to addslashes? That maybe your issue.
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On