PEAR Forum :: PHP Extension and Application Repository

PEAR Forum Forum Index
 FAQFAQ   SearchSearch   MemberlistMemberlist   RegisterRegister   ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 
MS Word document upload\download

 
Post new topic   Reply to topic    PEAR Forum Forum Index -> File Formats
View previous topic :: View next topic  
Author Message
shankar



Joined: 19 Sep 2007
Posts: 4

PostPosted: Wed Sep 19, 2007 8:25 pm    Post subject: MS Word document upload\download Reply with quote

Hi,

I have a problem in downloading word documents. In my application i upload some word documents to mysql database. When I try downloading the same, the file opens, but displays some junk characters all over the document. I use DB_DataObject. This is the snippet which I use to download the document.
<?php

$_REQUEST['resume_id'];
$dbImplInst=DBImpl::getInstance();
//echo "resume_id=".$_REQUEST['resume_id'];
$resume=$dbImplInst->getResume($_REQUEST['resume_id']);
$resume->fetch();
$name=$resume->resume_name;
$size=$resume->resume_size;
$type=$resume->resume_type;
$content=$resume->resume_file;

<html>
<head>
<title>Download File From MySQL</title>
<meta http-equiv="Content-Type" content="text/html;">
</head>
<body>
<?php
//echo $type;
header("Content-Length: $size");
header("Content-Type: $type");
header("Content-Disposition: attachment; filename=$name");
header("Content-Transfer-Encoding: binary");
echo $content;
exit;
?>
</body>
</html>

Can someone say, if I am missing out something.

Thanks
Shankar
Back to top
View user's profile Send private message Yahoo Messenger
mark



Joined: 07 Jan 2007
Posts: 1053

PostPosted: Wed Sep 19, 2007 10:27 pm    Post subject: Reply with quote

The junk characters might occur because you output HTML code before you send the contents of the Word file. The right way would be to output only the file contents, but not any HTML code.

But are you sure that you want to store the contents of the files in a database? It's recommended to store only filenames in the database, and the files itself as a normal file.
Back to top
View user's profile Send private message
shankar



Joined: 19 Sep 2007
Posts: 4

PostPosted: Thu Sep 20, 2007 8:07 am    Post subject: MS Word document upload\download-now asks for encoding Reply with quote

<?php
require_once('startup.php');
ob_start();
require_once('InitLog.php');
require_once('DBImpl.php');

$_REQUEST['resume_id'];
$dbImplInst=DBImpl::getInstance();
//echo "resume_id=".$_REQUEST['resume_id'];
$resume=$dbImplInst->getResume($_REQUEST['resume_id']);
$resume->fetch();
$name=$resume->resume_name;
$size=$resume->resume_size;
$type=$resume->resume_type;
$content=$resume->resume_file;

//echo $type;
header("Content-Length: $size");
header("Content-Type: $type");
header("Content-Disposition: attachment; filename=$name");
header("Content-Transfer-Encoding: binary");
echo $content;
exit;
?>
Mark, Thanks for your instant responce!!!
This is my complete code after removing the html stuff.I download it using firefox.It gets downloaded.Now when I try to open the file, word asks to select the encoding.When i select default, it displays junk data.

When I try the same with ie, the dowlaod dialog shows the file name as php file which executes this code instead of the actual document and shows error that unable to find the file.

Any clue on whats going wrong here????.IS the include files that use in this is causing problems.
_________________
Shankar
Back to top
View user's profile Send private message Yahoo Messenger
shankar



Joined: 19 Sep 2007
Posts: 4

PostPosted: Thu Sep 20, 2007 8:18 am    Post subject: MS Word document upload\download-now asks for encoding Reply with quote

Sorry with IE, get the proper file name in the download dialog.After downloading via IE when i try to open the doc it says unable to find the path.But when i use firefox to download i get a different error as mentioned in the previous post.Whats wrong with this code??????


Thanks
Shankar
_________________
Shankar
Back to top
View user's profile Send private message Yahoo Messenger
mark



Joined: 07 Jan 2007
Posts: 1053

PostPosted: Thu Sep 20, 2007 10:06 am    Post subject: Reply with quote

Does PHP maybe generate some notices (like "undefined index" etc.) that are inserted in the output? This could (very likely) confuse Word. A look with simple text editor into the generated file should tell you whether this happens.

I'm not sure, but is this really right?
Code:
header("Content-Transfer-Encoding: binary");


I don't know what the best value would be here, but specifying something like UTF-8 (depending on what you really send back, of course) might help.
Back to top
View user's profile Send private message
shankar



Joined: 19 Sep 2007
Posts: 4

PostPosted: Thu Sep 20, 2007 10:32 am    Post subject: Reply with quote

Hi Mark,

I tried removing the Content-Transfer-Encoding and also gave utf-8 without any success.As I said earlier.If i try with firefox,the download dialog shows the correct file name.But in ie it shows the php file name.So using firefox i dowload the file and try to open it.When i double click on it,it opens with word, but as error message as follows is dsiplayed

The document name or path not valid.Try these suggestions.
*Check the file permission for the document or drive.
*use the open dialog box to locate teh file.

So as per the second suggestion i tried opening the file from the open dialog.I still get the same dialog.

As per your suggestion I aslo tried opening the doc with wordpad.There are some junk contents displayed followed by the data which was in the document with all the formatting lost and again this is followed by junk contents.Infact this was similar to what i saw in MS word when i tried to open the downloaded document when the html code was there in my php file.

Thanks
Shankar
_________________
Shankar
Back to top
View user's profile Send private message Yahoo Messenger
Display posts from previous:   
Post new topic   Reply to topic    PEAR Forum Forum Index -> File Formats All times are GMT + 2 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum



PEAR Forum topic RSS feed 
Powered by phpBB © 2001, 2005 phpBB Group

Provided by Ministry of Web developement

'Actiemonitor' online projectmanagement software