Search the Community
Showing results for tags 'header()'.
-
OS: Windows 7 php version: 5.5.8 (running on EasyPHP Dev Server 14.1 VC11) Browser: Firefox 40.0.2 (same results in Chrome and IE) I am having problems with the show_image script in chapter 11. When I select an image from the list it opens a popup window of the appropriate size but with no image. In firefox I get the error message: 'The image "http://127.0.0.1/scripts/site/show_image.php?image=Picture.png"cannot be displayed because it contains errors.' When I use the Web Developer menu to View Response Headers all is as expected except that it lists Content-Length: 0. I commented out the header calls and replaced them with echos: echo "Content-Type: {$info['mime']}</br>"; echo "Content-Disposition: inline; filename=\"$name\"</br>"; echo "Content-Length: $fs"; Which return the values you would expect: Content-Type: image/png Content-Disposition: inline; filename="Picture.png" Content-Length: 35880 I changed the Content-Disposition to attachment. The files download as normal with the appropriate file names but have a size of 0. When I try to open them I get the error message: "Windows Photo Viewer can't display this picture because the file is empty." I double checked the files in the upload folder and they are all normal .png files. My code is below. Any advice you can offer would be very gratefully received. Adam show_image.php <?php $name = FALSE; if (isset($_GET['image'])) { //echo 'Image set </br>'; $ext = strtolower(substr($_GET['image'], -4)); if (($ext == '.jpg') OR ($ext == 'jpeg') OR ($ext == '.png')) { //echo 'Correct ext </br>'; $image = "../uploads/{$_GET['image']}"; if (file_exists($image) && (is_file($image))) { //echo 'File exists </br>'; $name = $_GET['image']; } } } if (!$name) { //echo 'Unavailable'; $image = 'images/unavailable.png'; $name = 'unavailable.png'; } $info = getimagesize($image); $fs = filesize($image); header ("Content-Type: {$info['mime']}\n"); header ("Content-Disposition: inline; filename=\"$name\"\n"); header ("Content-Length: $fs"); //echo "Content-Type: {$info['mime']}</br>"; //echo "Content-Disposition: inline; filename=\"$name\"</br>"; //echo "Content-Length: $fs"; images.php <!DOCTYPE html PUBLIC "-//W3C// DTD XHTML 1.0 Transitional//EN" "http:/www.w3.org/TR/xhtml/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"xml:lang="en" lang="en" <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>Images</title> <script type="text/javascript" charset="utf-8" src="js/function.js"></script> </head> <body> <p>Click on an image to view it in a separate window.</p> <ul> <?php # Script 11.4 - images.php $dir = '../uploads'; $files = scandir($dir); foreach ($files as $image) { if (substr($image, 0 , 1) != '.') { $image_size = getimagesize ("$dir/$image"); $image_name = urlencode($image); echo "<li><a href=\"javascript:create_window('$image_name',$image_size[0],$image_size[1])\">$image</a></li>\n"; } } ?> </ul> </body> </html> function.js // Script 11.3 - function.js function create_window (image, width, height) { width = width + 10; height = height + 10; if (window.popup && !window.popup.closed) { window.popup.resizeTo(width, height); } var specs = "location=no, scrollbars=no, menubars=no, toolbars=no, resizeable=yes, left=0, top=0, width=" + width + ", height=" + height; var url = "show_image.php?image=" + image; popup = window.open(url, "ImageWindow", specs); popup.focus(); }
- 5 replies
-
- php
- javascript
-
(and 2 more)
Tagged with:
-
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!
- 3 replies
-
- http headers
- header() function
-
(and 1 more)
Tagged with: