469,360 Members | 1,807 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,360 developers. It's quick & easy.

Get file contents

160 100+
hi to all

i am trying to get contents of a file in php.my code is
Expand|Select|Wrap|Line Numbers
  1. $filename = $filepath;
  2. $handle = fopen($filename, "r");
  3. $contents = fread($handle, filesize($filename));
  4. fclose($handle);
  5. echo $contents;
  6.  
but i am getting a problem that file contents is showing in the form of encode
like

Wؓђ?ђ?@ؓ@??ਚٲ䉁咀蓀,m0?? ???ђ$ؓؓؓؓؓؓؓBBؓؓؓ?ؓؓؓؓ??????????????? ???ؓؓؓؓؓؓؓؓؓ? ?: aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa ?h2 ؀h? Ӏ?,1?h?Я ?ཡ??"??#??$??%??Ђ?Ђ ?Ђf ?????????666666666vvvvvvvv v666666>66666666666666666 6666666666?6666666666?


can you please help me in getting correct content of file
Jun 4 '09 #1
15 4492
Dormilich
8,652 Expert Mod 8TB
what is the content supposed to be? what is character encoding in the file?
Jun 4 '09 #2
waqasahmed996
160 100+
that is simple word file means .doc file.and in file there is only content is "aaaaaaaaaaaaaaaaaaaaaaa"
Jun 4 '09 #3
Dormilich
8,652 Expert Mod 8TB
there's no chance properly reading a .doc with fread() (because of the included proprietary formatting). you may use the COM extension to read a Word file.
Jun 4 '09 #4
waqasahmed996
160 100+
hmm.okz.thanks.can you please refer me tutorial of reading content of a .doc file in php.
Jun 4 '09 #5
Dormilich
8,652 Expert Mod 8TB
since I can't use COM I can't advice one. some hints you'll get when reading through the manual, otherwise consult google.
Jun 4 '09 #6
waqasahmed996
160 100+
okz.Dormilich thanks again
Jun 4 '09 #7
Atli
5,058 Expert 4TB
Hi.

Just to point out...

If you just want to read a .doc file through PHP, like your code appears to be doing, you can do that without needing to actually use the file.
You would just leave it up to the client's browser to deal with how to use it.

For example;
Expand|Select|Wrap|Line Numbers
  1. <?php
  2. $filename = "/path/to/some/file.doc";
  3.  
  4. // Let the browser know it's getting a word document
  5. header("Content-Type; application/msword");
  6.  
  7. // Send the file
  8. readfile($filename);
  9. ?>
That would just feed the file through to the browser, which would most likely pop up a download dialog, or open it in a Word window.

If you actually wanted to use the word document in your PHP code, that's a much much harder task.

Microsoft don't really play well with others, and seeing as PHP is a open-source project...
Jun 4 '09 #8
waqasahmed996
160 100+
Thanks Atli

yes Atli i just want to show content of .doc file.editting is not required

Atli using this code also me get same problem.means output on browser is shown like this

 0@P`p€ 0@P`p€ 0@P`p€ 0
Jun 4 '09 #9
Atli
5,058 Expert 4TB
Ok.

Try adding the Content-Disposition and Content-Length headers.
That should tell the browser to download or open the file.

Expand|Select|Wrap|Line Numbers
  1. <?php
  2. $filename = "/path/to/some/file.doc";
  3.  
  4. // Let the browser know it's getting a word document
  5. header("Content-Type; application/msword");
  6. header("Content-Disposition: attachment; filename=". basename($filename)); 
  7. header("Content-Length: ". filesize($filename));
  8.  
  9. // Send the file
  10. readfile($filename);
  11. ?>
Jun 4 '09 #10
waqasahmed996
160 100+
thanks Atli,

by implement your above code file is downloaded. and downloaded file has not the correct content but like this

 0@P`p€ 0@P`p€ 0@P`p€ 0
Jun 4 '09 #11
Atli
5,058 Expert 4TB
Hmm...

Maybe you need to specify the transfer encoding to:
Expand|Select|Wrap|Line Numbers
  1. header("Content-Transfer-Encoding: binary");
By the way, which browser are you using?
And do you have Word installed on your PC?
Jun 4 '09 #12
waqasahmed996
160 100+
yes Atli i have word in my pc

and this problem exist in both mozila firefox and in IE. i am using latest versions of both.
Jun 4 '09 #13
Atli
5,058 Expert 4TB
I've tested this script on my server, sending a valid Word .doc file.
It works in all the major browsers.

Expand|Select|Wrap|Line Numbers
  1. <?php
  2. $filename = "test.doc";
  3.  
  4. // Make sure the file is valid
  5. if(!file_exists($filename) ) {
  6.     die("The file '{$filename}' does not exist.");
  7. }
  8. if(filesize($filename) == 0) {
  9.     die("The file is empty.");
  10. }
  11.  
  12. // Let the browser know it's getting a word document
  13. header("Content-Type; application/msword");
  14. header("Content-Disposition: attachment; filename=". basename($filename));
  15. header("Content-Length: ". filesize($filename));
  16. header("Content-Transfer-Encoding: binary");
  17.  
  18. // Send the file
  19. readfile($filename);
  20. ?>
If the file is coming out corrupted, there is most likely something wrong with your file.
Jun 4 '09 #14
waqasahmed996
160 100+
Atli can you please tell me which version of ms-word you are using

i am using ms-word 2007
Jun 4 '09 #15
Atli
5,058 Expert 4TB
I don't use M$ Office, but the file I was using locally to test this was created using Word 2007.

I just noticed, the content-type header in my example has a typo, which might have been messing with your server.
Expand|Select|Wrap|Line Numbers
  1. header("Content-Type; application/msword");
  2.  
  3. // It should be (note the : after Content-Type)
  4. header("Content-Type: application/msword");
See if that helps any. (My remote server complained a lot about this.)

I uploaded the example code (with the fixed content-type) to my remote test server.
You can test it here.
It uses a Word 97-2003 compatible .doc file created with OpenOffice. Should be the same format your 2007 Word creates.

If this doesn't work for you, the problem is with your PC, not the PHP code or the server.
Jun 4 '09 #16

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

6 posts views Thread by Foxy Kav | last post: by
7 posts views Thread by Joseph | last post: by
4 posts views Thread by Jim Michaels | last post: by
2 posts views Thread by Patrick | last post: by
reply views Thread by programming | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by suresh191 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.