Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Problem with FOpen()

Status
Not open for further replies.

james0816

Programmer
Jan 9, 2003
295
US
I am trying to open a document in a new page. The code is very simple:

<?php
$fo = fopen("index.txt", "r");
fclose($fo);
?>

All I get is a blank screen.

Index.txt is in the same folder of the script and Allow_URL in the php.ini is set to on.

Am I not using it correctly?

thx
 
I t looks o.k, but nowehere in yur code are you outputting the file to screen.

You need to read the file once its open using [blue]fread()[/blue] and then output it, maybe using echo or something similar.

PHP online manual entry for fread.




----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.
 
You're using the function just fine. And your code is working as I would expect it to.

It's just the fopen() only opens the file. It doesn't read from the file or print the file.

If you're looking for a single function that will open a file and write it to the output buffer, I recommend that you look at readfile().



Want the best answers? Ask the best questions! TANSTAAFL!
 
no. that looks fine.

but you are not doing anything with the file once you have opened it.

if you want the contents, do not use fopen but instead use file_get_contents().

if you want to work with the file line by line or whatever use fread in conjunction with fopen.

generally take a look at the manual. start at fopen and follow the links to get a feel for the file system functions.
 
wow, three responses saying essentially the same thing. isn't the OP lucky !!!

I agree that readfile is better than file_get_contents for direct-to-buffer output.
 
hmmm....interesting....they got me on that one. I was reading through the fopen(). seemed easy enough.

I allow want to open files for read only access. no writting.
 
Wow. Aren't we helpfull.

----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.
 
Your code is fine. using the "r" parameter for in fopen, tells it to open the file for reading only.

----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.
 
owy...ok this is obviously not what I am looking for.

how do you open things like text documents, word docs, etc.?
 
I generally recommend that your script simply output a link directly to the file. That way, the server doesn't have to deal with the overhead of extra PHP scripts streaming files.

But if you must provide the file through a script, then I still recommend that you use readfile(). Just output the headers necessary to identify the file. See the script examples in the user-supplied comments on the page for readfile():


Want the best answers? Ask the best questions! TANSTAAFL!
 
kinda...i first test on a standard text file. it opened but ran everything togther instead of how formatted. i then tried a simple .pdf file. that just displayed garbage (i image its just the mark up language).
 
sry guys....after reading again...it seems to me that a majority of the discussion on this was referring to forced downloads instead of actually opening the file for viewing.
i may be missing the boat here.
 
that just displayed garbage
Did you take a look at the example scripts I was talking about earlier? Did you, as I and the example scripts pointed out, send the necessary HTTP headers?

If you don't send the Content-Type headers, all you will ever get is gobbledygook.



Want the best answers? Ask the best questions! TANSTAAFL!
 
The opening of files for viewing is handled by the browser. You would server the file as is, and hope that the user has an app installed to view the files. It is not done through PHP.

PHP can open files and read files, but they will be taken as plain text.

----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.
 
james

step back and explain carefully and fully what you are trying to achieve. give use case examples and be precise when you use words like "open". they have specific meanings to us and if you're not sure of them yourself, use a more descriptive term.

that way the likes of vacunita and sleipnir214 will be far better able to direct you to a solution that meets your requirements.

read the document that sleipnir's sig links you to. it will help you understand how best to get cogent answers in this and other forums.

Justin
 
ok...deep breath in.....deep breath out.....


I have several types of files stored in the folder ranging from .doc, .xls, .pdf, etc......

When a user clicks on a link, I am looking to "open/view" that particular document in a new window.

much like in HTML when you can simply say <a href="word.doc">

in my case...the user clicks on the link <a href="viewonly.php"> where this file opens/displays the file in question.

the reason that i am looking to do it this way is so that the file will open in a new window with no toolbar and right mouse click is disabled. i think this will make it truely view only.

does that help?
 
if you are going to display a document in a browser window you must make sure that the client can handle this document type. i.e. make sure that the right viewer application is installed at the client side. This, to put it mildly, is difficult to achieve with any reliability.

even if the client does have an application installed, it is difficult to make a browser insist on opening the doc inside itself rather than allowing a document to be opened natively.

disabling right-click is a javascript function. It can be subverted just by turning javascript off. in any event there is a copy of the file sitting in the user's cache which he can just pick up and use.

i know of no foolproof way of preventing subversion of documents intended to be read-only. word and pdf security can be hacked in no time. or just avoided. imaging solutions (displaying each page as a tiff or jpeg) are no good as the digital quality necessary to make the viewable on screen intrinsically makes them very easily ocr'able.

even a dynamnically generated pdf will not help much.

bottom line: if you don't want content subverted, don't make it available on the net. until we have a really good DRM matrix this will remain the case (imo, of course). and even then, the ocr solution is still out there...


 
exactly...this is just a local application that i am working on...not intended for internet use....just local to the office.
 
Again unless a user has a viewer plugin in his browser for the type of file, you are not going to go very far.
When you click on <a href=file.doc> the browser looks in its settings for an app to open the file, if it doesn't find one, it just downloads it. If it finds one it calls the proper app. to open the file such as Microsoft Word. Or Microsoft Excel. In some cases there are Plugins in the borwser that allow the file to opened inside the browser window such as Adobe's PDF viewer, or Excel's plugin for Internet Explorer.

Trying to develop a multi-format file viewer in PHP is going to be a challenge.

You'll need to not only open the file, but also parse and translate to something displayable in the browser. i.e HTML.

Otherwise serving the file with the appropriate headers will trigger the plugins if they are availble.

I













----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top