Trying to display file in browser with php

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
Richard KortsBusiness Owner / Chief DeveloperAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Chris StanyonWebDevCommented:
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

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Richard KortsBusiness Owner / Chief DeveloperAuthor 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
0
Chris StanyonWebDevCommented:
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.
0
OWASP: Avoiding Hacker Tricks

Learn to build secure applications from the mindset of the hacker and avoid being exploited.

Richard KortsBusiness Owner / Chief DeveloperAuthor Commented:
It worked perfectly for me.

I'll ask the customer to try it.

Richard
0
Julian HansenCommented:
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

0
Richard KortsBusiness Owner / Chief DeveloperAuthor 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
0
Julian HansenCommented:
Thanks Richard, will do
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.