0

使用 PHP,我正在尝试下载已上传到 Oracle 10g 数据库的 blob 文件。我已经看到并模仿了许多我发现的例子。当我访问该页面时,会出现一个文件下载对话框,允许我打开或保存。如果我单击打开,媒体播放器会按原样出现,但从不检索文件。如果我选择保存,我总是会收到一条错误消息,指出“Internet Explorer 无法打开此 Internet 站点。请求的站点不可用或找不到。请稍后再试。”

下面是我的代码,它非常简单,非常类似于我找到的示例。

<?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;

?>
4

2 回答 2

1

使用您的数据库查询并首先执行这里的数据字段是 blob 数据:

$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();
于 2015-07-22T08:29:02.633 回答
0

为您的脚本添加更多错误处理。任何 oci* 函数都可能失败,然后后续步骤也将失败。该文档告诉您如果函数失败会发生什么以及返回值是什么。例如

返回值
返回连接标识符或错误时返回 FALSE。

如果您尽可能晚地设置 Content-type 标头,即直接在第一个输出之前,您可以发送包含某种错误消息的纯文本或 html。

<?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;
于 2010-08-10T16:12:33.437 回答