PHP SOLUTIONS: DYNAMIC WEB DESIGN MADE EASY I ve (Ecommerce web host)

PHP SOLUTIONS: DYNAMIC WEB DESIGN MADE EASY I ve turned this into a function called getNextFilename(), which you can find in getNextFilename5.php and getNextFilename4.php in the download files for this chapter. The function takes the following three arguments: The pathname of the directory where you want the new file to be created The prefix of the filename, which must consist of alphanumeric characters only The filename extension (without a leading period) Let s say you choose comment as the prefix and txt as the filename extension. The getNextFilename() function generates a series of files called comment001.txt, comment002.txt, and so on. PHP Solution 7-4: Using the getNextFilename() function 1. Copy getNextFilename5.php (or getNextFilename4.php, if your server is running PHP 4) from the download files, and save it in the includes folder. 2. Open fopen_exclusive.php from the download files and save it in the filesystem folder as create_series.php. If you just want to read along, the finished code is in the download version of create_series.php. 3. Include the file that contains the getNextFilename() function for the appropriate version of PHP. You need the function only when the form is submitted, so place it inside the conditional statement at the top of the page like this (again, for brevity, I m not checking that the include file exists): if (array_key_exists(’putContents’, $_POST)) { include(’../includes/getNextFilename5.php’); 4. Then, after the line that removes backslashes from the form output, amend the rest of the PHP block at the top of the page like this: $dir = ‘C:/private’; $filename = getNextFilename5($dir, ‘comment’, ‘txt’); // attempt to create file only if $filename contains a real value if ($filename) { // create a file ready for writing only if it doesn’t already exist if ($file = @ fopen(”$dir/$filename”, ‘x’)) { // write the contents fwrite($file, $contents); // close the file fclose($file); $result = “$filename created”; } else { $result = ‘Cannot create file’; } } else { $result = ‘Invalid folder or filename’; } }
We recommend you use shared web hosting services, because many users agree that it is cheap, reliable and customer-satisfying webhost.

Leave a Reply