Web server type - USING PHP TO MANAGE FILES When testing on
Friday, January 18th, 2008USING PHP TO MANAGE FILES When testing on your local system, you shouldn t see any error messages, unless you typed the code incorrectly or you did not set the correct permissions on a Mac. However, on a remote system, you may see error messages similar to this: The error messages in the preceding screenshot were created on a local system to demonstrate what happens when open_basedir has been set either in php.ini or on the server. It means that you are trying to access a file outside your permitted file structure. The first error message should indicate the allowed paths. On a Windows server, each path is separated by a semicolon. On Linux, the separator is a colon. Another possible cause of errors on a remote server is the use of spaces or illegal characters in the names of files or folders. Never use spaces in filenames on the Web. 4. At this stage, using file_get_contents() looks no different from using an include command. However, file_get_contents() treats the external file as a string, which means that you can store the contents in a variable and manipulate it in a way that s impossible with an include file. Change file_get_contents.php like this (or use file_get_contents02.php) and load the page into a browser: $contents = file_get_contents(’C:/private/filetest01.txt’); // convert contents to uppercase and display echo strtoupper($contents); The result should look like the screenshot to the right: Admittedly, this is a trivial example, but it means that you can use any of PHP s string functions to format the contents of an external file or to search for specific information with regular expressions and pat tern matching functions. 5. A danger with an external file is that you may not be able to open it: the file might be missing, its name misspelled, or the network connection down. Change the code like this (it s in file_get_contents03.php): $contents = file_get_contents(’C:/private/filetest01.txt’); if ($contents === false) { echo ‘Sorry, there was a problem reading the file.’; } else { // convert contents to uppercase and display echo strtoupper($contents); } 183
We recommend you use shared web hosting services, because many users agree that it is cheap, reliable and customer-satisfying webhost.