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?
 
it just takes a fresh mind...i have believe i have it working now....use inline to display the file in a browser window (providing of course you have a plugin installed) and attachment to force the download.

i just need to add a few more lines of code with a case statment and i believe i can get rid of the <a href tags.
 
each browser behaves differently but it is becoming prevalent that browsers require you to save the file before opening them. they then nag you about safety before they actually open them.

if they recognise the file, some browsers might ask you whether you want to open them directly. some browsers will allow you to do this in the browser and others will call the native application. it depends, as said previously, whether there is a helper application for the browser. Vacunita suggested that there is an MS Word helper for firefox. I don't have this so FF opens a word document in the native word application. Conversely, i do have a helper installed for IE and so IE will open the same application in the browser for me.

i would shy away from trying to force the file from opening in the browser. typically you have no control over the browser's configuration.




 
this is true....i am in a strictly IE environment and since this is for a local intranet, i know the machines that will be accessing the webpage will have all the helpers needed. that's the plus side of course.

now if it were on the internet where there are different browsers, helpers and such, then the force download would be the best option.

lemme slap this code together and see how it goes.
 
here is some code that is slightly adapted from the php manual user notes.

it may be of more use dealing with multiple file types in IE.
I am not at all sure about using the inline content disposition, so if this breaks set $disps[1] to attachment to debug.

Code:
<?
$path = "./";
$filename = "somefile.doc";

$fInfo = pathinfo($filename);
$type = $fInfo['extension'];
$disps = array("attachment", "inline");

switch ($type) {
case "exe": $ctype="application/octet-stream"; $disp = $disps[0];
break;
case "pdf": $ctype="application/pdf"; $disp = $disps[1];
break;
case "zip": $ctype="application/zip" ; $disp = $disps[0];
break;
case "doc": $ctype="application/msword"; $disp = $disps[1];
break;
case "xls": $ctype="application/vnd.ms-excel"; $disp = $disps[1];
break;
case "ppt": $ctype="application/vnd.ms-powerpoint"; $disp = $disps[1];
break;
case "gif": $ctype="image/gif"; $disp = $disps[1];
break;
case "png": $ctype="image/png"; $disp = $disps[1];
break;
case "jpe": case "jpeg": case "jpg": $ctype="image/jpg";  $disp = $disps[1];
break;
default: $ctype="application/octet-stream"; $disp = $disps[0;]
}
//EXIST FILE? YES ->FORCEDOWNLOAD
//                  NO ->DIE AND COME BACK IF IS POSSIBLE
if (file_exists($path.$file)) {
	header("Pragma: public");
	header('Expires: '.gmdate('D, d M Y H:i:s').' GMT');
	header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
	header("Cache-Control: private",false);
	header("Content-Type: $ctype");
	header('Content-Disposition: '.$disp.'; filename="'.$filename.'.'.$type.'"');
	header("Content-Transfer-Encoding: binary");
	header('Content-Length: '.filesize($path.$file));
	set_time_limit(0);
	@readfile($path.$file) or die("Cannot read file for output"); 
	exit;
} else { 
	die ("File does not exist at that path");
}
?>
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top