Alatar CMS File Upload NotesBack to Changelog When inserting a file into a database as a BLOB, many people have been told to do various things to the data when creating the database. One of the things I have never seen described anywhere is how to deal with the issue of octal NUL. Since most of the people reading this are not old-school C programmers, a bit of explanation is in order. Character codes in C can be specified as hexadecimal digits, such as '\x40' for '@'. They can also be specified as *octal* digits, in which '\100' serves as '@'. This means that some characters have multiple encodings -- the newline character '\n' can also be encoded as '\x0A' or as '\012'. In C or C++, these encodings are 100% identical for all intents and purposes. MySQL, for whatever reason, likes to store the NUL sequence '\0' as '\000' -- which is the same character in a C string, but in PHP comes out as a NUL byte followed by two '0' characters if you run stripslashes() on it. This behavior has been verified on MySQL 3.23.56 on Red Hat 9 and 4.0.16-nt on Windows XP Pro, and can probably be found on every reasonably recent version of MySQL. When retrieving a binary file from the database, however, it is only necessary to run one function to get rid of this particular sequence: $data=str_replace("\\000","\0",$row['file_data']); The information in $data should then be absolutely identical to the initial input. With regard to why nobody has already posted this information on one of the major code exchange sites, your guess is as good as mine. To make testing and verifying these results easier, you may want to download the ful_test.zip file here and examine the code, which is a basic working file upload system that receives uploaded files and verifies both that they can be inserted in the database and that identical data can be retrieved from the database. (Each file is deleted after retrieval, so you won't pile up a bunch of unwanted records -- but you will have to DROP TABLE `ful_test` manually when you're done playing with it.) This code is released under the BSD open source license, and you are more than welcome to use it in your own projects.
Copyright 2004 Darklock Communications |