Trying to display file in browser with php

Richard Korts
Richard Korts used Ask the Experts™
on
I have this code. This is the entire php file:
<?php
// read file & display. File passed in $_GET
$path = "../summsp/" . $_GET['file'];
echo get_file_contents($path);
exit;
?>

Open in new window


I execute it like this:

https://www.lakoshvac.com/disp_summary.php?file=18075summary.pdf

I get a blank screen.

The directory structure is in the attached file. I cannot show you the file contents (pdf) because it is sensitive.

Why do I get a blank screen?

Thanks
Server_dir_sructure.JPG
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Most Valuable Expert 2018
Distinguished Expert 2018
Commented:
Hey Richard,

Firstly, turn on error reporting and you'll see the first problem with your code. The function you want is called file_get_contents(), and not get_file_contents().

The second problem you'll have is that you can't just echo out the contents of a PDF file. You'll need to send the correct http headers first.

Your script should look something like:

<?php
// read file & display. File passed in $_GET
$path = "../summsp/" . $_GET['file'];

header("Content-type: application/pdf");
header("Content-Disposition: inline; filename=filename.pdf");

echo file_get_contents($path);

Open in new window

Richard KortsBusiness Owner / Chief Developer

Author

Commented:
Perfect. I figured the function was wrong because it was not bolded in my html editor, but sometimes, it doesn't bold them all.

Thanks.

Richard
Most Valuable Expert 2018
Distinguished Expert 2018

Commented:
No worries Richard,

I would suggest that HTML editors are generally quite poor when it comes to analyzing PHP. A proper IDE would be a better choice. Add in error_reporting and you'll see exactly what's going on at runtime. Without it, you're effectively black-boxing your script - allowing it to fail silently with no hint as to what went wrong.

Another tip - when developing, keep an eye on your browsers built-in development tools. Loading up the page you linked to responded with a 500 Internal Server Error, so that's a good indication that your code is not working properly.
Exploring SQL Server 2016: Fundamentals

Learn the fundamentals of Microsoft SQL Server, a relational database management system that stores and retrieves data when requested by other software applications.

Richard KortsBusiness Owner / Chief Developer

Author

Commented:
It worked perfectly for me.

I'll ask the customer to try it.

Richard
Most Valuable Expert 2017
Distinguished Expert 2018

Commented:
Also take a look at readfile() - this function exists for precisely this use case

<?php
// read file & display. File passed in $_GET
$path = "../summsp/" . $_GET['file'];

header("Content-type: application/pdf");
header("Content-Disposition: inline; filename=filename.pdf");

readfile($path);

Open in new window

Richard KortsBusiness Owner / Chief Developer

Author

Commented:
Thanks.

FYI, I don’t know if Paula got ahold of you, we need your help (paid) again on our Media Wiki site. If she has NOT contacted you, please contact her.

Thanks,

Richard
Most Valuable Expert 2017
Distinguished Expert 2018

Commented:
Thanks Richard, will do

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial