iSTORYA.NET

Go Back   iSTORYA.NET > Technology > Software & Games > Programming
: :

Register FAQ Members List Calendar Mark Forums Read

Programming :: Programming related discussions ::

Reply
LinkBack Thread Tools
  #1  
Old 09-01-2008, 05:52 PM
Junior Member
ichiriki is offline
ichiriki's Avatar
Join Date: Jul 2008
Posts: 53
Default Retrieving and Displaying Doc/txt files in php??..help.

hi guys,
im working on a project in fulfillment of my ojt, one of the task is to be able to upload doc/txt files in the database and then you can view all the uploaded doc/txt files. I am finished with the uploading part, i uploaded the file in a folder in the server while saving the path in the database but what concerns me most is how do can retrieve the file and display it using php??..

plssss if anyone can help me..badly need your help..
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2  
Old 09-01-2008, 06:44 PM
Senior Member
simoncpu is offline
simoncpu's Avatar
Join Date: Oct 2005
Posts: 698
Send a message via Yahoo to simoncpu
Default

I won't give you the full answer, since this is a homework question, but I'll give you some clues.

- Set your Content-Type header to "application/msword".
- use readfile() or other PHP functions to print out the file
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3  
Old 09-02-2008, 07:17 PM
Newbie
larrysbird is offline
Join Date: Aug 2008
Posts: 24
Default

Quote:
Originally Posted by ichiriki View Post
hi guys,
how do can retrieve the file and display it using php??..

plssss if anyone can help me..badly need your help..
All you have to do is locate the path of the files you intend to read then use the function given by the simoncpu or the likes. I think it would be better if you try coding it and show it to us here then we can help you debug if it will have some problems.

good job,
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4  
Old 09-03-2008, 02:18 PM
Junior Member
ichiriki is offline
ichiriki's Avatar
Join Date: Jul 2008
Posts: 53
Default

uhmmm...i have a similar php file in which i am to display an image file..it works..i just used fpassthru(); function to gather all the data in the file..is this still applicable in retrieving a doc file content??
heres my code in displaying a photo i uploaded:
Quote:
<?php
$image_folder = 'file:///C:/upload/photo/';
if (isset($_GET['name']) && basename($_GET['name']) == $_GET['name']) {
$pic = $image_folder.$_GET['name'];
if (file_exists($pic) && is_readable($pic)) {
$ext = substr($pic, -3);

switch ($ext) {
case 'jpg': $mime = 'image/jpeg'; break;
case 'gif': $mime = 'image/gif'; break;
case 'png': $mime = 'image/png'; break;
default: $mime = false;
}
if ($mime) {
header('Content-type: '.$mime);
header('Content-length: '.filesize($pic));
$file = @ fopen($pic, 'rb');

if ($file) {
fpassthru($file);
exit;
}

}

}
else
{echo "Sorry File is No Longer Readable";}

}
?>
ive just DL this code and edited it...
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #5  
Old 09-03-2008, 03:51 PM
Junior Member
ichiriki is offline
ichiriki's Avatar
Join Date: Jul 2008
Posts: 53
Default

ive tried this code:
Quote:
<? include('db.php'); ?>
<?
$doc_folder = 'file:///C:/upload/documents/cdn.doc';

$filename = $doc_folder;
header('Content-disposition: attachment; filename='.basename($filename));
header("Content-Type: application/msword");
header('Content-Length: '. filesize($filename));
readfile($filename);
?>
it opens the file in the ms word but not in the browser...i would like to print it in the browser f possible..help plss..
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #6  
Old 09-03-2008, 03:56 PM
C.I.A.
Tin_Tin is offline
Tin_Tin's Avatar
Join Date: Nov 2003
Gender: Male
Posts: 2,255
Default

Quote:
Originally Posted by simoncpu View Post
I won't give you the full answer, since this is a homework question, but I'll give you some clues.

- Set your Content-Type header to "application/msword".
- use readfile() or other PHP functions to print out the file
master ngano i change man ang content-type header?
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #7  
Old 09-03-2008, 03:57 PM
Senior Member
simoncpu is offline
simoncpu's Avatar
Join Date: Oct 2005
Posts: 698
Send a message via Yahoo to simoncpu
Default

I think the headers are case sensitive. Try using Content-Disposition instead of Content-disposition.

It's kindda weird though. According to the RFC, field names are supposed to be case-insensitive...

EDIT:
Try "Content-Type: application/octet-stream" diay. Lahi diay ang behavior sa imong browser kung application/msword. Sorry...
Last edited by simoncpu; 09-03-2008 at 04:09 PM.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #8  
Old 09-03-2008, 04:05 PM
Senior Member
simoncpu is offline
simoncpu's Avatar
Join Date: Oct 2005
Posts: 698
Send a message via Yahoo to simoncpu
Default

Quote:
Originally Posted by Tin_Tin View Post
master ngano i change man ang content-type header?
Mistress Tin_Tin,

Some web servers don't automatically detect the file type, so what happens is that random characters are instead sent to the browser.

Please see FAQ for more info...

[ simon.cpu ]
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #9  
Old 09-03-2008, 04:09 PM
C.I.A.
Tin_Tin is offline
Tin_Tin's Avatar
Join Date: Nov 2003
Gender: Male
Posts: 2,255
Default

Quote:
Originally Posted by simoncpu View Post
Mistress Tin_Tin,

Some web servers don't automatically detect the file type, so what happens is that random characters are instead sent to the browser.

Please see FAQ for more info...

[ simon.cpu ]
bro ko master uy. . . kasabot nko, tried coding it mu display siya ug mga garbage nga characters. . .
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #10  
Old 09-03-2008, 04:13 PM
Senior Member
simoncpu is offline
simoncpu's Avatar
Join Date: Oct 2005
Posts: 698
Send a message via Yahoo to simoncpu
Talking

Quote:
Originally Posted by Tin_Tin View Post
bro ko master uy. . .
Yikes... abi nako chix...
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #11  
Old 09-03-2008, 04:15 PM
Junior Member
ichiriki is offline
ichiriki's Avatar
Join Date: Jul 2008
Posts: 53
Default

its still the same...it will open it in my ms word..ive tried another code:
Quote:
$filename = "c:\\upload\documents\cdn.doc";
$handle = fopen($filename, "r");
$contents = fread($handle, filesize($filename));
echo $contents;
although it displays all the contents of the file in the browser, it is dis-organized, there were no new line in the display..
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #12  
Old 09-03-2008, 04:23 PM
Senior Member
simoncpu is offline
simoncpu's Avatar
Join Date: Oct 2005
Posts: 698
Send a message via Yahoo to simoncpu
Default

Bro,

Gi-edit nako akong previous post. Hehe. Try "Content-Type: application/octet-stream"...
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #13  
Old 09-03-2008, 04:29 PM
Junior Member
ichiriki is offline
ichiriki's Avatar
Join Date: Jul 2008
Posts: 53
Default

master sad ako tawag nim0..^__^
mu prompt ghapon xa og open file in ms word etc...
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #14  
Old 09-03-2008, 05:53 PM
Junior Member
ichiriki is offline
ichiriki's Avatar
Join Date: Jul 2008
Posts: 53
Default

nakuha na nko master...gamit nko tong code nga mu print og disorganized nga strings..
pero in efficient xa tungod i read nya per letter den iya i check kung \n char ba xa..
naa ra ako code:
Quote:
<? include('db.php'); ?>
<?
$doc_folder = 'file:///C:/upload/documents/';
if (isset($_GET['name']) && basename($_GET['name']) == $_GET['name']) {
$doc = $doc_folder.$_GET['name'];
$handle = fopen($doc, "rb");
if ($handle) {
while(!feof($handle))
{
$letter = fread($handle,1);
if($letter == "\n")
{echo "<br>";}
printf("%s",$letter);
}
}
else
echo "File Does not Exist";
}
?>

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #15  
Old 09-04-2008, 01:52 AM
C.I.A.
Tin_Tin is offline
Tin_Tin's Avatar
Join Date: Nov 2003
Gender: Male
Posts: 2,255
Default

pero wala nay garbage na mugawas bro. . ?

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply

Bookmarks