Jump to content

[SOLVED] linking a file (pdf) to an article


Recommended Posts

Hi thanks for reading,

 

I have put together a simple article cms script for a client. Via the backend they can type in their article and upload a pdf. The pdf needs to be displayed on the article page. So far I have worked out how to display a list of all the uploaded pdf's on the article page. They need the pdf associated with the article only displayed.

 

I need some help  :'(

 

here is the article page:

 

<?php
include 'library/config.php';
include 'library/opendb.php';

// if no id is specified, list the available articles
if(isset($_GET['id']))
{
$self   = $_SERVER['PHP_SELF'];

$query  = "SELECT id, title FROM courses ORDER BY id";
$result = mysql_query($query) or die('Error : ' . mysql_error()); 

// create the article list 
$content2 =  '<ol>';
while($row = mysql_fetch_array($result, MYSQL_NUM))
{
	list($id, $title) = $row;
	$content2 .= "<li><a href=\"courseinfo.php?id=$id\">$title</a></li>\r\n";
}

$content2 .= '</ol>';

$title   = '<span class="course_grey">our<span class="orange_hed"> courses:</span>';
} 

if(isset($_GET['id']))
{
// get the article info from database
$query   = "SELECT title, content FROM courses WHERE id=".$_GET['id'];
$result  = mysql_query($query) or die('Error : ' . mysql_error()); 
$row     = mysql_fetch_array($result, MYSQL_ASSOC); 

$title   = $row['title'];
$content = $row['content'];
}



include 'library/closedb.php';
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Train to Gain Thames Gateway Consortium :: NVQ Activity Leadership</title>
<link href="../css/jebus.css" rel="stylesheet" type="text/css" />
<link href="../css/menu.css" rel="stylesheet" type="text/css" />
<link href="../css/text.css" rel="stylesheet" type="text/css" />
<link href="../css/links.css" rel="stylesheet" type="text/css" />
<script src="../bits/scripts/testimonials.js" type="text/javascript">
</script>
</head>

<body onload="rotateEvery(1)">
<div id="main_container">

<div id="main_header">

<!--..........................menu begins........................-->
<div class="menucontainer">
<div class="submenu">
  <a href="../news.html" class="submenu">News</a>
  <a href="../sitemap.html" class="submenu">Sitemap</a>
  <a href="../login.html" class="submenu">Partner Login</a>
  <a href="../casestudies.html" class="submenu">Case Studies</a>
</div><!--submenu ends-->

<div class="mainmenu">
<a href="../index.html" class="nav"><div class="menu_frames" id="home_frame" align="center">Home</div></a>
<a href="../about.html" class="nav"><div class="menu_frames" id="about_frame" align="center">About Us</div></a>
<a href="../courses/contact.html" class="nav"><div class="menu_frames" id="contact_frame" align="center">Contact Us</div></a>
<a href="../employers.html" class="nav"><div class="menu_frames" id="employers_frame" align="center">Employers</div></a>
<a href="../employees.html" class="nav"><div class="menu_end_frame" id="employees_frame" align="center">Employees</div></a>
<div class="course_on" align="center">Course Guide</div>
</div><!--mainmenu ends-->

</div>
<!--..........................menu ends........................-->

<div class="logo_box" align="right">
<img src="../bits/images/general/logo_main.gif" height="58" width="106" border="0" class="logo_spacer" /><br />
<span class="logofont">Thames Gateway Consortium</span>
</div>

</div>
<!--main_header ends-->

<div id="flash_box"><img src="../bits/images/search/course.jpg" height="87" width="850" border="0" /></div>
<!--flashbox-->

<div id="content_box">
<div id="left_col">
<span class="quote_hed">What has <br />
Train to Gain <br />
done for you?</span><br />
<br /><br />
<span class="quote_body">
<div id="textrotator"><!--Quotations will be displayed here--></div>
<br />
<br />
<br />
<a href="../contact.html#1" class="foot_link">have your say</a><img src="../bits/images/general/bubble_xs.png" height="17" width="23" border="0" /></span>

</div>
<!--...........................left_column ends................................-->



<div id="right_col">
<div class="main_area">
<span class="course_grey">National Vocational Qualification</span><br />
<span class="course_or"><?php echo $title;?></span><br /><br />

<span class="body_grey">
<?php 
echo $content;


// when displaying an article show a link
// to see the article list
if(isset($_GET['id']))
{ 
?></span>
   <p> </p>
   <span class="body_grey">__________________________________________________</span><br /><br />

<span class="grey_hed">NVQ</span><span class="orange_hed"> courses:</span>
   <span class="courselinks"><p><?php echo $content2; ?></p></span>
<?php
}
?> 


<span class="body_grey">(click on a course to find out more)</span>



</div>
<!--...........................main area ends...............................-->

<div class="call_area">
<div class="call_pdf">
<span class="grey_hed">download</span><span class="orange_hed"> pdf</span><br />
<span class="grey_hed">of this course</span><br /><br />
<?php
include 'config.php';
include 'opendb.php';

$query  = "SELECT id, name FROM courses";
$result = mysql_query($query);

while($row = mysql_fetch_array($result, MYSQL_ASSOC))
{
    echo "id :{$row['id']} <br>" .
         "name : {$row['name']} <br>" ; 

} 

include 'closedb.php';
?> 


<?php
include 'library/config.php';
include 'library/opendb.php';

$query  = "SELECT id, name FROM courses";
$result = mysql_query($query) or die('Error, query failed');
if(mysql_num_rows($result) == 0)
{
echo "Database is empty <br>";
} 
else
{
while(list($id, $name) = mysql_fetch_array($result))
{
?>
<a href="downloadpdf.php?id=<?=$id;?>"><?=$name;?></a> <br>
<?php		
}
}
include 'library/closedb.php';
?>

</div>

<div class="call_info">
<span class="orange_hed"> enquire</span><span class="grey_hed"> about<br />
this course</span><br /><br />
<a href="../contact.html" class="or"> click here</a></div>

<div class="call">
  <a href="../courseguide.html"><img src="../bits/images/general/search1.jpg" alt="click here to search our course guide" width="170" height="234" border="0" class="call_spa" /></a></div>
<div class="contact_call_big">
<div class="contact_spa">
<span class="foot_link">Get a skills analysis or details of funded courses by calling us during
office hours</span><br />
<br />
<span class="med_white"></span><br />
</div>
</div>

</div>
<!--call area ends-->

</div>
<!--...........................right_column ends................................-->
<!--...........................right_column ends................................-->

<div class="clearboth" id="foo"><!--empty--></div>
</div><!--content_box-->


<div class="footer">
<div class="copyrightblock">
<span class="copyright">© Copyright Train to Gain 2008</span></div>
<div class="footerblock">
  <a href="../news.html" class="foot_link">News</a>
  <a href="../sitemap.html" class="foot_link">Sitemap</a>
  <a href="../login.html" class="foot_link">Partner Login</a></div>

<div class="contactblock">
  <span class="foot_link"><strong>Contact Us:</strong></span>
  <span class="foot_link"></span>
  </div>

</div><!--footer-->

<div class="clearboth" id="foo"></div>
</div><!--main_container-->
</body>
</html>

 

and here is the download page part:

 

<?php
error_reporting(E_ALL);
if(isset($_GET['id']))
{
include 'library/config.php';
include 'library/opendb.php';

$id      = $_GET['id'];
$query   = "SELECT name, type, size, path FROM courses WHERE id = '$id'";
$result  = mysql_query($query) or die('Error, query failed');
list($name, $type, $size, $filePath) = mysql_fetch_array($result);

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

readfile($filePath);

include 'library/closedb.php';	
exit;
}

?>

<html>
<head>
<title>Download File From MySQL</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>

<body>
<?php
include 'library/config.php';
include 'library/opendb.php';

$query  = "SELECT id, name FROM courses";
$result = mysql_query($query) or die('Error, query failed');
if(mysql_num_rows($result) == 0)
{
echo "Database is empty <br>";
} 
else
{
while(list($id, $name) = mysql_fetch_array($result))
{
?>
<a href="downloadpdf.php?id=<?=$id;?>"><?=$name;?></a> <br>
<?php		
}
}
include 'library/closedb.php';
?>
</body>
</html>

 

If you can help me you would make a sad  :( guy very happy

 

Thanks very much

Link to comment
https://forums.phpfreaks.com/topic/112197-solved-linking-a-file-pdf-to-an-article/
Share on other sites

I have worked this out

 

if(isset($_GET['id']))
{
// get the article info from database
$query   = "SELECT id, title, content, name, path FROM courses WHERE id=".$_GET['id'];
$result  = mysql_query($query) or die('Error : ' . mysql_error()); 
$row     = mysql_fetch_array($result, MYSQL_ASSOC); 

$title   = $row['title'];
$content = $row['content'];
$pdfname = $row['name'];
$pdfpath = $row['id'];
}

 

<a href="downloadpdf.php?id=<?=$pdfpath;?>"><?=$pdfname;?></a> 

 

The only thing I have now found is that when i open the pdf, it says its not supported file type or damaged or wasnt correctly decoded?

 

If i open the file from the server it is fine but if i open it from the webpage it is not?

 

Does anyone know why?

this is my upload script:

 

$uploadDir = '/#/';

if(isset($_POST['save']))
{
$title   = $_POST['title'];
$content = $_POST['content'];

//file upload

$fileName = $_FILES['userfile']['name'];
$tmpName  = $_FILES['userfile']['tmp_name'];
$fileSize = $_FILES['userfile']['size'];
$fileType = $_FILES['userfile']['type'];

    // get the file extension first
$ext      = substr(strrchr($fileName, "."), 1); 

// generate the random file name
$randName = md5(rand() * time());

// and now we have the unique file name for the upload file
    $filePath = $uploadDir . $randName . '.' . $ext;

    // move the files to the specified directory
// if the upload directory is not writable or
// something else went wrong $result will be false
    $result    = move_uploaded_file($tmpName, $filePath);
if (!$result) {
	echo "Error uploading file";
	exit;
}

// end file upload

if(!get_magic_quotes_gpc())
{
	$title   = addslashes($title);
	$content = addslashes($content);
	$fileName  = addslashes($fileName);
        $filePath  = addslashes($filePath);
}
include 'library/config.php';
include 'library/opendb.php';                   


$query = "INSERT INTO courses (title, content, name, size, type, path) VALUES ('$title', '$content', '$fileName', '$fileSize', '$fileType', '$filePath')";
mysql_query($query) or die('Error, query failed : ' . mysql_error()); 
include 'library/closedb.php';

echo "<div class=\"alert\">Course '$title' added</div>";
echo "<br>File uploaded<br>";
}
?>
<form action="" method="post" enctype="multipart/form-data" name="uploadform">
  <table width="700" border="0" cellpadding="2" cellspacing="1" class="box" align="center">
    <tr> 
      <td width="100">Title</td>
      <td><input name="title" type="text" class="box" id="title"></td>
    </tr>
    <tr> 
      <td width="100" valign="top">Content</td>
      <td><textarea name="content" cols="80" rows="80" class="box" id="content"></textarea></td>
    </tr>
    <tr> 
      <td width="100">pdf upload </td>
      <td><table width="350" border="0" cellpadding="1" cellspacing="1" class="box">
    <tr> 
      <td width="246"><input type="hidden" name="MAX_FILE_SIZE" value="20000000000"><input name="userfile" type="file" class="box" id="userfile">
	 </td>
      <td width="80"></td>
    </tr>
  </table></td>
    </tr>
    <tr> 
      <td colspan="2" align="center"><input name="save" type="submit" class="box" id="save" value="Save Article"></td>
    </tr>
  </table>
</form>

 

and this is my download script:

 

<?php
error_reporting(E_ALL);
if(isset($_GET['id']))
{
include 'library/config.php';
include 'library/opendb.php';

$id      = $_GET['id'];
$query   = "SELECT name, type, size, path FROM courses WHERE id = '$id'";
$result  = mysql_query($query) or die('Error, query failed');
list($name, $type, $size, $filePath) = mysql_fetch_array($result);

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

readfile($filePath);

include 'library/closedb.php';	
exit;
}

?>

<html>
<head>
<title>Download File From MySQL</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>

<body>
<?php
include 'library/config.php';
include 'library/opendb.php';

$query  = "SELECT id, name FROM courses";
$result = mysql_query($query) or die('Error, query failed');
if(mysql_num_rows($result) == 0)
{
echo "Database is empty <br>";
} 
else
{
while(list($id, $name) = mysql_fetch_array($result))
{
?>
<a href="downloadpdf.php?id=<?=$id;?>"><?=$name;?></a> <br>
<?php		
}
}
include 'library/closedb.php';
?>

If you know the exact path of your PDF, try this?

 

download.php

header("Pragma: public"); // required
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Cache-Control: private",false); // required for certain browsers 
header("Content-Type: application/pdf");
header("Content-Disposition: attachment; filename=\"".basename($filename)."\";" );
header("Content-Transfer-Encoding: binary");
header("Content-Length: ".filesize($filename));
readfile("$filename");
exit();

 

user by download.php?filename=PDF.pdf

Try doing a base64_encode when putting it into the database.

 

$fp = fopen($_FILES['userfile']['tmp_name'], 'r');
				$content = fread($fp, filesize($_FILES['userfile']['tmp_name']));
				$content = base64_encode($content);
...insert into db with $content...

 

and then on the download page, do the same thing but with a base64_decode.

 

Not sure if that's the problem, but I know I had a lot of problems with PDFs read into my database before I did this.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.