Domanda

Utilizzo di PHP, sto cercando di scaricare un file di blob che è già stato caricato nel database Oracle 10g. Ho visto e imitato numerosi esempi che ho trovato. Quando accedo alla pagina una finestra Download del file sembra che mi permette di aprire o salvare. Se clicco Open, lettore multimediale viene in su come dovrebbe, ma non recupera il file. Se scelgo Salva, ho sempre arrivare un messaggio di errore che indica "Internet Explorer non è stato in grado di aprire questo sito internet. Il sito richiesto non è disponibile o non può essere trovata. Riprova più tardi."

Di seguito è riportato il mio codice, che è piuttosto semplice e più o meno come gli esempi che ho trovato.

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

?>
È stato utile?

Soluzione

Aggiungi più la gestione degli errori di script. Qualsiasi della funzione oci * può fallire e poi passi successivi saranno anche sicuro. Il documentazione ti dice cosa succede se una funzione non riesce e quale sarà il valore di ritorno. Per es.

Valori restituiti
Restituisce un identificatore di collegamento o FALSE in caso di errore.

Se si imposta l'header Content-type il più tardi possibile, vale a dire direttamente prima della prima uscita, è possibile inviare testo normale o HTML che contiene una sorta di messaggio di errore.

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

Altri suggerimenti

Utilizzare la query db ed eseguire prima dove si trova il campo di dati sono dati 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();
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top