1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
   | /*
 * Ouverture du fichier binaire à stocker dans la table
 */
if (!$handle = fopen('image.jpg','rb')) return 0;
$contenu_fichier = fread($handle, filesize ('image.jpg'));
fclose($handle);
 
/*
 * Clé, type, valeurs à insérer dans la table
 */
$arrVals[0]['key'] = ':pChamp1';
$arrVals[0]['type'] = 'DEFAUT';
$arrVals[0]['valeur'] = 'du texte';
 
$arrVals[1]['key'] = ':pBlob';
$arrVals[1]['type'] = 'BLOB';
$arrVals[1]['valeur'] = $contenu_fichier;
 
/*
 * Insertion dans la table
 */
$stmt = OCIParse($connexion, 'BEGIN INSERT_BLOB(:pChamp1, :pBlob);END;');
$cpt = count($arrVals);
for($i=0; $i<$cpt; $i++) {
    //CAS D'un BLOB
    if ($arrVals[$i]["type"]== 'BLOB') {
        ${'BLOB'.$i} = OCINewDescriptor($connexion,OCI_D_LOB);
        OCIBindByName($stmt, $arrVals[$i]["key"],${'BLOB'.$i}, -1, OCI_B_BLOB);
        ${'BLOB'.$i}->WriteTemporary($arrVals[$i]["valeur"], OCI_TEMP_BLOB);
    }
    // Cas par défaut
    else if ($arrVals[$i]["type"]== 'DEFAUT') {
        OCIBindByName($stmt, $arrVals[$i]["key"], $arrVals[$i]["valeur"]);
    }
}
$r = OCIExecute($stmt, OCI_DEFAULT);
OCICommit($this->ptConnexion);
ocifreestatement($stmt);
for($i=0; $i<$cpt; $i++) {
    if ($arrVals[$i]["type"]== 'BLOB') {
        ${'BLOB'.$i}->close();
        ${'BLOB'.$i}->free();
    }
} | 
Partager