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 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68
| <?
ob_start();
### GLOBAL ####################################################################
function OuiNon ( $condition ) {
# Retourne OUI, NON, NULL ou $condition
if ($condition===true) return 'OUI';
else if ($condition===false) return 'NON';
else if ($condition===null) return 'NULL';
else return $condition;
}
function hex2bin ( $data ) {
$len = strlen( $data );
return pack( 'H'.$len, $data );
}
$DSNless = 'Driver={Microsoft Access Driver (*.mdb)};Dbq=';
define( '¶', "\n" );
ini_set('error_prepend_string','');
ini_set('error_append_string','');
ini_set('html_errors','Off');
$realData = true; // For debug, I don't use real data from the file
ob_end_clean();
###############################################################################
### PAGE Début ################################################################
###############################################################################
header('Content-Type: text/plain; charset=ASCII');
$cnx = odbc_connect( $DSNless.'test.mdb', '','' ); // , SQL_CUR_USE_IF_NEEDED
### INSERT ####################################################################
if ( $realData ) {
$data = file_get_contents( 'logo.bmp' );
$data = addslashes( $data ); // to avoid a ' to accidentally close a string
} else {
$data = chr(65).chr(66).chr(67).chr(68); // = "ABCD" sure ! Even from UTF-8 PHP file.
}
echo "file_put_contents: ".OuiNon( file_put_contents( 'logo2.bmp', $data )).¶;
echo "data: ".$data.¶;
$SQL = "INSERT INTO `img` ( bin_data ) VALUES ('$data');"; // LONGBINARY côté PHP et Objet OLE côté Access
odbc_exec( $cnx, $SQL );
### SELECT ####################################################################
$qry = odbc_exec($cnx, "SELECT * FROM `img` ORDER BY id DESC");// To get the just last inserted, or WHERE id=".$id);
if ( odbc_fetch_row( $qry ) ) {
//header('Content-type: image/bmp');
// ODBC_BINMODE_PASSTHRU ODBC_BINMODE_RETURN ODBC_BINMODE_CONVERT
echo "odbc_binmode: ".OuiNon( odbc_binmode( $qry, ODBC_BINMODE_RETURN )).¶;
$id = odbc_result( $qry, 'id' );
$data = odbc_result( $qry, 'bin_data' );
echo ¶;
echo "id: ".$id.¶;
echo "file_put_contents: ".OuiNon( file_put_contents( 'logo3.bmp', $data )).¶;
echo "mb_detect_encoding: ".OuiNon( mb_detect_encoding ( $data, 'ASCII', true )).¶;
echo "mb_check_encoding ASCII: ".OuiNon( mb_check_encoding( $data, 'ASCII' )).¶;
echo "data: ".$data.¶;
//mb_convert_encoding( $data, '8bit', 'ASCII' );
//$out = hex2bin($data);
//echo "out: ".$out.¶;
}
else {
header('Content-Type: text/plain; charset=ascii');
return 'error';
}
odbc_close($cnx);
###############################################################################
### PAGE Fin ##################################################################
###############################################################################
?> |
Partager