I have a uload script that saves PDFs to the DB, and i have another page, where I want to read the PDF file, but the latter is not really happening..

AM I DEFINING MY PDF_DIR CORRECT:

define('PDFS_DIR', $_SERVER['DOCUMENT_ROOT'] .'includes/pdfs');

This is the read_pdf.php file:

<?php require_once("includes/config.php"); ?>
<?php require(MYSQL); ?>
<?php $valid =''; ?>
<?php
if(isset($_GET['id'])&&(strlen($_GET['id'])==40)&&(substr($_GET['id'],0,1)!='.')){
$file = 'PDFS_DIR'.$_GET['id'];
//echo $file; // Does echo out the id of the file!

if(file_exists($file)&&(is_file($file))){
$query = 'SELECT title, description, file_name, FROM pdfs WHERE tmp_name="'.mysqli_real_escape_string($connection, $_GET['id']).'"';
$result = mysqli_query($connection, $query);
if($mysqli_num_rows($result) == 1){//OK!
	 $row = mysqli_fetch_array($result, MYSQLI_ASSOC);
	 $valid = true;
	 
	 $fs = filesize($file);
	 
	 header("Content-Length:$fs\n");
	 header('Content-type:application/pdf');
	 header('Content-Disposition: inline;filename="'.$row['file_name'].'"');	 
	 
	 readfile($file);
	 //exit(); 	 
		}
	}
}
?>

I get an error = undefined variable $row, which is marked in bold just down here.
Further down the page try to echo out the file:

<?php
echo "<div>{[B]$row[/B]['description']}</div>"; // HERE I GET AN ERROR - UNDEFINED VARIABLE!??
if(!$valid){// = Error
echo'<h4>Der er sket en fejl og PDF-filen kunne ikke vises. 
Vi beklager ulejligheden, og vil løse problemet hurtigst muligt, da vores Administrator er blevet automatisk informeret!</h4>';	
}
?>

Guys and girls:

How can I read the PDF files I have uploaded to my database?????????????

Klemme

Recommended Answers

All 4 Replies

on your echo statement lose the {} and put in php tags

<?php echo $row['description']; ?>
// move your echo to the else of your if(!$valid) statement.  
//if it is not valid which is set in your above code nothing will be in $row.
//so if not valid 'error', and if it is valid, display your echo statement.
VARIABLE!??
if(!$valid){// = Error
echo'<h4>Der er sket en fejl og PDF-filen kunne ikke vises. 
Vi beklager ulejligheden, og vil løse problemet hurtigst muligt, da vores Administrator er blevet automatisk informeret!</h4>';	
} else {
    echo "<div>{$row['description']}</div>"; // $row should always have data as 'valid' is true and passed
}

Thanks for your answer,

I have changed it accordingly to your suggestion.

But i only get the error message, which should mean that $row; is not set/is empty.

So I guess the script for some reason cant find the pdf?

This is the script as it looks now:

<?php require_once("includes/config.php"); ?>
<?php require(MYSQL); ?>
<?php $valid = false; ?>
<?php
if(isset($_GET['id'])&&(strlen($_GET['id'])==40)&&(substr($_GET['id'],0,1)!='.')){
$file = 'PDFS_DIR'.$_GET['id'];

if(file_exists($file) && is_file($file)){
$query = 'SELECT title, description, file_name, FROM pdfs WHERE tmp_name='.mysqli_real_escape_string($connection, $_GET['id']).'';
$result = mysqli_query($connection, $query);
if($mysqli_num_rows($result) == 1){//OK!
$row = mysqli_fetch_array($result, MYSQLI_ASSOC);
$valid = true; 
	 
$fs = filesize($file);
	 
header("Content-Length:$fs\n");	 
header('Content-type:application/pdf');
header('Content-Disposition:inline;filename='.$row['file_name'].'');	
	 
readfile($file);
	 
die(); 	 
		}
	}
}
?>

Any idea of where it is going wrong??

Can you see if I have defined my upload folder wrong?
It is being included in the top of the script in the config.php

define('PDFS_DIR', $_SERVER['DOCUMENT_ROOT'] .'includes/pdfs');

My suggestion is that you debug your code at each step.

// you should not have to have your $connection in this statement.  
//this should be a valid sql query  have you ever gotten any data out of your db with a query like this?
$query = 'SELECT title, description, file_name, FROM pdfs WHERE tmp_name='.mysqli_real_escape_string($connection, $_GET['id']).'';
// take $connection out of the above line.
// if the query is failing $valid will always be false.  
// put some echo statments in and see if you have values that you are expecting throughout your code, not just at the end...

I was mistaken about your mysqli_real_escape_string($connection,'value');
code. I use mysql and not mysqli and did not know the proper syntax. anyway,
is it possible that readfile() is not working. try to eliminate some of the code. only make one portion of it work at a time, put echo statements in and debug each step of the way

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.