To load up a variable with the contents of a file with PHP, you can use this command:
$filecontents=file_get_contents("path to file"); |
One problem is that we had for this in our application was quotes. The text in the file had both ‘ and “. Magic quotes automatically convert quotes to escaped quotes so that when the text is loaded into a database, it gets loaded correctly. There are two different types of magic quotes: magic_quotes_gpc, and magic_quotes_runtime. On our system, at least, magic_quotes_gpc was on, but to use the file_get_contents function to use magic quotes, we needed to set magic_quotes_runtime = On. This is set in /etc/php.ini (or wherever your php.ini is):
; Magic quotes for incoming GET/POST/Cookie data. magic_quotes_gpc = On ; Magic quotes for runtime-generated data, e.g. data from SQL, from exec(), etc. magic_quotes_runtime = On |
Note that this is a somewhat contentious issue, along with stripslashes and addslashes. To read the data back out of the database with the above, we do need to use stripslashes. The general concern is that PHP is changing what the user inputs. Another concern is that how your code runs on various servers that may or not have magic quotes on. The particular app we are working on, though, is intended to be a local app, run by known users, running on a server we control, so we aren’t that concerned. In fact, it is entirely like that with this app, the entire system will be run by the user out of their own xampp install so that it can be somewhat independent. Indeed, with this app, the user has direct access to edit documents directly on the file system. Do beware, though, that there are issues with using magic quotes. For more details, see this article. In most cases, I’d imagine, what you would want to do is use something like the preg_replace function, a test for magic quotes, and specific character tests in hex or something like that. The PCRE man page would be useful in this case.