PDF Download from BLOB help.

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
ferio-moreno
Forum Commoner
Posts: 30
Joined: Tue Feb 28, 2006 10:31 pm
Location: florida

PDF Download from BLOB help.

Post by ferio-moreno »

Hello,
I'm having a few problems downloading an uploaded PDF file from the BLOB field. Everytime each PDF opens up in Acrobat, it gives me an error saying that the file is corrupted.

This is what I do to the file before database insertion

Code: Select all

 
<?php
        $tmpName = $_FILES['form_upload']['tmp_name'];
        $fp      = fopen($tmpName, 'r');
        $content = fread($fp, filesize($tmpName));
        $content = addslashes($content);
        fclose($fp);
?>
and here's the code that I am using for download.

Code: Select all

<?php
    require_once($_SERVER['DOCUMENT_ROOT'].'/ext/classes/class.forms.php');
    $forms = new forms;
    $file = $forms->downloadForm($_GET['file_id']);
    header("Content-type: ".$file->fields('form_type'));
    header("Content-Disposition: attachment; filename=".$_GET['file_name']);
    echo $file->fields('file_data');
?>
any help?
User avatar
andyhoneycutt
Forum Contributor
Posts: 468
Joined: Wed Aug 27, 2008 10:02 am
Location: Idaho Falls

Re: PDF Download from BLOB help.

Post by andyhoneycutt »

does your forms class strip slashes before it outputs? you may need a stripslashes in there due to your addslashes.
ferio-moreno
Forum Commoner
Posts: 30
Joined: Tue Feb 28, 2006 10:31 pm
Location: florida

Re: PDF Download from BLOB help.

Post by ferio-moreno »

thx for the fast reply :)

I just added the stripslashes, it didn't seem to solve the problem. Also, I just noticed that all the file sizes of the PDFs seem to be 64kb. I know they're alot bigger.

i'm stumped! lol
User avatar
andyhoneycutt
Forum Contributor
Posts: 468
Joined: Wed Aug 27, 2008 10:02 am
Location: Idaho Falls

Re: PDF Download from BLOB help.

Post by andyhoneycutt »

Would you post the code for your forms class (at least the relevant code in it) and also the statement where you insert the pdf into the database?

Thanks,
Andy
ferio-moreno
Forum Commoner
Posts: 30
Joined: Tue Feb 28, 2006 10:31 pm
Location: florida

Re: PDF Download from BLOB help.

Post by ferio-moreno »

Sure thing,

class.forms.php

Code: Select all

 
<?php
    require_once($_SERVER['DOCUMENT_ROOT'].'/ext/classes/class.data_Suncoast.php');
    
    class forms{
        function uploadForm($department,$form_title,$form_type,$file_name,$file_size,$file_data){
            $db = new data_Suncoast;
            $sql = "INSERT INTO data_Suncoast.smi_forms(department,form_title,form_type,file_name,file_size,file_data)
                    VALUES($department,'".$form_title."','".$form_type."','".$file_name."','".$file_size."','$file_data')";
            $rs = $db->conn->Execute($sql);
        }
 
    function downloadForm($id){
        $db = new data_Suncoast;
        $sql = "SELECT form_type,file_size,file_data FROM data_Suncoast.smi_forms WHERE id = ".$id;
        $rs = $db->conn->Execute($sql);
        return $rs;
    }
    }
?>
 
upload_form_submit.php

Code: Select all

 
<?php
    require_once($_SERVER['DOCUMENT_ROOT'].'/ext/classes/class.forms.php');
    $form = new forms;
    
    $tmpName = $_FILES['form_upload']['tmp_name'];
    $fp      = fopen($tmpName, 'r');
    $content = fread($fp, filesize($tmpName));
    $content = addslashes($content);
    fclose($fp);
    
    $form->uploadForm($_POST['department'],$_POST['form_title'],$_FILES['form_upload']['type'],$_FILES['form_upload']['name'],$_FILES['form_upload']['size'],$content);
?>
 
 
download.php

Code: Select all

<?php
    require_once($_SERVER['DOCUMENT_ROOT'].'/ext/classes/class.forms.php');
    $forms = new forms;
    $file = $forms->downloadForm($_GET['file_id']);
    header("Content-type: ".$file->fields('form_type'));
    header("Content-Disposition: attachment; filename=".$_GET['file_name']);
    echo stripslashes($file->fields('file_data'));
?>

hope it helps :)
ferio-moreno
Forum Commoner
Posts: 30
Joined: Tue Feb 28, 2006 10:31 pm
Location: florida

Re: PDF Download from BLOB help.

Post by ferio-moreno »

any help please?
Post Reply