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 SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

fopen() problem - will not open a file

Status
Not open for further replies.

james0816

Programmer
Jan 9, 2003
295
0
0
US
very simple:

<?php
$fh=fopen("Logs/test.txt","r") or die ("can't open file");
?>


Doesn't open the file. blank screen. File is in the location.

Am i missing the big picture here?
 
Code:
$filename="spcc.doc";
$path="C:\\";

header("Cache-Control: private",false);
header('Content-Type: application/octet-stream');
header("Content-Type: image/jpeg");
header("Content-Type: application/pdf");
header("Content-Type: application/msword");
header("Content-Type: application/vnd.ms-excel");
//header('Content-Disposition: attachment; filename="'.$filename.'"');
header("Content-Transfer-Encoding: binary");
header('Content-Length: ' . filesize($path.$filename));
readfile($path.$filename) or die("cannot open file");

this is not going to work.

try this instead
Code:
$filename="spcc.doc";
$path="C:\\";
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename="'.$filename.'"');
header('Content-Length: ' . filesize($path.$filename));
readfile($path.$filename) or die("cannot open file");
 
sorry all...trying every which way this side of sunday...but having absolutely no luck.

It is possible to open these files in the browser this way correct? I'm just really curious at this point with all the code changes and it still not working.
 
yes, it is possible provided that you have the right helper applications installed for your browser. the server has no control over this, of course.

the code that i posted above does NOT open the file in the browser, it forces a download of the file to the browser.

these download scripts are very very simple. there is no reason why it should not work for you. one thing to look at perhaps, is to ensure that the user under whose privileges the web server is running has read access to the files in question. i have been assuming it was not a permissions issue as you are reporting "garbage" rather than errors or the die() message.
 
yeah...definately not a permission problem. the interesting this is....i've copied code from my download page...and it doesn't work from the new page either. I don't get prompted to save or anything.

Granted...I dont want to ask the user to open or save most files (I.e. word, excel, pdf, etc). I just want to click a link and it opens the in a new browser window.

That is why I was originally using the <a href method.
 
ok...let me drop back 20 yards and punt.

what would/should be the correct code to open a file in a new browser window without prompting to open or save?

we'll keep it simple and just say a standard word document.

I'm going to completely start from scratch.
 
this will (i believe) only work with IE. i don't think there are helper applications for other browsers. which is why typically people don't try to force browser opens.

also i think that IE will now ask you to save/open regardless of content type, given certain security settings. you may also need expressly to register the helper application.

the how-to:
Code:
$filename="spcc.doc";
$path="C:/";
header('Content-Type: application/msword');
header('Content-Length: ' . filesize($path.$filename));
readfile($path.$filename) or die("cannot open file");
 
Code:
<?PHP
$filename="spcc.doc";
$path="C:\\";
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename="'.$filename.'"');
header('Content-Length: ' . filesize($path.$filename));
readfile($path.$filename) or die("cannot open file");
?>

This piece of code form jpadie works for me with absolutely no modifications. i just dropped a file called spcc.doc onto my C drive and it works.

Now Place it in en empty PHP file and see if it works. No spaces before the starting <?PHP tag. and no other HTML. Does it work?




----------------------------------
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.
 
It works in Firefox, provided you have the plugin, if you don't it will just prompt for a download instead.

----------------------------------
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.
 
ok...just did the easy one again (standard text file). i got it to open...now to try the pdf again.


very quick question...i notice that it opens in the same window as the code. can it open in a new window?
 
set the link you are clicking to target="_blank"

but get it working first!
 
well duh!! lol

see...i am soooo frustrated at this. i was thinking it had to come from the second page. DOH!

time for beer....then back to coding.
 
It would be cool if you could use some nifty ajax features for this.. it would however require that your workplace uses mostly IE and FF..

Olav Alexander Mjelde
Admin & Webmaster
 
Is there a default context type or string to use that it will prompt you to download the file if the appropriate plug in is not available.
 
application/octet-stream is most widely supported
 
ok..one last question on this...then its just time to move on.

TIF files. I have a viewer installed on the machine. how do i get the content-type in there to open these files?
 
why not just use application/octet-stream and leave the OS to handle it?
 
i tried that ... the dialog box that opens is asking me open or save the php page...not the file.
 
you must provide a Content-Disposition to force a download:

Code:
header ("Content-Disposition: attachment; filename:\"$filename\"");
 
<?PHP
$filename="spcc.doc";
$path="C:\\";
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename="'.$filename.'"');
header('Content-Length: ' . filesize($path.$filename));
readfile($path.$filename) or die("cannot open file");
?>

ok....i got this code snippet working however...it is asking to save the files instead of opening them if they are of recognizable format (i.e. doc, txt, etc). in this case...how do you open the files?

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top