what makes difference to download a binary file?

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
jiehuang001
Forum Commoner
Posts: 39
Joined: Mon May 12, 2003 12:53 pm

what makes difference to download a binary file?

Post 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);
?>
User avatar
Wayne Herbert
Forum Commoner
Posts: 34
Joined: Tue Apr 29, 2003 3:13 pm
Location: Houston, Texas

Post 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.
jiehuang001
Forum Commoner
Posts: 39
Joined: Mon May 12, 2003 12:53 pm

the right solution!

Post by jiehuang001 »

change
$FilePointer = fopen($TmpName,"r");
to
$FilePointer = fopen($TmpName,"rb");
Post Reply