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
 
everyone can open all neccessary files in the browser that that is not an issue. Just opening the file itself at this time is the issue.
 
Opening the file is not the first issue. Sending the appropriate "Content-Type" header to the browser before streaming the file is.

james0816:
Previously, when you tried using readfile() with a PDF and got gobbledygook, did you set the "Content-Type" header before streaming the file? You script must do this for the script to be recieved and interpreted propery at the client end.



Want the best answers? Ask the best questions! TANSTAAFL!
 
I did copy a snippet from one of the examples listed (the forced download code). It didn't work for me:

$filename = 'test.xls';
$filename = realpath($filename);
$file_extension = strtolower(substr(strrchr($filename,"."),1));
switch ($file_extension) {
case "pdf": $ctype="application/pdf"; break;
case "doc": $ctype="application/msword"; break;
case "jpg": $ctype="image/jpg"; break;
}

header("Pragma: public");
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Cache-Control: private",false);
header("Content-Type: video/x-ms-wmv");
header("Content-Type: $ctype");
header("Content-Disposition: attachment; filename=\"".basename($filename)."\";");
header("Content-Transfer-Encoding: binary");
header("Content-Length: ".@filesize($filename));
set_time_limit(0);
@readfile("$filename") or die("File not found.");
 
the files that i put in were test.doc and test.pdf not the .xls that is currently listed.
 
Did you change this line:

header("Content-Type: video/x-ms-wmv");

To match the content-type of the file you're transferring? For example, the content-type of a PDF is "application/pdf", not "video/x-ms-mwv".



Want the best answers? Ask the best questions! TANSTAAFL!
 
hmmm...i see that....there are two lines for Content-Type and Cache-Control. Should there only be one line for each?
 
It works for me like so:
Code:
$filename = 'costos.pdf';
           $filename = realpath($filename);
           $file_extension = strtolower(substr(strrchr($filename,"."),1));
           switch ($file_extension) {
               case "pdf": $ctype="application/pdf"; break;
               case "doc": $ctype="application/msword"; break;
               case "jpg": $ctype="image/jpg"; break;
        }
           header("Pragma: public");
           header("Expires: 0");
           header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
           header("Cache-Control: private",false);
           header("Content-Type: $ctype");
[green]/*           header("Content-Disposition: attachment; 

filename=\"".basename($filename)."\";");*/[/green]
           header("Content-Transfer-Encoding: binary");
           header("Content-Length: ".@filesize($filename));
           set_time_limit(0);
           @readfile("$filename") or die("File not found.");

If I leave the green part in, it shows the download dialog box, asking me wether i want to save it or open it. if i comment it out, it automatically opens the file without asking. loading the PDF plugin into the browser.

BTW, the content type should of course match the type of file you are sending. so the line
header("Content-Type: video/x-ms-wmv");
should not be there because your file is not a video.
Everything else is O.K.

----------------------------------
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.
 
i do believe that has got it. as far as file types go....is there a "master" list of sorts that would tell me how to program for file extensions?
 
Actually, there is. But whether it's active or not depends on your PHP installation.

On my installation, which I compiled from source, I set up the MIMEtype function family ( which consists of one function: mime_content_type().

mime_content_type() can use the MIME magic file that ships with Apache to determine the MIME type of a file. On my system, sending a file is as simple as:

Code:
<?php
$filename = 'the_rain_in_spain.odt';
$mime = mime_content_type($filename);
header ('Content-type: ' . $mime);
header('Content-Disposition: attachment; filename="' . $filename . '"');
readfile($filename);
?>

It workds where $filename is equal to 'the_rain_in_spain.odt' (an OpenOffice document), 'the_rain_in_spain.pdf' (the PDF version of that document) or 'the_rain_in_spain.doc' (the Word version of the document). When I point my browser at the script, I get a prompt asking what I want done with the file and in each case my browser knows which program to use to open it (OpenOffice Writer, Acrobat or Word, respectively).



Want the best answers? Ask the best questions! TANSTAAFL!
 
Does the readfile() have a readonly option like fopen()? I've been looking around and haven't found anything as yet.
 
i think you're missing the point of the server-client interaction with web-clients. when you push something down the browser then you're not giving the client access to the server copy, you're creating a local copy on the client's computer with which the client will interact.

readfile() on the server is intrinsically read only, but as soon as the document is finished downloading to the client, the readfile() function terminates, the script terminates (probably) and the http connection to the server terminates.

when interacting like this there is nothing that the client can do to overwrite the server copy.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top