Php open file in browser

Simple question, probably been asked a million times but I've searched and am no closer to getting this to work.

I have a PHP script within the UserFrosting framework that serves a file to the user via their browser. I would like the file to open in their browser if they select 'Open' insteand of download.

Problem is, the only things that are opening properly are text files. Anything else (PDFs and JPGs tested so far) are just opening as a page of ASCII/garbled code instead of being interpreted as a PDF or an image.

Here's the code I'm using:

header('Content-Type: '. $mime);
header('Content-Length: ' . filesize($diskname));
header('Content-Disposition: inline; filename=' . $savename);
return readfile($diskname);

$mime is the Mime type of the file set via a function, so could be application/pdf or image/jpeg for the files I'm trying to view so far.

Does anyone have any ideas why the files aren't opening properly?

UPDATE Having checked the response headers, I'm getting a Content Type of text/html;charset=UTF-8 instead of the application/jpeg or application/pdf that it should have been set to according to the code I'm using. I'm wondering if this is a UserFrosting issue?


In this chapter we will teach you how to open, read, and close a file on the server.


PHP Open File - fopen()

A better method to open files is with the fopen() function. This function gives you more options than the readfile() function.

We will use the text file, "webdictionary.txt", during the lessons:

AJAX = Asynchronous JavaScript and XML
CSS = Cascading Style Sheets
HTML = Hyper Text Markup Language
PHP = PHP Hypertext Preprocessor
SQL = Structured Query Language
SVG = Scalable Vector Graphics
XML = EXtensible Markup Language

The first parameter of fopen() contains the name of the file to be opened and the second parameter specifies in which mode the file should be opened. The following example also generates a message if the fopen() function is unable to open the specified file:

Example

$myfile = fopen("webdictionary.txt", "r") or die("Unable to open file!");
echo fread($myfile,filesize("webdictionary.txt"));
fclose($myfile);
?>

Run example »

Tip: The fread() and the fclose() functions will be explained below.

The file may be opened in one of the following modes:

ModesDescription
r Open a file for read only. File pointer starts at the beginning of the file
w Open a file for write only. Erases the contents of the file or creates a new file if it doesn't exist. File pointer starts at the beginning of the file
a Open a file for write only. The existing data in file is preserved. File pointer starts at the end of the file. Creates a new file if the file doesn't exist
x Creates a new file for write only. Returns FALSE and an error if file already exists
r+ Open a file for read/write. File pointer starts at the beginning of the file
w+ Open a file for read/write. Erases the contents of the file or creates a new file if it doesn't exist. File pointer starts at the beginning of the file
a+ Open a file for read/write. The existing data in file is preserved. File pointer starts at the end of the file. Creates a new file if the file doesn't exist
x+ Creates a new file for read/write. Returns FALSE and an error if file already exists


PHP Read File - fread()

The fread() function reads from an open file.

The first parameter of fread() contains the name of the file to read from and the second parameter specifies the maximum number of bytes to read.

The following PHP code reads the "webdictionary.txt" file to the end:

fread($myfile,filesize("webdictionary.txt"));


PHP Close File - fclose()

The fclose() function is used to close an open file.

It's a good programming practice to close all files after you have finished with them. You don't want an open file running around on your server taking up resources!

The fclose() requires the name of the file (or a variable that holds the filename) we want to close:

$myfile = fopen("webdictionary.txt", "r");
// some code to be executed....
fclose($myfile);
?>


PHP Read Single Line - fgets()

The fgets() function is used to read a single line from a file.

The example below outputs the first line of the "webdictionary.txt" file:

Example

$myfile = fopen("webdictionary.txt", "r") or die("Unable to open file!");
echo fgets($myfile);
fclose($myfile);
?>

Run example »

Note: After a call to the fgets() function, the file pointer has moved to the next line.


PHP Check End-Of-File - feof()

The feof() function checks if the "end-of-file" (EOF) has been reached.

The feof() function is useful for looping through data of unknown length.

The example below reads the "webdictionary.txt" file line by line, until end-of-file is reached:

Example

$myfile = fopen("webdictionary.txt", "r") or die("Unable to open file!");
// Output one line until end-of-file
while(!feof($myfile)) {
  echo fgets($myfile) . "
";
}
fclose($myfile);
?>

Run example »


PHP Read Single Character - fgetc()

The fgetc() function is used to read a single character from a file.

The example below reads the "webdictionary.txt" file character by character, until end-of-file is reached:

Example

$myfile = fopen("webdictionary.txt", "r") or die("Unable to open file!");
// Output one character until end-of-file
while(!feof($myfile)) {
  echo fgetc($myfile);
}
fclose($myfile);
?>

Run example »

Note: After a call to the fgetc() function, the file pointer moves to the next character.


Complete PHP Filesystem Reference

For a complete reference of filesystem functions, go to our complete PHP Filesystem Reference.


PHP Exercises



How do I open a PHP file in Chrome?

Step by step instructions:.
Download and install XAMPP – The installation is quite simple and straightforward. ... .
Starting XAMPP – Once installed, you need to open the XAMPP Control Panel. ... .
Create your PHP page. ... .
Place the PHP file on the server. ... .
Find the path to your PHP page in your Chrome browser..

Why PHP file is not opening in browser?

So possible reasons could be: PHP is not installed properly on your system or the server is not properly installed. PHP module isn't loaded in your apache. You did not put your scripts in the right place.

How do I open a PHP file in Firefox?

But first, right-click any php-file->Open with... ->choose program, then select Firefox in the list.

Can a browser read PHP?

No, they can't. PHP is a server-side scripting language, the browser never actually sees the php code.