1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

how to show Saved longblob data including images from mysql to convert into pdf

Discussion in 'PHP' started by rashid100, Nov 28, 2012.

  1. #1
    Hi ,

    I am getting the problem to saved LongBlob data type to show in a PDF. HERE is my Coding
    File imageUpload.php
    PHP Code:
    <?php
    // Connect to database
    //set_time_limit(1300);
    $errmsg = "";
    if (! @
    mysql_connect("localhost","root","elephant")) {
    $errmsg = "Cannot connect to database";
    }
    @
    mysql_select_db("test");

    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, 'rb');
    $content = fread($fp, filesize($tmpName));
    $content = addslashes($content);
    fclose($fp);

    if(!
    get_magic_quotes_gpc())
    {
    $fileName = addslashes($fileName);
    }

    $query = "INSERT INTO special (name, size, type, data) ".
    "VALUES ('$fileName', '$fileSize', '$fileType', '$content')";

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

    //echo "<br>File $fileName uploaded<br>";
    }

    ?>

    <html><head>
    <title>Upload an image to a database</title>
    <body bgcolor=white><h2>Here's the latest picture</h2>
    <form method="post" enctype="multipart/form-data">
    <table width="350" border="0" cellpadding="1" cellspacing="1" class="box">
    <tr>
    <td width="246">
    <input type="hidden" name="MAX_FILE_SIZE" value="2000000">

    <input name="userfile" type="file" id="userfile">

    </td>
    <td width="80"><input name="upload" type="submit" class="box" id="upload" value=" Upload "></td>
    </tr>
    </table>
    </form>

    <?php

    $query
    = "SELECT id,name,data FROM special";
    $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,$data) = mysql_fetch_array($result))
    {

    echo
    "<img src='download.php?id=$id' width='200' height='200' />";



    }
    }

    ?>
    </body>
    </html>


    and the other file is download.php
    PHP Code:
    <?php

    if (! @mysql_connect("localhost","root","elephant")) {
    $errmsg = "Cannot connect to database";
    }
    @
    mysql_select_db("test");


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

    $id = $_GET['id'];
    $query = "SELECT name, type, size, data FROM special WHERE id = '$id'";

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


    header('Content-type: image/jpeg');
    echo
    $content;

    exit;


    }

    ?>


    If I use the following command in imageUpload.php
    PHP Code:
    header("Content-type: application/pdf"); // add here more headers for diff. extensions
    header("Content-Disposition: attachment; filename='hello.pdf'"); // use 'attachment' to force a download

    echo "<img src='download.php?id=$id' width='200' height='200' />";


    If I take the OUTPUT in browser its work fine but in PDF Its give me the Error!

    Can any body help me in this matter.

    Thanks
     
    rashid100, Nov 28, 2012 IP
  2. Rukbat

    Rukbat Well-Known Member

    Messages:
    2,908
    Likes Received:
    37
    Best Answers:
    51
    Trophy Points:
    125
    #2
    What error does it give you? (And exactly what do you mean by "in PDF"? When you save the file and open it in a PDF reader? In that case, give us the link to one of your files, so we can ask your site for the file, save it and see why it doesn't look like a PDF file.)

    BTW, the best way to handle files with a database is to store the path and filename in the database, and store the file as a file. (We stored files in the database back when drive space was expensive. These days drive space is cheap, so you can store files as files, and you don't have to bang the database to return a 5MB string.)
     
    Rukbat, Nov 30, 2012 IP
  3. rashid100

    rashid100 Peon

    Messages:
    6
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Thanks for the reply. I mean to say that images saved in database and then I retrieve this from database I need to output in PDF format and I am using this command lines


    $query = "SELECT id,name,data FROM special";
    $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,$data) = mysql_fetch_array($result)){
    
    header("Content-type: application/pdf"); // add here more headers for diff. extensions
    header("Content-Disposition: attachment; filename='hello.pdf'"); // use 'attachment' to force a download
    
    echo "<img src='download.php?id=$id' width='200' height='200' />";
    
    
    } 
    }
    PHP:
    My example are here:
    htp://smarttest.atwebpages.com/


    so the result is Its give me download file but I cant open this pdf file. That's the problem Hope you will understand my point.
     
    Last edited: Dec 1, 2012
    rashid100, Dec 1, 2012 IP
  4. Rukbat

    Rukbat Well-Known Member

    Messages:
    2,908
    Likes Received:
    37
    Best Answers:
    51
    Trophy Points:
    125
    #4
    The contents of the downloaded file is:

    <img src='download.php?id=7' width='200' height='200' />

    You want to read the longblob (the data from the database) into a variable and echo that variable. (You might also want to change

    header("Content-Disposition: attachment; filename='hello.pdf'");

    to the actual name of the uploaded file (which you're already saving in the database). So filename='".$filename."'"); (You fill $filename from the name field of the database record.)
     
    Rukbat, Dec 1, 2012 IP
  5. rashid100

    rashid100 Peon

    Messages:
    6
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Yes you are right and the download.php coding is below



    <?php

    if (! @mysql_connect("localhost","root","elephant")) {
    $errmsg = "Cannot connect to database";
    }
    @
    mysql_select_db("test");


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

    $id = $_GET['id'];
    $query = "SELECT name, type, size, data FROM special WHERE id = '$id'";

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


    header('Content-type: image/jpeg');
    echo
    $content;

    exit;


    }

    ?>

    when echo the data comes from longblob data type field I want in a PDF format and longblob data is included some text and images. That's my original task.
     
    rashid100, Dec 1, 2012 IP
  6. Rukbat

    Rukbat Well-Known Member

    Messages:
    2,908
    Likes Received:
    37
    Best Answers:
    51
    Trophy Points:
    125
    #6
    Another little problem - you're sending header information after headers have been sent. Change download.php to

    <?php 
    ob_start(); //add this line
    if (! @mysql_connect("localhost","root","elephant")) { 
    PHP:
     
    Rukbat, Dec 1, 2012 IP
  7. rashid100

    rashid100 Peon

    Messages:
    6
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #7
    thats fine I add this line.
     
    rashid100, Dec 1, 2012 IP
  8. rashid100

    rashid100 Peon

    Messages:
    6
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #8
    now did you check that where I am doing mistake The purpose of all this procedure is that I have a project where you have write the online books through some editors like fckeditor or any other editor and through this editor all the data including text and images go into in the database and then on client request I want to retrieve the data from database and then download in a PDF format.

    Note : This data will save in a longblob data type field. Hope you now better understand my point
     
    rashid100, Dec 1, 2012 IP
  9. Rukbat

    Rukbat Well-Known Member

    Messages:
    2,908
    Likes Received:
    37
    Best Answers:
    51
    Trophy Points:
    125
    #9
    No, you can't send text and have the user get a pdf file. If you edit text you get text. If you want a text file in the database to be sent to the user as a pdf, you have to use a pdf library. Try Horde_pdf, or whatever pdf library you prefer. (It shouldn't take too long to figure out something as simple as converting a variable that contains text into one that's the pdf equivalent of that text. It should be just a call or two. (I haven't used any PEAR routines for pfd, but I have used the Excel routines, and sending data as an Excel file is pretty simple.)

    You have to send the application/pdf header if you're sending a pdf file, but you have to send a pdf file. Text isn't pdf. For one thing, a pdf file begins "%PDF-", and it has all sorts of encoding in it. If you lie to the browser (saying it's pdf but sending text) the best the user will get is a text file. He may get nothing.
     
    Rukbat, Dec 1, 2012 IP
  10. rashid100

    rashid100 Peon

    Messages:
    6
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #10
    Yes I am totally agree with you and try to use fpdf and then let you inform very soon thanx again for the help
     
    rashid100, Dec 1, 2012 IP