i would like to be able to upload a pdf to a database then later be able to download the book and view it. i want the names of the books to appear as links which when clicked will open the book. the uploading is being done but the downloading is giving me problems. this is the upload php file

 <!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 content="text/html; charset=utf-8" http-equiv="Content-Type" />
<title>Upload2.php</title>
</head>

<body>

<?php


if(isset($_POST['Upload']) && $_FILES['userfile']['size'] > 0)
{
    $fileName = $_FILES['userfile']['name'];
    $tmpName = $_FILES['userfile']['tmp_name'];
    $fileSize = $_FILES['userfile']['size'];
    $fileType = $_FILES['userfile']['type'];
    $fp = fopen($tmpName, 'r');
    $content = fread($fp, filesize($tmpName));
    $content = addslashes($content);
    fclose($fp);
    if(!get_magic_quotes_gpc())
    {
        $fileName = addslashes($fileName);
    }

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


    $dbcnx = @mysql_connect("localhost","root","");
        if(!$dbcnx)
        {
            die('Could not connect to database:'.mysql_error());
        }

        $tableLink = @mysql_select_db('book3', $dbcnx);     
        if(!$tableLink)
        {
            die('Could not connect:'.mysql_error());
        }

        $query = "INSERT INTO books (name, size, type, content)". "VALUES ('$fileName','$fileSize','$fileType','$content')";
        mysql_query($query) or die('Error sql failed');

        echo "File $fileName uploaded";


}

?>

</body>

</html>

and this is the download

<!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 content="text/html; charset=utf-8" http-equiv="Content-Type" />
<title>download 1</title>
</head>

<body>

<?php

        $dbcnx = mysql_connect("localhost","root","")
        or die('Unable to connect to database');

        mysql_select_db(book3)      
        or die('unable to connect');

        $query = "SELECT id,name FROM books LIMIT 0,30";

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

        if(mysql_num_rows($result) > 0)
        {
            while($row = mysql_fetch_object($result))
            {

                ?>

                  <!--echo "<a href=\"download1.php?id=$id\">$name</a><br/>";-->
                  <a href="download1.php?id= <?php echo $row->id; ?>"><?php echo $row->name; ?></a><br /><?php 
           }
        }

        else
        {
                echo "data is empty";       
        }

        mysql_close($dbcnx);

?>

<?php
    if(!isset($_GET['id']) || trim($_GET['id'] == ''))
    {
        die('Missing record ID');
    }
    else
    {
        $id = $_GET['id'];
        //echo $id;

        $dbcnx = mysql_connect("localhost","root","")
        or die('Unable to connect to database');

        mysql_select_db(book3)      
        or die('unable to connect');

        $query = "SELECT name, type,size,content FROM books WHERE id = '$id'";
        $result = mysql_query($query) or die('Error, query failed');
        $row = mysql_fetch_object($result);

        if($row)
        {
            ?>
            <?php echo nl2br($row->content); ?>
            <?php
        }
    }
?>

</body>

</html>

at present what i get is that the browser tries to open the document but appears as though its encrypted or corrupt. i get know errors.
hope i am clear as to what i want to do

Recommended Answers

All 7 Replies

suggestion only:

i am not sure this works fine or not

place this header declaration at line no :65 in download.php before echoing your pdf file data

header("Content-Type: application/pdf")

just check it once

let me know the status

Note: i am not good at coding side

Why do you store contents in database? You could just save a file in a directory and let users downolad it from there. If it is really necessary to store the contents in database I think the field type should be binary. I am not sure if you can recreate the file back just by reading it form the database to be normally readable as PDF.

no luck still appears funny. what a get on the browser has such symbols
s!1AQa"q2B#R3b$r
i wana save it on the database so that each user can insert material and not have access to files. i may be wrong but i assumed if i allow user to place books in the folder they will also be able to delete

If user can upload this does not mean they can delete (if you do not permit them). The directory containing the books should not be browsable. You provide a GUI for uploading and downloading and that is it.

will try that. otherwise how can i alter the code i have to allow the fucntionality i desire?

You cannot upload a PDF file and then just read it's content. The file content is encoded.

There are ways to extract the content of a PDF as plain text, but this won't work with images.

Alternatively, you could use file_get_contents to read the content of the file, as is, store this as a BLOB in the database, although I agree with others that you just reference the file location in the database, then set the headers to write the PDF content to the HTTP response to return it to the user as a PDF again.

You can refer Click Here to see how to do this.
Eg code:-

<?php
if ($id_files) {
  include "open_db.inc";
  $sql = "SELECT bin_data, filetype, filename, filesize FROM tbl_Files WHERE id_files=$id_files";

  $result = @mysql_query($sql, $db);
  $data = @mysql_result($result, 0, "bin_data");
  $name = @mysql_result($result, 0, "filename");
  $size = @mysql_result($result, 0, "filesize");
  $type = @mysql_result($result, 0, "filetype");

  header("Content-type: $type");
  header("Content-length: $size");
  header("Content-Disposition: attachment; filename=$name");
  header("Content-Description: PHP Generated Data");
  echo $data;
}
?>

Although it is possible to store and retrieve data but it is advisable not to store data in DB as everytime data is stored it is encoded into binary data(BLOB-. Binary Large Object) and when you retrieve it is decoded, so it will impact performance.
You must store data in a file system and just store location of file in DB

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.