Jump to content
Larry Ullman's Book Forums

Recommended Posts

Hello everyone!

 

I'm new to the forum, so let me start by saying I'm happy to be here and that I'm learning a lot from Larry's (awesome!) books and I'm thoroughly enjoying them.

 

I'm having a problem with HTTP Headers, specifically the header() function. Script 11.5, entitled "show_image.php" in Chapter 11, doesn't seem to work for me. I have tried it on two separate computers (desktop with Windows XP Pro, and laptop with Windows 7 Home) both using Firefox 11.0 and XAMPP 1.7.4 (which uses PHP 5.3.5).

 

So, then I tried to test out the simple headers, like the ones Larry uses on pages 356 & 357 and they, too, seem to not be working correctly for me. Presumably this means that I am probably doing something wrong and/or need to change some settings, perhaps in the php.ini file.

 

Anyhow, when I run the following simple program, the header redirects my browser to the url indicated, despite the fact that I put all kinds of HTML stuff before it.

 

Here's the program:

 

<!DOCTYPE html>
<html lang="en">

<head>

<meta charset="UTF-8" />
<title>This is my title</title>

</head>

<body>

<h1>Welcome to my website !</h1>

<p>Insert witty banter in this paragraph...</p>

<hr />

<a href="http://www.bbc.co.uk/news/">BBC News</a>

<?php

echo "Lorem ipsum dolor sit amet, consectetur adipiscing elit.";

#### a header() function to redirect the browser to The New York Times homepage...
header('Location: http://www.nytimes.com/');

?>

</body>
</html>

 

 

And then, when I run a similar program with three headers for the browser to receive a PDF, everything seems to open or download, but my PDF readers (I tried Foxit and Adobe) keep giving me the following error (even when I omit all of the HTML stuff before the headers) in a dialog box: format error: not a PDF or corrupted.

 

Anyhow, here is the second program:

 

<?php

#### 3 header functions for a PDF of the train schedule...
header("Content-Type:application/pdf\n");

#### and I tried putting this PDF, and other PDF's and image files in various folders...
header("Content-Disposition:attachment; filename=\"C:/xampp/htdocs/NJTransit.pdf\"\n");

#### I find the length, in bytes, by right-click, properties, size and size on disk
header("Content-Length: 622592\n");

?>

 

It's probably something wrong with me and/or my computers, but needless to say it's driving me crazy, and I believe that I need to use HTTP headers in Chapter 12, "Cookies and Sessions."

 

I'm wondering, for example, why the first program appropriately redirects the browser despite all of the HTML junk before it, and why the second program informs me that it's not a PDF or that it's corrupted.

 

As for the second problem, I've tried many different things in a futile attempt to fix these problems, such as:

  • moving the PDF to different folders
  • using different PDF's and images, in multiple folders
  • putting in different sizes (right-click, properties, size and size on disk) for header("Content-Length: 622592\n")
  • trying these programs and others on separate computers...

Everyone here seems much better at PHP than me, and by including both programs, someone may notice a common source of my frustration.

 

Any help would be really, really appreciated!

 

Thanks again!

Link to comment
Share on other sites

Welcome to the forum, CambridgeK!

 

In regards to your first issue, it is functioning correctly. You are using the header() function to change window.location, which is the URL of the current webpage, so that is why it is redirecting.

 

In regards to your second issue, try changing the filename part of the Content-Disposition statement to just filename=\"/NJTransit.pdf\"\n" (assuming it is located in your document root) -- I think you are giving too much of a path. I believe the browser is looking for an URL to the document, not the file system path.

  • Upvote 1
Link to comment
Share on other sites

Thanks for your help Paul!

 

So, a quick update:

 

I figured out why my first script was not working exactly as I expected. After consulting the phpinfo() function, I turned off output_buffering (which was set to 4096 bytes).

 

I'm not sure if I should turn it back on and increase the value from 4096, but at least now I know why the header was working despite all of the HTML and other stuff I purposely put before it.

 

Hopefully, that does not affect me because my second script is still not working. It's still frustrating to have my PDF viewers unable to open the PDF, informing me that it's not a PDF or that it's corrupted.....

  • Upvote 1
Link to comment
Share on other sites

It is probably best to have output_buffering turned off in php.ini. You can invoke output buffering whenever you need to by including the PHP Output Control Functions that are available.

 

For your second script, here is a snippet from some code that I use (slightly modified):

	    $location = $_SERVER['DOCUMENT_ROOT'] . '/pdf/myFile.pdf';
	    $filename = 'myFile.pdf';
	    $filesize = filesize ($_SERVER['DOCUMENT_ROOT'] . '/pdf/myFile.pdf');

	    // download file
	    header ("Content-Type: application/pdf");
	    header ("Content-Disposition: attachment; filename=\"$filename\"");
	    header ("Content-Length: $filesize");
	    header ("Content-Transfer-Encoding: binary");
	    readfile ($location);

 

A couple of things to note:

  • I use the superglobal $_SERVER['DOCUMENT_ROOT'] instead of typing the path to the document root, since my development environment is wildly different than my server environment in terms of the location of the document root. The script will work in both environments without modification.
  • I use the filesize() function to get the file size so I don't have to check it every time.
  • I'm letting the browser know that it's a binary file by using header ("Content-Transfer-Encoding: binary"). The example code you provided didn't include this, but maybe you had it and didn't include it.
  • readfile() is needed to send the file. Again, maybe your example wasn't the full code you used.

Hope this helps!

  • Upvote 1
Link to comment
Share on other sites

 Share

×
×
  • Create New...