Page 1 of 1

PDF Download from BLOB help.

Posted: Thu Sep 04, 2008 11:09 am
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?

Re: PDF Download from BLOB help.

Posted: Thu Sep 04, 2008 11:32 am
by andyhoneycutt
does your forms class strip slashes before it outputs? you may need a stripslashes in there due to your addslashes.

Re: PDF Download from BLOB help.

Posted: Thu Sep 04, 2008 11:44 am
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

Re: PDF Download from BLOB help.

Posted: Thu Sep 04, 2008 2:40 pm
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

Re: PDF Download from BLOB help.

Posted: Fri Sep 05, 2008 7:50 am
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 :)

Re: PDF Download from BLOB help.

Posted: Mon Sep 08, 2008 8:19 am
by ferio-moreno
any help please?