Web hosting provider - USING PHP TO MANAGE FILES The first two

USING PHP TO MANAGE FILES The first two lines assign the target folder to a variable and call getNextFilename5() (use getNextFilename4() for PHP 4) to generate the next filename in the series. The function runs a number of checks on the three arguments and returns false if any fail. So the next section of code is wrapped in a conditional statement to ensure that the script attempts to create a new file only if a valid filename is obtained. The call to fopen() is enclosed in another conditional statement. This checks that the file has been successfully created before attempting to write to it. If the file can t be opened, a suitable message is assigned to $result. The final else clause belongs to the following conditional statement: if ($filename) { So, if getNextFilename() returns false, $result reports the likely reasons for failure. 5. Insert the following PHP code between the opening and

tags to display the outcome of the operation after the form has been submitted. $result

“; ?> } 7
6. Save create_series.php and load it into a browser. Test the page, and you should see the following message, indicating that the first file in the series has been created: 7. Submit the form again. This time the message should read comment002.txt created. Experiment with invalid filename prefixes, such as including a forward slash in the name. Also try selecting a directory that doesn t exist or for which you don t have the necessary permissions. How the getNextFilename() function works The function builds a Perl-compatible regular expression (PCRE) in line 11, using the values in the second and third arguments, to find the correct series of files and extract the numerical part of matching filenames. The numbers are stored in an array, and the max() function is used to find the highest number, to which 1 is added. If the array is empty, no files have yet been created, so it assigns the number 1. The final part of the function calculates how many leading zeros to add to the 201
We recommend high quality webhost to host and run your jsp application: christian web host services.

Leave a Reply