Page 1 of 1

what makes difference to download a binary file?

Posted: Tue May 27, 2003 8:05 am
by jiehuang001
Please test my website at http://198.108.102.145/vbfiles (use "testit" as both username and password). As you will see, you can upload and downlaod text files without problem. However, if you upload a binary file such as a .doc file, you will get problem to open it after downloading. Below is my code for uploading and downloading, please tell me how to modify this code to make it work for binary files.

Also, in my downloading code, i passed the $file_name variable from previous page. How can I get it from the query directly, with only $id is passed?

Thanks.

Jie Huang


---------------upload file to Oracle database--------------
for ($i=0; $i<$add_number; $i++){

$TmpName = $_FILES['userfile']['tmp_name'][$i];
$FileName = $_FILES['userfile']['name'][$i];
$FileType = $_FILES['userfile']['type'][$i];


if($FileName!=""){
$FilePointer = fopen($TmpName,"r");
$FileContents = fread($FilePointer, filesize($TmpName) );
fclose($FilePointer);

$query = "INSERT INTO lexicon_files VALUES (file_sequence.nextval, '$FileName', '$FileType', EMPTY_BLOB(), 'No', sysdate, '$username') RETURNING file_content INTO :the_blob";
$stmt = OCIParse($conn, $query);

$blob = OCINewDescriptor($conn, OCI_D_LOB);
OCIBindByName($stmt, ":the_blob", &$blob, -1, OCI_B_BLOB);
OCIExecute($stmt, OCI_DEFAULT);
$blob->save($FileContents);
OCICommit($conn);
//OCIFreeDescriptor($blob);
OCIFreeStatement($stmt);
OCILogoff($conn);
}
}


----------------download file from Oracle database-----------------
<?php
include_once("Connection.txt");
$id = $_REQUEST['id'];
$file_name = $_REQUEST['file_name'];

$stmt = OCIParse($conn, "select file_content from lexicon_files where id=".$id);

OCIExecute($stmt);
OCIFetchInto($stmt, &$blob);

header('Content-Type: application/download');
header('Content-Disposition: attachment; filename="'.$file_name.'"');

print $blob[0]->load();

OCIFreeStatement($stmt);
OCILogoff($conn);
?>

Posted: Tue May 27, 2003 2:48 pm
by Wayne Herbert
I think you may have to add slashes to the incoming binary data before loading it to the blob field in order to escape characters used as field definitions, etc.

the right solution!

Posted: Tue May 27, 2003 7:44 pm
by jiehuang001
change
$FilePointer = fopen($TmpName,"r");
to
$FilePointer = fopen($TmpName,"rb");