I run the bridge game on a Monday evening and just recently I brought in a card dealer so the computer can select the cards and deal them into 4 hands. 20 sets of these hands and we can play for the evening. My problem is putting the dealt card images on the web site. If the file is opened directly http://shellpoint.info/BridgeScores/uploads/20210426_MonEve_printout_1.pdf the card images are fine.

If the file is opened by script they're junk http://shellpoint.info/BridgeScores/listscores.php?file=./uploads/20210426_MonEve_printout_1.pdf.

Can someone help fix the web site for next Monday's game?
I’ll add my email emitchell@ieee.org

Hi Ed. Thanks for posting this here. I tried to help you via our little one-on-one chat, but the private chat isn't really conducive to posting code and such.

The problem seems to be that you are using the listscores.php to point the browser to this PDF file. However, web browsers typically interpret the output of PHP files as plaintext HTML markup, and the PDF is essentially binary data.

When you try to load the .pdf file directly, the web browser knows how to correctly handle .pdf files, and so it treats it as such. When you try to load the .pdf file by way of .php, the web browser assumes HTML is the output of the .php file.

It would help if you could show us the contents of listscores.php

However, in lieu of that, adding the following PHP headers to listscores.php just before you echo anything out should resolve the problem for you:

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

Here's the source for the listscores.php:

<?php
// will list a file that has the path transferred through the URL
?>
<html>
<head>

    <title>Bridge Scores</title>
</head>
<body>
    <a href="index.php">Back to Result List</a>
<?php
    $file = $_GET['file'];
    $contents = file_get_contents($file);
    // note contents will have new lines so we keep these and don't
    // add the <br /> tags since then we'll have an extra blank line
    echo("<p><pre>");
    echo($contents);
    echo("</pre></p>");
?>
    <a href="mailto:emitchell@ieee.org">Report Problems</a>
</body>
</html>

The start is at index.php:

<?php
// will list a file that has the path transferred through the URL
?>
<html>
<head>
    <title>Bridge Scores</title>
</head>
<body>
    <a href="index.php">Back to Result List</a>
<?php
    $file = $_GET['file'];
    $contents = file_get_contents($file);
    // note contents will have new lines so we keep these and don't
    // add the <br /> tags since then we'll have an extra blank line
    echo("<p><pre>");
    echo($contents);
    echo("</pre></p>");
?>
    <a href="mailto:emitchell@ieee.org">Report Problems</a>
</body>
</html>

To be complete here is SelectUpload.php:

<?php
// will list a file that has the path transferred through the URL
?>
<html>
<head>
    <title>Bridge Scores</title>
</head>
<body>
    <a href="index.php">Back to Result List</a>
<?php
    $file = $_GET['file'];
    $contents = file_get_contents($file);
    // note contents will have new lines so we keep these and don't
    // add the <br /> tags since then we'll have an extra blank line
    echo("<p><pre>");
    echo($contents);
    echo("</pre></p>");
?>
    <a href="mailto:emitchell@ieee.org">Report Problems</a>
</body>
</html>

and finally upload.php

<?php
// will list a file that has the path transferred through the URL
?>
<html>
<head>
    <title>Bridge Scores</title>
</head>
<body>
    <a href="index.php">Back to Result List</a>
<?php
    $file = $_GET['file'];
    $contents = file_get_contents($file);
    // note contents will have new lines so we keep these and don't
    // add the <br /> tags since then we'll have an extra blank line
    echo("<p><pre>");
    echo($contents);
    echo("</pre></p>");
?>
    <a href="mailto:emitchell@ieee.org">Report Problems</a>
</body>
</html>

I added the 2 header(...) lines but the system still produced gibberish: I've never used "header" before.

<?php
// will list a file that has the path transferred through the URL
?>
<html>
<head>
    <title>Bridge Scores</title>
</head>
<body>
    <a href="index.php">Back to Result List</a>
<?php
    $file = $_GET['file'];
    $contents = file_get_contents($file);
    header("Content-type:application/pdf");
    header("Content-Disposition:attachment; filename='printout.pdf'");
    // note contents will have new lines so we keep these and don't
    // add the <br /> tags since then we'll have an extra blank line
    echo("<p><pre>");
    echo($contents);
    echo("</pre></p>");
?>
    <a href="mailto:emitchell@ieee.org">Report Problems</a>
</body>
</html>

The headers have to be spit out before any other contents on the page. Also, are trying to spit out the contents of a PDF file and HTML on the same page. You can't do that. The webpage needs to be either HTML or a PDF file, not both.

For a PDF file, listscores.php should look as so:

<?php
$file = $_GET['file'];
header("Content-type:application/pdf");
header("Content-Disposition:attachment; filename='printout.pdf'");
readfile($file);

And that's it in the entire php file. Just those 5 lines. (Of course, this is very insecure because you're essentially letting someone download any file on the web server, depending upon the path they pass in as a URL parameter.)

I changed the the second header( ) line to :

<?php
    $file = $_GET['file'];
    $contents = file_get_contents($file);
    header("Content-type:application/pdf");
    header("Content-Disposition:attachment; filename=$file");

I changed the position of the header(...) lines to be above file_get_contents(...) and I think fixed the use of the file name (filename=".$file) but the image is the same. I surely have to echo the contents after getting them.

<html>
<head>
    <title>Bridge Scores</title>
</head>
<body>
    <a href="index.php">Back to Result List</a>
<?php
    $file = $_GET['file'];
    header("Content-type:application/pdf");
    header("Content-Disposition:attachment; filename=".$file);
    $contents = file_get_contents($file);
    // note contents will have new lines so we keep these and don't
    // add the <br /> tags since then we'll have an extra blank line
    echo("<p><pre>");
    echo($contents);
    echo("</pre></p>");
?>
    <a href="mailto:emitchell@ieee.org">Report Problems</a>
</body>
</html>
commented: This appears to break the rule "The webpage needs to be either HTML or a PDF file, not both." +15

It’s not working because you’re still doing the same thing wrong of including HTML output on the same Page as you’re trying to spit out PDF contents.

What happens when you use the code I provided to you?

As far as having to echo the contents of the PDF out, my readfile() function is shorthand for both reading and then echoing out a file contents. It’s just like doing file_get_contents() and then echo() in one.

Your problem is you are still printing echo’ing out HTML code, even though it’s clear that’s what is breaking it.

When I comment our the echoing of the contents I just get the output of the 2 <a href="..." ... </a> lines as a single line:

Back to Result List Report Problems

which are really the 2 hyperlinks in the listscores.php file.

<body>
    <a href="index.php">Back to Result List</a>
 <?php
    $file = $_GET['file'];
    header("Content-type:application/pdf");
    header("Content-Disposition:attachment; filename=".$file);
    $contents = file_get_contents($file);
    // note contents will have new lines so we keep these and don't
    // add the <br /> tags since then we'll have an extra blank line
    // echo("<p><pre>");
    // echo($contents);
    // echo("</pre></p>");
?>
<a href="mailto:emitchell@ieee.org">Report Problems</a>

</body>

Without the echo(...) I don't see the $contents.

When I comment our the echoing of the contents I just get the output of the 2 <a href="..." ... </a> lines as a single line:

As I keep pointing out, you are still including HTML code. In the code above, the lines 1, 2, 13, and 14 are all HTML that cannot exist if you're trying to print a PDF. Also, you aren't seeing any of the PDF contents because you're not echo'ing out the PDF contents (you commented out line 11).

Please, please, please just try the five lines of code I provided to you, and it should work.

Also, I noticed you changed my code to:

header("Content-Disposition:attachment; filename=".$file);

This is incorrect. You don't want the filename to be a full path, as $file denotes. Go back to my initial value here.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.