472,333 Members | 1,140 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,333 software developers and data experts.

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 4605
Dormilich
8,658 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,658 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,658 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

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

Similar topics

1
by: chuck amadi | last post by:
By the way list is there a better way than using the readlines() to > > >parse the mail data into a file , because Im using > >...
6
by: Foxy Kav | last post by:
Hi, another question from me again, i was just wondering if anyone could give me a quick example of reading data from a file then placing the data...
7
by: Joseph | last post by:
Hi, I'm having bit of questions on recursive pointer. I have following code that supports upto 8K files but when i do a file like 12K i get a...
8
by: Eric Lilja | last post by:
Hello, I had what I thought was normal text-file and I needed to locate a string matching a certain pattern in that file and, if found, replace that...
4
by: Jim Michaels | last post by:
after a file upload, $_FILES is not populated but $_POST is. what's going on here? $_POST=C $_POST=C $_POST=C...
2
by: Patrick | last post by:
Hi All, I have numerous files that look like the chunk below. From the date and time I calculate a year day value. For the example that value is...
15
by: Gan Quan | last post by:
I'm writing a c++ program that has many (100+) threads read/write files simultaneously. It works well if not considering the efficiency. The file...
7
by: snitu | last post by:
hello i am new user of this forum. i have a problem in javascript code. plz. give me idea about how to create tree using xml file (not using...
0
by: programming | last post by:
hi all, here is the php code! i am trying to use to 'edit' hyperlink to edit txt in member.txt. Problem is that it reads the values ok into an...
45
by: Dennis | last post by:
Hi, I have a text file that contents a list of email addresses like this: "foo@yahoo.com" "tom@hotmail.com" "jerry@gmail.com"...
0
better678
by: better678 | last post by:
Question: Discuss your understanding of the Java platform. Is the statement "Java is interpreted" correct? Answer: Java is an object-oriented...
0
by: Kemmylinns12 | last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and...
0
by: CD Tom | last post by:
This happens in runtime 2013 and 2016. When a report is run and then closed a toolbar shows up and the only way to get it to go away is to right...
0
by: CD Tom | last post by:
This only shows up in access runtime. When a user select a report from my report menu when they close the report they get a menu I've called Add-ins...
0
by: Naresh1 | last post by:
What is WebLogic Admin Training? WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge...
0
by: Matthew3360 | last post by:
Hi there. I have been struggling to find out how to use a variable as my location in my header redirect function. Here is my code. ...
2
by: Matthew3360 | last post by:
Hi, I have a python app that i want to be able to get variables from a php page on my webserver. My python app is on my computer. How would I make it...
0
by: AndyPSV | last post by:
HOW CAN I CREATE AN AI with an .executable file that would suck all files in the folder and on my computerHOW CAN I CREATE AN AI with an .executable...
0
by: Arjunsri | last post by:
I have a Redshift database that I need to use as an import data source. I have configured the DSN connection using the server, port, database, and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.