开发者

Downloading BLOB file with Oracle and PHP

Using PHP, I'm trying to download a 开发者_Go百科blob file that has already been uploaded to an Oracle 10g database. I've seen and imitated numerous examples I've found. When I access the page a File Download dialog appears allowing me to Open or Save. If I click Open, media player comes up as it should but never retrieves the file. If I choose Save, I always get an error message stating "Internet Explorer was not able to open this Internet site. The requested site is either unavailable or cannot be found. Please try again later."

Below is my code which is pretty straight forward and pretty much like the examples I've found.

<?php

header('Content-Disposition: attachment; filename='.$_GET['fileName']);
header('Content-length: '.$_GET['fileSize']);
header('Content-type: '.$_GET['mimeType']);

require_once("Include/Application.php");

$connection = oci_connect ($userID, $password, $TNS);

$phpCur = oci_new_cursor($connection);
$stmt = oci_parse($connection, "BEGIN MOR.DOWNLOAD_ATTACHMENT (:morID, :attachmentType, :phpCur); END;");
oci_bind_by_name($stmt, ":morID", $_GET['morID'], -1);
oci_bind_by_name($stmt, ":attachmentType", $_GET['attachmentType'], -1);
oci_bind_by_name($stmt, "phpCur", $phpCur, -1, OCI_B_CURSOR);
oci_execute($stmt);
oci_free_statement($stmt);

$output = '';
oci_execute($phpCur);
while( $row = oci_fetch_array($phpCur) )
    $output .= $row['ATTACHMENT_BL'];

oci_free_statement($phpCur);

oci_close($connection);

echo $output;

exit;

?>


Use your db query and excecute first here is the data field is blob data:

$sql="SELECT FILE_NAME,data,length(data) as filesize  FROM branch_data where id='$id'";
$r = $db->execute($sql);
$filename=$r->data[0]['FILE_NAME'];
$d=$r->data[0]['DATA'];
$filesize = $r->data[0]['FILESIZE'];
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream'); 
header('Content-Disposition: attachment; filename="'.$filename.'"');
header('Cache-Control: must-revalidate');
header('Pragma: public');
header('Content-Length: ' .$filesize);  
echo $d->load();


Add more error handling to your script. Any of the oci* function can fail and then subsequent steps will also fail. The documentation tells you what happens if a function fails and what the return value will be. E.g.

Return Values
Returns a connection identifier or FALSE on error.

If you set the Content-type header as late as possible, i.e. directly before the first output, you can send plain text or html that contains some sort of error message instead.

<?php
// error_reporting/ini_set: for testing purposes only.
error_reporting(E_ALL); ini_set('display_errors', 1);

require_once("Include/Application.php");

$connection = oci_connect ($userID, $password, $TNS);
if ( !$connection) {
  die('database connection failed');
}

$phpCur = oci_new_cursor($connection);
if ( !$phpCur) {
  die('creating cursor failed');
}

$stmt = oci_parse($connection, "BEGIN MOR.DOWNLOAD_ATTACHMENT (:morID, :attachmentType, :phpCur); END;");
if ( !$stmt) {
  die('creating statement failed');
}

// and so on and on. Test the return values of each oci* function.

oci_close($connection);

header('Content-Disposition: attachment; filename='.$_GET['fileName']); // at least at basename() here
header('Content-length: '.$_GET['fileSize']); // strange...
header('Content-type: '.$_GET['mimeType']); // possible but still strange...
echo $output;
exit;
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜