Welcome Guest, Not a member yet? Register   Sign In
Displaying pdf stored in MS SQL varbinary(MAX) column
#1

[eluser]Benjo[/eluser]
Hello,

I'm working on a CI application using PHP 5 and MS SQL 2005.

I've managed to store some pdf files into the database. However, when I try to display the pdf files, I get the following error: "The file is damaged and could not be repaired."

I use the following lines of code to insert the files in the database:

//using the data() helper function, get all of the data related to the uploaded

$data = array('upload_data' => $this->upload->data());

$fileType = $data['upload_data']['file_type'];
$tmpName = $data['upload_data']['full_path'];
$fileSize = $data['upload_data']['file_size'];

$fp = fopen($tmpName, 'r');
//make the file mysql insert safe
$content = fread($fp, filesize($tmpName));
$content = addslashes($content);
fclose($fp);

$stmt = "INSERT INTO AA_FILES (FILE_OFFICE_ID, FILE_FY, FILE_SHORT_DESC, FILE_TYPE,
FILE_USER, FILE_ACTIVITY_DATE, FILE_OBJECT)
SELECT '$office_id' AS FILE_OFFICE_ID, '$current_fy' AS FILE_FY, ".$this->db- >escape($short_desc)." AS FILE_SHORT_DESC, '$fileType' AS FILE_TYPE, '$editor' AS FILE_USER, getdate() AS FILE_ACTIVITY_DATE,
* FROM OPENROWSET(BULK N'$tmpName', SINGLE_BLOB) AS FILE_OBJECT";

$this->db->query($stmt);

When trying to display the file on the browser, I have the following code:

From the model:

$blob_stmt = "SELECT FILE_TYPE, FILE_OBJECT
FROM AA_FILES
WHERE FILE_ID = $file_id";

$blob_query = $this->db->query($blob_stmt);

$users_data['blob_results'] = $blob_query->row_array();

$this->load->view('view_blob', $users_data);

From the view:

<?php
$fileType = $blob_results['FILE_TYPE'];
$fileData = $blob_results['FILE_OBJECT'];

header("Content-type: ". $fileType);
$pdf = stripslashes($fileData);
echo $pdf;
?>

Can anyone tell me what's wrong with my approach?

Thanks!




Theme © iAndrew 2016 - Forum software by © MyBB