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 strongm 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
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?
 
you have to do more than opening the file..

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

$contents = fread($fh, filesize($filename));
fclose($handle);

echo $contents;
?>

Olav Alexander Mjelde
Admin & Webmaster
 
i got this figured...wasn't passing anything to the browser.

but....when attempting to open a PDF file...all i get is garbage. Is there a certain header that i need to be passing for this?
 
btw. wrap the entire logic in:

Code:
if (is_file($filename)) {
  // put open code here
}
else {
  echo "404 - file not found";
}

Olav Alexander Mjelde
Admin & Webmaster
 
send a header with the content-type set to application/pdf and make sure the content encoding is binary.

the header function is well documented with examples in the manual
 
lol...that helps not.

was testing with other formats (xls doc etc..)....they all open as garbage
 
that's what i have currently...i still get garbage on the screen instead of the the file.
 
i'd always open the file for binary read. safer that way
Code:
$fp = fopen("test.pdf", "rb");
[/content]
 
James, are you sure the problem is not on the machine you are on?

Do you have acrobat reader installed on the webbrowser?

Olav Alexander Mjelde
Admin & Webmaster
 
yes...no problem with the machine. as suggested from an earlier post...i am trying to move away from using <a href to fopen() to open my files.
 
try this

Code:
$filename="test.pdf";
$path = "path/to/file/";
header('Content-type: application/pdf');
header('Content-Disposition: attachment; filename="'.$filename.'"');
readfile($path.$filename) or die("cannot open file");

readfile is binary safe.
 
ok...it appears that standard text files and images i can open. I can't open anything else..displays garbage. Here is my code I am using:

$filename="test.doc";
$path="C:\\";

header("Cache-Control: private",false);
header('Content-Type: application/octet-stream'); // Good
header("Content-Type: image/jpeg"); // Good

header("Content-type: application/pdf"); // Garbage
header("Content-type: application/msword"); // Garbage
header("Content-type: application/vnd.ms-excel"); // Garbage

header('Content-Disposition: attachment; filename="'.$filename.'"');
header("Content-Transfer-Encoding: binary");
readfile($path.$filename) or die("cannot open file");
 
can you try adding a filesize header.
header('Content-Length: ' . filesize($path.$filename));
 
also if you specify a content type like msword, i think the disposition would be better left out or set to inline.
 
modified 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");
 
i tried both ways...having all in there and also commenting all but msword
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top