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 > Data Access, Manipulation & Batch Languages > PHP > 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:46
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 ,

this is praveen.i am new to this forum

I had one problem while downloading images,pdf or compressed files from mysql databse. It shows unknown format(as a corrupted file) while downloading files from database.

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
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