Store/Read file in Postgres-DB - php

I'm trying to save a file in the database and I'm having trouble retrieving it.
The result is not a valid file.
Isolating part of the code, I think this should work:
$content = pg_escape_bytea (file_get_contents($tmp)); //image.jpg
header('Content-type: ' . $mime); // image/jpg
echo pg_unescape_bytea($content); exit;
$content is stored, but I can't read it again!
What to do?

A possible explanation would be that you're in this configuration:
the PHP side uses a 8.4 or older libpq library
Server is 9.0 or newer
the bytea_output config parameter is left to its default value (hex)
In this case, pg_unescape_bytea will not properly decode the bytea contents coming in text format from the database. As a workaround, you may add to the php code:
pg_query("SET bytea_output=escape");
before SELECTing the bytea contents and see if that makes a difference. If it does solve the problem you may make it a persistent setting for the database by issuing:
ALTER DATABASE mydb SET bytea_output=escape;
until upgrading libpq to a newer version.

Related

PHP-Firebird (ibase_connect = CHARACTER SET iso-8859-1 is not defined)

im new about firebird. I'm trying to connect between php and firebird. This is the code :
$host='192.168.12.1:D:/DB/ALFABETA.FDB';
$username='john.doe';
$password='123456789';
$database='ALFABETA';
$dbh=ibase_connect($host,$username,$password) or die (ibase_errmsg());
$sth= ibase_query($dbh) or die (ibase_errmsg());
But after i run the code in browser, the warning statement is coming up. Help. What i should do ?
Warning Statement
Warning: ibase_connect(): bad parameters on attach or create database
CHARACTER SET iso-8859-1 is not defined in
/var/www/fortrainingcrud/connect_db.php on line 7 bad parameters on
attach or create database CHARACTER SET iso-8859-1 is not defined
I finally got the answer ! Here it is :
In php.ini, i add extension=php_interbase.dll
Restart the Apache service
The code that i've write before, works well
You aren't setting the connection charset yet you get an error about it. That suggests that PHP is taking the value from somewhere else and the first candidate is the ibase.default_charset directive. You can see its current value with var_dump(ini_get('ibase.default_charset')); or simply by running phpinfo().
You can either change the directive yourself or, even better, specify the encoding at ibase_connect() so your code doesn't break randomly depending on server configuration.
As about iso-8859-1, it seems the appropriate syntax for Firebird is ISO8859_1 (assuming you really want that encoding).

Retrieving image from postgresql database using Yii2 (php)

We have a problem retrieving uploaded image from postgres database with yii2
we store image with that way to the db:
$data = pg_escape_bytea(file_get_contents($model->CheckIfAvatarExists(Yii::$app->user->identity->username)));
$profile->passphoto = new Expression("'{$data}'");
$profile->save();
stores image perfectly
but when we try to display image, it is not working:
header('Content-type: image/png');
echo pg_unescape_bytea( $profile->passphoto);
I think the big problem is data after escaped it wont back to original be unescape
Any solutions?
Consider this user-contributed comment in pg_unescape_bytea online doc:
PostgreSQL 9.0 introduced "hex" as the new default format for encoding
binary data. Because "pg_unescape_bytea" only works with the old
"escape" format, you need to do pg_query('SET bytea_output =
"escape";'); before executing your select queries.
In fact, it's only true when the client library is older than 9.0 (libq.so.5.2 I believe, now EOL'ed).
If that is your case, that explains the problem of wrong unescaping.
To force bytea_output to escape to decode properly, you may either :
dynamically with SET bytea_output=escape; as a SQL command in the session
or statically for the whole database: ALTER DATABASE SET bytea_output=escape;
or in postgresql.conf for the whole instance.
The ideal solution would be to upgrade the libpq client library to a newer version, in which case none of this is necessary, it just works.

Htmlentities function returns null, after upgrading PHP and MySQL

I have a web server with a hundred applications developed on PHP 5.2.3, Apache 2.2.4, MySQL 5.0.37 (databases with utf8_general_ci character set).
I set up a new machine to which I will port the web server that has PHP 5.5.12 (default_charset=UTF-8), Apache 2.4.9 (html head with content="text/html; charset=utf-8", MySQL 5.6.17 (test database with utf8_general_ci character set).
In the PHP scripts I used many time the htmlentities function in a form like htmlentities($var) (ok not the best way but I was a beginner) in which $var is text extracted from MySQL and contain special chars like "è" (when I save in the dbase I use set var=_utf8'è').
The problem is that on the new server the htmlentities function returns nothing (the same code in the old server return the correct &egrave).
After some googling I found a solution that is rewrite the call as htmlentities(utf8_encode($var)), but you know to correct hundred application...
There is a solution (with .ini variables, database charset modification or similar) to maintain the "old" functionality of the htmlentities function?
[EDIT]
Thanks to CBroe's suggestion, I can solve the problem linked to MySQL using the function mysql_set_charset called when I connect to the DB (a common function).
But, the problem still remain for a generic conversion. For example, if I want to print the euro symbol and I want to to use the htmlentities function instead to remember the html code.
As other note if I use htmlentities("è",ENT_QUOTES,'UTF-8') the result is nothing, if I use htmlentities("è",ENT_QUOTES,'ISO-8859-1') or htmlentities("è",ENT_QUOTES,'') the result is right.
PS:
The problem is the same if I pass a string with a special char like "abcdè".
[EDIT]
I found a solution for the same problem on the ODBC connection:
https://www.saotn.org/php-56-default_charset-change-may-break-html-output/
so setting default_charset = "iso-8859-1" the old applications still work fine.

retrieving blob from database loses part of the file

I'm having a problem with retrieving some blob data from sql server and processing that in php to convert it back into an image.
Here is an example of one of the blobs (varbinary) in the database:
0xFFD8FFE000104A46494600010101006000600000FFDB0043000D090A0B0A080D0B0A0B0E0E0D0F13201513121213271C1E17202E2931302E292D2C333A4A3E333646372C2D405741464C4E525352323E5A615A50604A51524FFFDB0043010E0E0E131113261515264F352D354F4F4F4F4F4F4F4F4F4F4F4F4F4F4F4F4F4F4F4F4F4F4F4F4F4F4F4F4F4F4F4F4F4F4F4F4F4F4F4F4F4F4F4F4F4F4F4F4F4FFFC000110800FA00FA03012200021101031101FFC4001F0000010501010101010100000000000000000102030405060708090A0BFFC400B5100002010303020403050504040000017D01020300041105122131410613516107227114328191A1082342B1C11552D1F02433627282090A161718191A25262728292A3435363738393A434445464748494A535455565758595A636465666768696A737475767778797A838485868788898A92939495969798999AA2A3A4A5A6A7A8A9AAB2B3B4B5B6B7B8B9BAC2C3C4C5C6C7C8C9CAD2D3D4D5D6D7D8D9DAE1E2E3E4E5E6E7E8E9EAF1F2F3F4F5F6F7F8F9FAFFC4001F0100030101010101010101010000000000000102030405060708090A0BFFC400B51100020102040403040705040400010277000102031104052131061241510761711322328108144291A1B1C109233352F0156272D10A162434E125F11718191A262728292A35363738393A434445464748494A535455565758595A636465666768696A737475767778797A82838485868788898A92939495969798999AA2A3A4A5A6A7A8A9AAB2B3B4B5B6B7B8B9BAC2C3C4C5C6C7C8C9CAD2D3D4D5D6D7D8D9DAE2E3E4E5E6E7E8E9EAF2F3F4F5F6F7F8F9FAFFDA000C03010002110311003F00EBAFD7E7527FBB59B28E2B56F86761F6359B30E0D7A38797BA6125A904838E6AB38E2ADC99239AAD20AED818959C540E2AC3D42D5A0C81BA5466A56A8CD4B1919A6114F3DE9A6A58C6F1EB4D3B7A53B38ED9A427DA95860ACA8C1949CA9C8356EDA75FB3F9625113F9BBD988FBC2A8E4775A3E5CD43498D3B172ED61118B98721659CAAE3B28A921B5886A7736B202C150988FA90335477B796620C761E76D4925CCD2BABBB92CBD0FA54F2BEE526598E78E0580CAADE7DB295D831B581F5ACFEA4FB9CD3B93C9A4A12B09BB8DC518A7525500868C52D266900838ED4A49C7DDC51ED461BD45030A5033DF140A08CD21A1E14776153DA0517B6DCF3E6AFF003AAC1467AD4F6808BDB6C74F357F9D4CB6348EE7A09E188A314ADF7A92BCF66E348E693069E68A41712F07CAB59B28E6B52E86631F5ACF9508AE8C33D0E39EE557E9555EAD482AB49D2BD08183DCACE3D6A063561EA06AD4085B9A88D4AC2A36A9652236A61EF4F34D348637F1C5369DC7A5348A9180507D290AAFA54B15AC931F9178F5AD186C553E6719F5CD6152B461B9A469B6648427EE8A3691D456DF951A1F97039EF51DC1557F9555B8E7B66B9FEB8AFB1AFB13242B303804D3769C55F0E40DCBF2A7438A64AB8200C1471F7B1D29FD690BD91468A9E583CBC7A7AD4722143C8E3D6B68558CF622506861F4A4C529A4AD0810D1807BD2F5A4E3D0D318BD28A323B52D21A018F43562D4FF00A5DBFF00D755FE7502B1C74A9ED4E6F6DBFEBAAFF3A996C5C773D09BAD14A7A9A2BCD7B9B8DA29475A29082E8ED873EF59AF3EDCF15A5763FD1CFB1159128E0FB574E1926B538AA2F788D9B7AE718AAEF52AFF00AB3F5A89EBD08A322BBD42D533D42D5A8D10B544D52B544D498C61A61A71A69A9631BDAB474FD364BA6590A9110FD7D6A2D32C5F50BA112F083976F415D3DD4F1DBC09142005DBB540EA6B8F115B93446F4A17DCA0C91C24AC58F73DAB3E6B83E66064FA9ABB70AC63545FE2E4D675DAEC2B10E5BD3D2BCB6DB77675DAC4734FF74EC24F40077AAF2C37329DCFF27A0F4A9D9763139E9D4FBD3D14C8B82480DF99A57B0CA2AAD0F59467BFFF005EA600CA9B46016E9E94A6D2477F2E24271DEA636C2D700B8761E9D050E43512A90EA76BF5ED9AB02D9DA262C98CF38ED4D9641B36B2641EFE95620BB678C212368FBA69A9B8EA84E3732E64F2E42B51F7AB97199D8F1F30F4EF54CF5AF52854E789C9521CAC4A39ED49CD193DAB720773DC51483777A0500870CFAD4F6A7FD32DF27FE5AAFF003AAFF854D6E7FD2EDCFF00D355FE7532D8B8EE7A29EA696985BE63466BCD6B53A5A149E6939A6E79A7669582C3AEFF00E3DDFF000FE7553C8C59C8C06E6299AB575FEA24FF0074D2DA10F6AA3B6083550938C558E5946ECC1404401FB3138A864AB0C4AC623EC8481559CD7AB4F557399AD481EA17A95EA162335B0113546C69EC6A327D6932861A631F41927A0A79AB5A3C1F6AD5EDE223E50DBDBE82B39CB953638ABB3A0B6B75D2F4600E1659802E7B8CD5447592F2495CE6281368F734DD6AE45E4F2C71B7CB11C63D71FF00D7355A43E445144DC1DBE63FD4F4AF22A36F56764158B1713ED185FBE7A0F4AADE5012967396DB9C9EC2A1B498493493CDF71326AC5AFF00A4106438327EF256FEEAF65AE67A1B2D4AF2A858BCD97E541D07AD578E59EE24D907C89DCF7A7DF5C7DB270B18C44A78156ADD04716C8C727A9A1E88D231B8C5490831A938EE7B9A916D58617A9AB914585AB10C79901238AC9C8D5452316FAD5A3F982E57B8AAF1C6228CEC3943D33D45749790064615CD5DB794D81C63AD5C5DCCE6BA906F68E40E3AA9E29972143878FEEBF3F4A567DEC31DC5315832321EC78AEAC3CF96461523CD123E293AD1D3AD1B881C015EAA38838F53467D28DCC7D28FAD0342807D6A5B7E2E60FF00AE8BFCEA206A48BFD7C3FF005D17F9D44B62E1B9E82EDF391407C1A809F9CD2E6BCE7B9E872126FC9A5CD459E69327D690F91176EBFD53FF00B86A0D3E4F9244F4008AB171F748F55359F64FB657FF00AE59FCAB5A6AF03CD968CA3236770F73559CD485B2C7DF9A85CD7A94D591CF2DC89AA16A95AA1635A08889E2A36A7B546D4994349AD5F0F15B79AEEF1FFE5942715946AED9B797A7DF64F2C8303DB35856D60CB86E1619621DCFCF2924FE26A3D4A5DF3CA7D5B68FA0A34A62F247BB9232DF90AACC7794CFF13126BCC99D682572904708EAE79AB8D2F9566C13EF487693FCEA9121A6690F45185AB1102E2353D867F1358C91AC092CED8B0040E2B6ECED323245416FB502A8EB5A11CC1547A560F53A2D65A129815476A458C29A4F3F70EB8A723231E5AA58B55B89291B4E6B94D6D76C848AE9E7EF83915CEEB684A034E1B8DAF74C781C1033D8D21936CC73DCD436E712107B1A27EBB87635D0B730E84CE307EB49BB1DA856DC9CF5A4DD8E82BD5A52BC4E29AB30DD9ED4A293713E94035A123854911FDFC5FEFAFF003A8853E33FBE8BFDF1FCEA27B33486E7745BE63466A266F98D2E6BCBE6D4F5F97424CF349BA99BA9334B98394D6B8EDF4AC8B6931337BC26B52EDB010FD6B091B13C7CF5CAFE75D9415E078D5374400FCDF8531CD2670E47A714D735E8C0E796E46E6A16EB5231A85CF35608631A8C9A79EB4C35230C66AC5B93E5DC21E8D1E07E073500CF6A9A2C88E618392A39F4E6B2A9F0B2E1B8BA4FEEE2B990F25206FC3354F70523FD95AD3853C8B1D41B8C14403F1AC763D7DEBCD99D687827CA3EBD6A682531C8A4F4AAD26E3136CEA3029DB5C8DC33D060562D5CD63A1B90CBE6B02B524B706206A0D023692ECC67A119FA55BF105A9B78C48A383C560D6A74A9AD8CC9B5094F0A702A34BF981FF5BC55668D8EDF43D6ABCD6F299F099D99AB514CCDB77365350949E5F34B76EB716FC761541207DC00EDD2B492CDD2DF2FDEB2692668B639B2BB249298C77023DAACDF26C91F1E954D9B0EA3DB15BC753092B3278FEE7E00D2E403D29909C02A7E94F3807AD7A3877EE9C7556A1BBFD9A5A6E467A9A3BD74198EA7A7FAD8FF00DF1FCE998E6957FD627FBC3F9D673D8D29FC476A4FCD4035113F31A037BD78CE5A9EEA5A1293499A66EC8A4CFBD2B8F94D6BF6C4087DCFF2AE7E5930411F515BD7EACF6CAB18258B1007E06AA8D1375B1F324C4B8E31D057A546A460ACCF0AA2BD8C12FF00383EA69CE6A1991A29DA29061D1B045399ABD0A6EE8E59218C6A3634E635113CD5B62109E69B9A526999A91A2752315247200768E49E83D4D54DC7B54D68C12E559BEF745CF407D6A25AA2A3B93DE964B4960279D819BEB9AA3044D3B6101356E67F3B53981FBB2A613E95B3E1DB248ECDA46197663D6BCBACEC7753573321D3A409964A78B07C80178AEA19507DE038A8A578829E0015CAE4CE88BF22AE836EB0C92123E622A6D5944F008DAAC5828C6F1C67A5173106527BE2A1EC24D739C97D95F7151DAACC7A7CAD8C2D5876FB3DC0571D7A1AD582E1368E94B999B495B5452B4D2C4443CA79F4AB374A8622A3153492A30EB542E1F00806B36114D9CBEA400958565C9D8E38AD3D539989AA823530B67BF4AE9A7A98D4B5C8E13B816F4EB4F3D6990A94420F7A757A9423CB1386A3BB1452D2519AD8CC70A553FBC4FF00787F3A666957EFA7FBC3F9D44FE134A7F123AF2DCD01BB5464F34A1ABC093D4FA28AD0933466A3DD499A5CC3E537E6B8F261DE98387DBF9D73BA86A5792AC9990AF94410ABC6715AF7C7FD02E71D5641FD2B33529A16B584018924EA074FC6BD4A6D276B1E04E3A261ADAABC51EA11F2B346A4FD6B318F356524927F0ACF00E5ECE4E7FDDAA6581031E95DD877A58E6A82139A8C9A5269A48ADEE662134DA09A6F7A4C63B3C629A4D1DE92A58CBF6204CD193F7A3048F7AE834563F63955BAA3FE86B99B393CAFDEFA657F3AEAB4D402DE439CEE406BCFC4C3A9D54643A69B1D4D67C92B4ADB16A4BB279C557B7521B9EA6BCE67A11D8B0754FB3CBE511B401F2D52BDD6A461B53A54D7712CCA32391D0D65496CCA4E7A5349013FDADEF634047FAB3D6A78EE191704F4AAF6A0451FD698EC779C77A9B1772F0BA39EB4493175EB544120669EAD52D0D33235490ADE6C1D319AAEA4EDE69DA83EFD41C8FA521E302BD0C2C2ECE0C4480D2504D257A28E3168A4CFAD1400B9A33F32FF00BC292933F32FD47F3ACEA7C2CD69FC48EACB734A0F15193CD1BB8AF9D93D4FA58AD1126EE69335196A326A2E558DBBCFF8F7BC5F707F9573F7C77C413BA2EE07DB35BF77D6E97D5335CDDD36DBDB7CFDD910A1AF669BF78F9EA8BDC449A14E1F549ADE53F2DE4650FD7B5542A622D0BFDE8C953F8557676B5BB8E65E1A270D57B5260FA9DC3AFDD760C3F1506BB68FC4CE49EC5763C530F5A09A6935D06680F5A4CD266933CD2B8C5CD2F5229B9A01C8C54817AC2D5AFA786D22EEF976F415D5AFEEAFA5CB05855420F73591A1491DBD94D328C344BBD8FA9EC2AF244FA9DC44DCADBC632D8EE7AD7156779599D14F4571973C48CB54F64AEFBA3702AEEA3CB0980C2B923F2AAF06715E74D599E841DD15277BB462028AA734F70576B29CD6BDC862BD2B26691C310D42772C83CD980C714F456FBCEDF853954B76A56047149B10F2462A26976F3E94D76C0A8243B948CF5A561DCA3F7EE4B1EE69D21F9AA4316C9C2FA75A8E71B64C577E19EA70D75A5C8F3475141A4AEF472875A5A4A4CD002E68EEBF5149413C8FA8ACEA7C2CD69FC48E98B73416A8F3CD2935F3527AB3E9E0B443B751BA999A4DD50D9A58E82ECE6593FDA8BFA560CF03CEF6CD1A33947C90066B667901788E7AC5CFE9556CE6961B76F29F67CE735ED2BF3687CEB57A666EA760EBE64A50A8273823A5510E5E3524E5B183F856EADD3CFACCF633BEEF36018CFF00785624D1986468FD0D77D27A9C324464FA5213499A435BDCCC5CD349A4CF18A4A96C63B34A2984851C9A8C4DF38C0E2B395448A49B37F48977C3736C07CD28014575413FB3B492A89B9D5318FEF31AE57C1B119F599663F7628FF535DC100F519AF3AB4EF2378E88E7ECE2964D17CABC18983B67D8E6ABDB36C90AC9C1ADCBC508370180E727EB58D771EE395E0D734B53AE9BBA2C3142307154668E2673902A9CB732C4486E71555EEDC9EF49459AA9A34D5214538C66A95CB2282462AA9B96231CD44CCCFD7A51CA3E74C19B71E299302222475E2A555A193CC961807591C0A109BD0596D645D2D75123E51279647B7AD67DD3ED5898F520835DA6B30C76DE19BB8F036C680FE35E7D34DE6451027900935BD26E2EE8E5A9A93070DD0D2E7D2A9838EF4F12153D6BB635FB9CCE059A4A884C0F5A90303D0D6CA69EC4D9A173CD21EDF5A2909CE3EB4AA7C2CBA7F123A3CF231484D349C5216AF989BD59F530D90FCD2D47BA937D4967516DA218DC48F31661D38E2ABDF591B1B490821CBB8C0EF8F6AD09A7BD9485850431EF20C8DC9C0F6A79850092E243E63A82CA0FAE2BDB8DD3BB3E5B9DDAC71335E6FD4DAE63408D18E07B8F5A65C4BE7307CFDE1CD5BB6D0B50954CBE510B3A93EFD6ABDED8C96056275233CE4F7AEEA6D5CC5950D37049C0A711EB504B29E8BC0AB9CD4484AE39DC2F00E4D44F31E82A3CD318F35CF29B668A23CBE7BD0AD51679A506B36C6767E0120FDB87F16E5FCB15D7D707E06B9F2B579ADD88C4D1E47D457795CD35A944379099AD5D14E1BAAFD6B9F130953246083820F635D35739AE5ABDA5C1BB89498643FBC03F84FAD66D5CDE94EDA333EEA30C322B39D00CF15A424120E0D579630727BD24EC6EF5286DE4D3D56A4F2E9CAB81498E2302D4DA2DBB5E6BB1381FBBB7F9D8FF002A82566256288664738515D4E9B60BA669E538323F2EDEA69C5133919DE30B954D0265CE1A690281EA2BCFFD2B6BC537C6E750F255BF770F18CF7AC51CE735BC11CD37A8B9E682690521AB20783C50AC45314D2D17B01616604734FCF4F4C8AA99A91642383C8AD15576B31C56A75CC90C9830C83E8D50CB0C89D47E559EB2064478DF21AA54BC9A2E0938FCEBC6A90D59F434EA7BA897752EE14A9770CA3F78801F55A5CDBFFCF43F9563CA6AA68F41750C857D6AA4D90B8CA8C8DBCF6AB16E2558B133066CF503B5569C095976329F30E00AF5E9EF63E59E858B4903C4071F2FF2AE5BC5D7704D7104513067849DC7B0CF6A6EA5AABDA1682062B21055F1DAB9991C9249249279AE88C39657B8AF715DF9EB559CF34AE735196C9C553770485CD358F14D071C50C722A6E31334A0E2980E6941A406868D75F64D62D2E3380B200DF43C57ABE73C8E95E32C4ED38EA3915EAFA1DE8BED1AD6E01C96401BEA2B0A8B5B948BF4D911648D91C02AC3041A5A2B30395D474796DD9A4B40593AECEE2B36397770C39AEE1C771D6B2352D2E1B866920C24A3AE3BD0CE884FB984101A8A62116ACBC13C1C4B190077ED4FD3EC5B50BC45C1F290EE73DB1E94AC68DD9167C39A61321BDB81F37FCB307B0F5AD0D7AF16CB4E9A627955E3DCD6778975F4D2E35B5B12A67041623A281DAB96D775F7D52DE18F694C72E33C1356A37306FA98CEED23B3B1CB31C9A41D293BD28AD92B1930CE29B4A4E29280115B0DCD3CFAD447AD3D4E45260389A33C534D266931A2EE9F77F66981750F19E194D6CEEB6B8198DF61F46AE6B357ED184B1156EAB5CD5A0B73BF0B59AF74BF2DB32F207E22A2C37F78D2473CD0FDC6C8F4352FDBFD6DC572D99E85E27A816C118049F6AE37ED522DC4E232C3CB96419279EB9C0AE81F5B813384635CADD5DE67B897CB004A59BE99AF568C923E7E507D4CBBD9CDC5DCB331E5DB35519B9A1CF7A8F76456CD99D818F35131C1A7138A63549404F7A33C5341E293348001E714BDEA3CF34ECE2802406BB1F00DFF00171A739E87CC8C7B77AE2C1E6ADE977CDA76A96F76A4E11B0C3D54F5A892BA291EB7BA862150924003A9359BA8EB9A7E9F00964983B30CAA29E4D70DAD7892F35362818C50F645FEB5928B651D26BBE2B82D51A0B02259BA17ECB5C9D9EB77D6974D3ACECCCC72E18E41ACB273DE9335A722B0B99F43B787C5F6EEA7CFB7393D40E41AA37DE277F25A0B08C5BC47D3A9FC6B96C907228672DD4E6A79115CFA0B34CF239662493D49A889ED413DA93156886EE28A507AD213487AD00293934B9A68EB4B4AE035A950F6A0F2298386A00918E2909E828278A4079A4315BB0AB5A7B62E369FE21550F269F136C915BD0D44D5D1A52972C93361979A660D4B9CF3EB498AF3EE7B3BEA5E9E49D24D8CE738078AAD3330898B13E95A779008E601B04B02E0F5E09E2B375120468A3B9E6BDA492D8F024D9418E73509E0D399B0698E7BD0D92048A6934DCD373CE2A4629348C78A46A4CE452012941E29A6941ED400EC9A5CD37346690C7BC8CD8DCC4E38E69377BD333403400FCD349CD266933E9400B9C5213484D25002F7A052668CD218A7DE9282690726801451DE8A4A005A69EB4A4D35A900ECF145276A33C5200079A78EA6A35E94E5EB81C9A18D1B16927996CA4F51C54D556C5764441EA4E7156ABCFAB1B48F6684AF046BDEE56FE75CE406E39CE38CD63EA4FF00BD51E82B5AFE42DA95D0EC8C10718EDD7F5ACEBD11B85CE370EB5EA2678D35A996C78A613DAAD340AC3E538AAD242CA7D69DC8223C1E69A4F7A71F7EB4C348001C8E6901EDDE9338349FC5400ECD341F9A90D2FD295C63FB52669074A4273400A0F3467D29A0D2E68016933484D264D003A8A4A2800CF140A28A4004D03AD21A5A062D266933484D20173CD21EB477A0FB53003D29B93C0A5278A1137375C0F5A402806A7876A7CCDD69B26D00246327B9A5588F1938A4C116AD6567B8C8E17157B9ACD122C2B84FBD8EB57926528A491C8AE5AB0BBB9E861AA5A3666DEA5118B5099C0F965C375CE1BB8AC3BE4733BB0E8715B37FFF001F03FDDAA173F78FE15DE91C13DCCB32489DCD219D9860E2A69FEED556E94729023B67A8E69B8C8E3A52B75A58BA5160217E29A1B9AB173F74D551D68E50B8FCD00D3475A5FF001A2C2B8B9A4CD21EB41EB4728EE3BE9453569F4AC171A4D20E94E341E94EC2B894B4DEF4EF4A560B871EB471486834728EE1C519068ED42F4A5CA17038A4E29DDA994582E282297228EC686E94728EE31BDBAD3F6155FAD347DEA79A2C171C8CA9F5A469CB74E951B50B4B9463D4E7AD2F9AC063D284E82987A9A8944D29B3FFD9
If i copy and paste that out of the database and do the following:
Pack it into binary:
$data = pack("H*", substr($data, 2);
Then encode it with base64:
base64_encode($data);
Then I can print that out and I get the full base64 data, which I can then convert into the image:
/9j/4AAQSkZJRgABAQEAYABgAAD/2wBDAA0JCgsKCA0LCgsODg0PEyAVExISEyccHhcgLikx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NskVvQ1E1dGlKXLJM2GXmmYNS5zz60mK8+57O+penknSTYznOAeKrTMwiYsT6Vp3kAjmAbBLAuD14J4rN1EgRoo7nmvaSS2PAk2UGOc1CeDTmbBpjnvQ2SBIppNNzTc84qRik0jHikakzkUgEpQeKaaUHtQA7Jpc03NGaQx7yM2NzE445pN3vTM0A0APzTSc0maTPpQAucUhNITSUAL3oFJmjNIYp96SgmkHJoAUUd6KSgBaaetKTTWpAOzxRSdqM8UgAHmnjqajXpTl64HJoY0bFpJ5lspPUcVNVWxXZEQepOcVarz6sbSPZoSvBGve5W/nXOQG45zjjNY+pP8AvVHoK1r+QtqV0OyMEHGO3X9azr0RuFzjcOteomeNNamWx4phParTQKw+U4qtJCyn1p3IIjweaaT3px9+tMNIAByOaQHt3pM4NJ/FQA7NNB+akNL9KVxj+1JmkHSkJzQAoPNGfSmg0uaAFpM0hNJk0AOopKKADPFAoopABNA60hpaBi0maTNITSAXPNIetHeg+1MAPSm5PApSeKETc3XA9aQCgGp4dqfM3WmybQAkYye5pViPGTikwRatZWe4yOFxV7ms0SLCuE+9jrV5JlKKSRyK5asLu56GGqWjZm3qURi1CZwPllw3XOG7isO+RzO7DocVs3//AB8D/dqhc/eP4V3pHBPcyzJInc0hnZhg4qaf7tVW6UcpAjtnqOabjI46UrdaWLpRYCF+KaG5qxc/dNVR1o5QuPzQDTR1pf8AGiwri5pM0h60HrRyjuO+lFNWn0rBcaTSDpTjQelOwriUtN7070pWC4cetHFIaDRyjuHFGQaO1C9KXKFwOKTindqZRYLigilyKOxobpRyjuMb260/YVX600fep5osFxyMqfWkact06VG1C0uUY9TnrS+awGPShOgph6molE0ps//Z
All well and good.
Now if I try and do this with a script... So first I select the data from the db:
$s = odbc_prepare($ebs, "select TOP 1
binary_object as img
from BLOBS
where owner_ref = 271040");
$q = odbc_execute($s);
$record = odbc_fetch_object($s);
Then I encode that data into base64, then it looks as if it's worked, but I actually only get about 2/3 of the data:
echo ( base64_encode($record->img) );
/9j/4AAQSk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Ne0r82h861emZup2Dr5kpQqCc4Iw==
To answer a question I can forsee. I cannot pack the data that comes straight out of the database like that, because when I echo it out its a load of gibberish that starts with:
ÿØÿàJFIF``ÿÛC ' .)10.)-,3:J>36F7
Does anyone have any idea where I might be going wrong?
Cheers.
EDIT Just to clarify as well, I think whatever is wrong is how I am selecting the data, because if I simply set the file header to image/jpeg and echo out $record->img without changing anything, I get about two thirds of the image, then the bottom bit is cut off.
Cheers.
Managed to get this working now. The problem was in the php.ini settings for odbc.
By default odbc has a byte limit of 4096 for data returned, so it was cutting it off at that point.
By changing this in php.ini
odbc.defaultlrl = 60000
I am now able to get all the data back and construct the image fully.
I searched a bit and came across an interesting bit of information here:
Base64-encoded data takes about 33% more space than the original data.
It sounds like the encoded string does not have enough memory. I can only guess, why that would be, but maybe the echo-command (or something else) only reserves enough space for $record->img?
Maybe you can try to store the encoded string in a variable with more space before echo-ing it?
I hope this helps at...
We had the exact same issue on our WAMP server. We switched from an oracle database to a MsSql database. The old oracle server was set up years ago so we are not sure of the settings (none of us had access to it) and the php code for oracle addressed the size in the code. There was nothing comparable in php for mssql that would do this, and even when we tried to force the size in the header it did nothing. Finally found this and some other articles and by changing 4 different php.ini files in our servers we were able to show the entire image by increasing the file size to 60000 We had to update php.ini php.ini-development php.ini-production and phpforApache on the odbc.defaultlrl line and increase them from the 4096 to 60000.

Accents in uploaded file being replaced with '?'

I am building a data import tool for the admin section of a website I am working on. The data is in both French and English, and contains many accented characters. Whenever I attempt to upload a file, parse the data, and store it in my MySQL database, the accents are replaced with '?'.
I have text files containing data (charset is iso-8859-1) which I upload to my server using CodeIgniter's file upload library. I then read the file in PHP.
My code is similar to this:
$this->upload->do_upload()
$data = array('upload_data' => $this->upload->data());
$fileHandle = fopen($data['upload_data']['full_path'], "r");
while (($line = fgets($fileHandle)) !== false) {
echo $line;
}
This produces lines with accents replaced with '?'. Everything else is correct.
If I download my uploaded file from my server over FTP, the charset is still iso-8850-1, but a diff reveals that the file has changed. However, if I open the file in TextEdit, it displays properly.
I attempted to use PHP's stream_encoding method to explicitly set my file stream to iso-8859-1, but my build of PHP does not have the method.
After running out of ideas, I tried wrapping my strings in both utf8_encode and utf8_decode. Neither worked.
If anyone has any suggestions about things I could try, I would be extremely grateful.
It's Important to see if the corruption is happening before or after the query is being issued to mySQL. There are too many possible things happening here to be able to pinpoint it. Are you able to output your MySql to check this?
Assuming that your query IS properly formed (no corruption at the stage the query is being outputted) there are a couple of things that you should check.
What is the character encoding of the database itself? (collation)
What is the Charset of the connection - this may not be set up correctly in your mysql config and can be manually set using the 'SET NAMES' command
In my own application I issue a 'SET NAMES utf8' as my first query after establishing a connection as I am unable to change the MySQL config.
See this.
http://dev.mysql.com/doc/refman/5.0/en/charset-connection.html
Edit: If the issue is not related to mysql I'd check the following
You say the encoding of the file is 'charset is iso-8859-1' - can I ask how you are sure of this?
What happens if you save the file itself as utf8 (Without BOM) and try to reprocess it?
What is the encoding of the php file that is performing the conversion? (What are you using to write your php - it may be 'managing' this for you in an undesired way)
(an aside) Are the files you are processing suitable for processing using fgetcsv instead?
http://php.net/manual/en/function.fgetcsv.php
Files uploaded to your server should be returned the same on download. That means, the encoding of the file (which is just a bunch of binary data) should not be changed. Instead you should take care that you are able to store the binary information of that file unchanged.
To achieve that with your database, create a BLOB field. That's the right column type for it. It's just binary data.
Assuming you're using MySQL, this is the reference: The BLOB and TEXT Types, look out for BLOB.
The problem is that you are using iso-8859-1 instead of utf-8. In order to encode it in the correct charset, you should use the iconv function, like so:
$output_string = iconv('utf-8", "utf-8//TRANSLIT", $input_string);
iso-8859-1 does not have the encoding for any sort of accents.
It would be so much better if everything were utf-8, as it handles virtually every character known to man.

Categories