PDF not displaying on button Submit; Getting error

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
diseman
Forum Contributor
Posts: 174
Joined: Mon Jul 26, 2010 1:30 pm
Location: Florida

PDF not displaying on button Submit; Getting error

Post by diseman »

Can some please comment on why they think this code isn't working. When I click the VIEW button, it brings up the Adobe window asking me if I want to open it, but then I get a black screen w/ error that says "Adobe can't open the file xxx.pdf because it's either not a supported file type or because the file has been damaged."

The file is definitely not damaged as I CAN view the file when I click the link to the left of the VIEW button, WHICH uses virtually the same code on another page. See, I first started with this script with it displaying the PDF file name as a URL link, which I can currently click it a see the PDF. THEN, I wanted to try a VIEW button. So yes, I have two ways of doing the same thing right now except the VIEW button doesn't work completely. Yes, I could remove the button and just use the link, but this is a mock site for learning; nothing more and it's ticking me off that I can't get it to work. : )

Anyways, if helps to know... the working code on the other page is identical to what you see on this page EXCEPT it has:

Code: Select all

if(isset($_GET['id']))
{

$id    = $_GET['id'];
At the top. I did however try that on this page (below) and nothing; still getting error.

Headers seem right from what I've seen in other examples. I tested to make sure the $id is being properly seen and it is. Paths seem to be identical with this page and the working page being in same directory.

Here it is (please EXPAND):

Code: Select all

<?php

// ----------------------------------------------------------------------------------------------------------
// Connect to server then db and return error if not connected.
// ----------------------------------------------------------------------------------------------------------

include ("../_includes/dbconnect.php");

// ----------------------------------------------------------------------------------------------------------
// Set the value for id:
// ----------------------------------------------------------------------------------------------------------

$id = $_POST["id"];

// ----------------------------------------------------------------------------------------------------------
// DELETE Button code: Working...
// ----------------------------------------------------------------------------------------------------------

if ($_POST['delete'])

{

$sql = "DELETE FROM documents WHERE id = ".$id;" " ;

mysql_query($sql,$con) or die("query: $query<br>" . mysql_error());

}

// ----------------------------------------------------------------------------------------------------------
// VIEW Button code: Not Working...
// ----------------------------------------------------------------------------------------------------------

if ($_POST['view'])

{
$id    = $_POST['id'];

$query = "SELECT name, type, size, content " .
         "FROM documents WHERE id = " . $_POST['id'];

$result = mysql_query($query) or die('Error, query failed');
list($name, $type, $size, $content) =  mysql_fetch_array($result);

header("Content-length: $size");
header("Content-type: $type");
header("Content-Disposition: attachment; filename=$name");
echo $content;

mysql_close($con);
exit;

}

// ----------------------------------------------------------------------------------------------------------
// Rename Button code:
// ----------------------------------------------------------------------------------------------------------

if ($_POST['rename'])

{
// haven't gotten to this yet
}

// ----------------------------------------------------------------------------------------------------------
// Pull all pdf document records by 'id' and sort ascending
// Display PDF documents by Name/URL> 
// Provide buttons to Rename and Delete
// ----------------------------------------------------------------------------------------------------------

$query = "SELECT id, name FROM documents ORDER BY name ASC";

$result = mysql_query($query,$con) or die('Error, query failed');

while(list($id, $name) = mysql_fetch_array($result))
{
?>
<form action="" method="post">
<table>
<tr>
<td width="300"><a href="../modules/documents.php?id=<? echo $id; ?>"><u><? echo $name; ?></u></a></td>
<td><input type="submit" name="view" 	value="View" /></td>
<td><input type="submit" name="rename" 	value="Rename"	/></td>
<td><input type="submit" name="delete" 	value="Delete"	/></td>
<td><input type="hidden" name="id" value="<? echo $id; ?>" /></td>
</tr>
</table>
</form>

<?
}

// ----------------------------------------------------------------------------------------------------------
// Close database connection
// ----------------------------------------------------------------------------------------------------------

mysql_close($con);

?>
Thanks in advance for any help..
Post Reply