Archive for January, 2008

Web hosting reviews - USING PHP TO MANAGE FILES pass the URL

Thursday, January 31st, 2008

USING PHP TO MANAGE FILES pass the URL as an argument to the function. Unfortunately, as noted earlier, many hosting companies disable the allow_url_fopen setting in PHP. One way to get around this is to use a socket connection instead. To create a socket connection, use the fsockopen() function, which takes the following five arguments: The target domain name The port you want to open for web pages, this is always 80 A variable to capture an error number if the connection fails A variable to capture any error message The number of seconds to attempt the connection before timing out Only the first argument is required, but using all five is a good idea since you can always use the same values, and the error message may help you understand what s gone wrong if the connection fails. The fsockopen() function works in a very similar way to fopen() by opening a file for you to read. Let s use fsockopen() to access the friends of ED news feed at www.friendsofed.com/ news.php. PHP Solution 7-5: Opening a news feed with fsockopen() 1. Create a PHP file called fsockopen.php in the filesystem folder. If you just want to study the final code, use fsockopen.php in the download files for this chapter. 2. If your script editor automatically inserts a DOCTYPE declaration and XHTML skeleton, remove them. You need to start with a blank page. Insert the following code: This is the basic skeleton for any socket connection using fsockopen(). The only change you normally need to make is to the first argument, which is the domain name of the site that you want to access. If a connection can t be made, the first half of the conditional statement displays the error message and number. If the error number is 0, it may indicate the socket connections have been disabled on your server. In that event, consult your hosting company. If a successful connection is made, the else clause is executed. At the moment, it contains just a comment. So let s fix that now. 203
From our experience, we can recommend PHP5 Web Hosting services, if you need affordable webhost to host and run your web application.

PHP SOLUTIONS: DYNAMIC WEB DESIGN MADE EASY number (Database web hosting)

Thursday, January 31st, 2008

PHP SOLUTIONS: DYNAMIC WEB DESIGN MADE EASY number and then builds the filename from its composite parts. A series can contain up to 999 files and still remain in the correct order. The full listing follows: function getNextFilename($dir, $prefix, $type) { // run some security checks on the arguments supplied if (!is_dir($dir)) return false; if (!preg_match(’/^[-._a-z0-9]+$/i’, $prefix)) return false; $permittedTypes = array(’txt’,'doc’,'pdf’,'jpg’,'jpeg’,'gif’,'png’); if (!in_array(strtolower($type), $permittedTypes)) return false; // if the checks are OK, get an array of the directory contents $existing = scandir($dir); // create a search pattern for files that match the prefix and type $pattern = ‘/^’.$prefix.’(d+).’.$type.’$/i’; $nums = array(); // loop through the directory // get the numbers from all files that match the pattern foreach ($existing as $file) { if (preg_match($pattern, $file, $m)) { $nums[] = intval($m[1]); } } // find the highest number and increase it by 1 // if no file yet created, assign it number 1 $next = $nums ? max($nums)+1 : 1; // calculate how many zeros to prefix the number with if ($next < 10) { $zeros = '00'; } elseif ($next < 100) { $zeros = '0'; } else { $zeros = '' ; } // return the next filename in the series return "{$prefix}{$zeros}{$next}.{$type}"; } As with the buildFileList() function, I have created an array of acceptable file types (highlighted in bold on line 5 in the preceding code). Although create_series.php is used to create a text file, you can incorporate this function in a file upload script. If you need to change the range of acceptable file types, amend the $permittedTypes array. The function should not need any other alteration. The PHP 4 version is identical except for the way it opens and inspects the directory. Opening remote data sources PHP can open publicly available files on other servers just as easily as on the same server. This is particularly useful for accessing XML files or news feeds. All that you need to do is
If you are looking for cheap and quality webhost to host and run your website check Jboss Web Hosting services.

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

Wednesday, January 30th, 2008

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.

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

Tuesday, January 29th, 2008

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.

USING PHP TO (Com web hosting) MANAGE FILES $fileTypes = array(’jpg’,'jpeg’,'gif’,'png’);

Tuesday, January 29th, 2008

USING PHP TO MANAGE FILES $fileTypes = array(’jpg’,'jpeg’,'gif’,'png’); // traverse folder, and add file to $found array if type matches $found = array(); foreach ($contents as $item) { $fileInfo = pathinfo($item); if (array_key_exists(’extension’, $fileInfo) && . in_array($fileInfo[’extension’],$fileTypes)) { $found[] = $item; } } // Check the $found array is not empty if ($found) { // sort in natural, case-insensitive order, and populate menu natcasesort($found); foreach ($found as $filename) { echo “n”; } } } } How the buildFileList() function works I suspect many readers will be happy just to use the function, but if you re curious as to how it works, here s a brief description to flesh out the inline comments. After the folder has been opened, each item is passed to a PHP function called pathinfo(), which returns an associative array with the following elements: dirname: The name of the directory (folder) basename: The filename, including extension (or just the name if it s a directory) extension: The filename extension (not returned for a directory) Because the extension element is not returned for a directory, you need to use array_key_exists() before attempting to check its value. The second half of the condi tional statement in line 12 uses in_array() to see if the value of extension matches one of the file types that you re looking for. It there s a match, the filename is added to the $found array. It s then just a case of building the

PHP SOLUTIONS: DYNAMIC WEB (Web hosting companies) DESIGN MADE EASY 3.

Monday, January 28th, 2008

PHP SOLUTIONS: DYNAMIC WEB DESIGN MADE EASY 3. Create a form inside imagelist.php, and insert a

This

Make sure that the pathnames to the include file and the images folder are correct for your site s folder structure. If you re using PHP 4, the two lines in the PHP code block need to refer to the PHP 4 version of the function like this: include(’../includes/buildFileList4.php’); buildImageList4(’../images’); For brevity, I m not using the techniques in Chapter 4 for checking that the include file exists. 5. Save imagelist.php and load it into a browser. You should see a drop-down menu listing all the images in your images folder, as shown in Figure 7-1. When incorporated into an online form, the filename of the selected image appears in the $_POST array identified by the name attribute of the

  • You are currently browsing the Jsp, Tomcat, J2Ee, Hibernate Programming, Http Web Server Blog weblog archives for January, 2008.

  • Archives

  • Categories