#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    9
    Rep Power
    0

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


    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($fpfilesize($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
  2. #2
  3. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    3
    Rep Power
    0
    Not entirely sure what you are trying to accomplish but a PDF is a binary file format, you can't just take an image or some other file format and say it's a pdf. You can create your own pdf with a library like fpdf however.

IMN logo majestic logo threadwatch logo seochat tools logo