Archive for February, 2008

Web design conference - PHP SOLUTIONS: DYNAMIC WEB DESIGN MADE EASY Making

Friday, February 8th, 2008

PHP SOLUTIONS: DYNAMIC WEB DESIGN MADE EASY Making a smaller copy of an image The aim of this chapter is to show you how to resize images automatically on upload. This involves adapting the file upload form from Chapter 6. However, to make it easier to understand how to work with PHP s image manipulation functions, I propose to start by using images already on the server, and merge the resizing script with the upload code only at the final stage. Getting ready The starting point is the following simple form, which uses the buildFileList() function from the last chapter to create a drop-down menu of the photos in the images folder. You can find the code in create_thumb01.php in the download files for this chapter. Copy it to a new folder called gd in the phpsolutions site root, and rename it create_thumb.php.


We would like to recommend you tested and proved virtual web hosting services, which you will surely find to be of great quality.

GENERATING THUMBNAIL IMAGES If you can t find this (Web hosting solutions)

Thursday, February 7th, 2008

GENERATING THUMBNAIL IMAGES If you can t find this section, it means that the GD extension isn t enabled, so you won t be able to use any of the scripts in this chapter. Your next move depends on your situation. On a hosting company s shared server, there s nothing you can do about it, apart from complain or move to a different host. If you re checking your local testing environment on a Windows computer, open php.ini and locate the following line in the list of Windows extensions: ;extension=php_gd2.dll Remove the semicolon at the start of the line, save php.ini, and restart Apache or IIS. If you still can t see that GD support has been enabled, refer back to Chapter 2. Make sure that the correct version of php.ini is being read, extension_dir is pointing to the correct location, and your Windows path setting includes your PHP folder. On a Mac, GD is enabled by default in the package created by Marc Liyanage that I recommended in Chapter 2. Assuming that GD support is enabled on your server, check the version and the settings for GIF Read Support, GIF Create Support, JPG Support, and PNG Support. GD Version needs to be a minimum of 2. All versions should support JPEG and PNG files, but you need 2.0.28 or later for full GIF support. If the version number is lower than 2.0.28, you will probably be able to read GIF files, but not create them. The scripts in this chapter have been designed to respond appropriately to different levels of support. Strictly for abbreviation/acronym freaks: GIF stands for Graphics Interchange Format, JPEG is the standard created by the Joint Photographic Experts Group, and PNG is short for Portable Network Graphics. Although JPEG is the correct name for the standard, the E is frequently dropped, particularly when used as a filename extension. 8 Manipulating images dynamically The GD extension allows you to generate images entirely from scratch or work with existing images. Either way, the underlying process always follows four basic steps: 1. Create a resource for the image in the server s memory while it s being processed. 2. Process the image. 3. Display and/or save the image. 4. Remove the image resource from the server s memory. This process means that you are always working on an image in memory only and not on the original. Unless you save the image to disk before the script terminates, any changes are discarded. Working with images requires a lot of memory, so it s vital to destroy the image resource as soon as it s no longer needed. If a script runs very slowly or crashes, it probably indicates that the original image is too large. 213
From our experience, we are can tell you that you can find a reliable and cheap webhost service at Java Web Hosting services.

PHP SOLUTIONS: DYNAMIC WEB DESIGN MADE EASY (Vps web hosting) What

Wednesday, February 6th, 2008

PHP SOLUTIONS: DYNAMIC WEB DESIGN MADE EASY What this chapter covers: Scaling an image Saving a rescaled image Automatically resizing and renaming uploaded images PHP has an extensive range of functions designed to work with images. You ve already met one of them, getimagesize(), in Chapter 4. As well as providing useful information about an image s dimensions, PHP can manipulate images by resizing or rotating them. It can also add text dynamically without affecting the original; it can even create images on the fly. To give you just a taste of PHP image manipulation, I m going to show you how to generate a smaller copy of an uploaded image. Most of the time, you ll want to use a dedicated graphics program, such as Photoshop or Fireworks, to generate thumbnail images because it gives you much better quality control. However, automatic thumbnail generation with PHP can be very useful if you want to allow registered users to upload images, but make sure that they conform to a maximum size. You can save just the resized copy, or the copy along with the original. Checking your server s capabilities Working with images in PHP relies on the GD extension. Originally GD stood for GIF Draw, but support for GIF files was dropped in favor of JPEG and PNG because of a dispute over a patent. However, the name GD stuck, even though it no longer stands for anything. The problematic patent has now expired and GIF is once again supported, but you need to make sure GD has been enabled on your server and check which features are available. As in previous chapters, load a page containing to check the server s configuration. Scroll down until you reach the section shown in the following screenshot (it should be about halfway down the page).
In case you need quality webspace to host and run your web applications, try our personal web hosting services.

Free web servers - 8 GENERATING THUMBNAIL IMAGES

Wednesday, February 6th, 2008

8 GENERATING THUMBNAIL IMAGES
From our experience, we are can tell you that you can find a reliable and cheap webhost service at Java Web Hosting services.

USING PHP TO MANAGE FILES Click OK, and (Hosting your own web site)

Tuesday, February 5th, 2008

USING PHP TO MANAGE FILES Click OK, and the file should be saved rather than displayed. Click Cancel to abandon the download. Whichever button you click, the original page remains in the browser window. The only time download.php should load into the browser is if the file cannot be opened. That s why it s important to create a back link through $nogo or send the user to a different page. I ve demonstrated download.php with image files, but it can be used for any type of file because the headers send the file as a binary stream. This script relies on header() to send the appropriate HTTP headers to the browser. It is vital to ensure that there are no new lines or whitespace ahead of the opening PHP tag. If you have removed all whitespace and still get an error message saying headers already sent, your editor may have inserted invisible control characters at the begin- ning of the file. Try opening your script in a different editor, delete the opening PHP tag, and press the Backspace key several times before retyping the opening tag. Summary The file system functions aren t particularly difficult to use, but there are many subtleties that can turn a seemingly simple task into a complicated one. It s important to check that you have the right permissions. Even when handling files in your own website, PHP needs permission to access any directory where you want to read files or write to them. When dealing with remote data sources, you also need to check that allow_url_fopen hasn t been disabled. Hopefully, this problem will disappear when PHP 6 becomes standard. However, hosting companies are notoriously slow at implementing major upgrades of PHP, so it s useful to know how to work around this issue with fsockopen(). In the next two chapters, we ll put some of the PHP Solutions from this chapter to further practical use when working with images and building a simple user authentication system. 209
In case you need quality webspace to host and run your web applications, try our personal web hosting services.

Managed web hosting - PHP SOLUTIONS: DYNAMIC WEB DESIGN MADE EASY page

Monday, February 4th, 2008

PHP SOLUTIONS: DYNAMIC WEB DESIGN MADE EASY page (see Redirecting to another page in Chapter 5). The second line that needs to be changed defines the pathname to the folder where the download file is stored. The script works by taking the name of the file to be downloaded from a query string appended to the URL and saving it as $getfile. Because query strings can be easily tampered with, the opening conditional statement uses basename() to make sure that an attacker cannot request a file, such as one that stores passwords, from another part of your file structure. As explained in Chapter 4, basename() extracts the filename component of a path, so if basename($_GET[’file’]) is different from $_GET[’file’], you know there s an attempt to probe your server, and you can stop the script from going any further by setting $getfile to NULL. After checking that the requested file exists and is readable, the script gets the file s size, sends the appropriate HTTP headers, and opens the file in binary read-only mode by adding b after the r mode argument. Finally, fpassthru() dumps the file to the output buffer. 3. Test the script by creating another page and add a couple of links to download.php. Add a query string at the end of each link with file= followed by the name a file to be downloaded. You ll find a page called prompt.php in the download files, which contains the following two links:

Download image 1

Download image 2

4. Click one of the links, and the browser should present you with a dialog box prompting you to download the file, as shown in Figure 7-3. Figure 7-3. The browser prompts the user to download the image, rather than opening it directly.
If you are looking for affordable and reliable webhost to host and run your business application visit our ftp web hosting services.

USING PHP TO MANAGE FILES (Web file server) The only two

Sunday, February 3rd, 2008

USING PHP TO MANAGE FILES Back.’; if (!$getfile) { // go no further if filename not set echo $nogo; } else { // define the pathname to the file $filepath = ‘C:/htdocs/phpsolutions/images/’.$getfile; // check that it exists and is readable if (file_exists($filepath) && is_readable($filepath)) { // get the file’s size and send the appropriate headers $size = filesize($filepath); header(’Content-Type: application/octet-stream’); header(’Content-Length: ‘.$size); header(’Content-Disposition: attachment; filename=’.$getfile); header(’Content-Transfer-Encoding: binary’); // open the file in binary read-only mode // suppress error messages if the file can’t be opened $file = @ fopen($filepath, ‘rb’); if ($file) { // stream the file and exit the script when complete fpassthru($file); exit; } else { echo $nogo; } } else { echo $nogo; } } ?> The only two lines that you need to change in this script are highlighted in bold type. The first defines $nogo, a variable that is called whenever something prevents the file from being downloaded. In this script, I have simply created a link to a page called prompt.php, which you will create in the next step. You could, however, use the header() function in combination with Location to divert the user to another 207
In case you need quality webspace to host and run your web applications, try our personal web hosting services.

PHP SOLUTIONS: DYNAMIC WEB DESIGN MADE EASY This (Web design)

Sunday, February 3rd, 2008

PHP SOLUTIONS: DYNAMIC WEB DESIGN MADE EASY This uses the strpos() function to find the position of the beginning and end of the XML feed. At the time of this writing, the friends of ED news feed is enclosed in tags. Other feeds may use different tags, so you need to adjust the value of $endTag accordingly to find the end of the feed. The strpos() function returns the position of the first matching character in the substring that you re searching for, so to find the end position, you need to add the number of characters in the end tag. The strlen() function is designed to do precisely that, so adding strlen($endTag) to the position of the first character of $endTag gives you the end of the feed. Finally, the substr() function extracts the news feed. It takes three arguments: the original string, the position from which you want to start the extraction (the opening XML tag), and the number of characters (calculated in this case by subtracting $start from $end). 8. Save the page and reload it. Switch to your browser s source code view, and you should see the clean XML feed. You need to use fsockopen() only if your hosting company has disabled allow_url_fopen. Once you have captured the remote data, you treat it as any other string. The easiest way to handle an XML news feed is with SimpleXML, which is available in PHP 5 and later. To learn more about SimpleXML, visit www.php.net/manual/en/ ref.simplexml.php or see Beginning PHP and MySQL 5: From Novice to Professional by W. Jason Gilmore (Apress, ISBN: 1-59059-552-1). If using SimpleXML with fsockopen(), you need to use simplexml_load_string() instead of simplexml_load_file(). Creating a download link A question that crops up regularly in online forums is, How do I create a link to an image (or PDF file) that prompts the user to download it? The quick solution is to convert the file into a compressed format, such as ZIP. This frequently results in a smaller download, but the downside is that inexperienced users may not know how to unzip the file, or they may be using an older operating system that doesn t include an extraction facility. With PHP file system functions, it s easy to create a link that automatically prompts the user to download a file in its original format. The script sends the necessary HTTP headers, opens the file, and outputs its contents as a binary stream. PHP Solution 7-6: Prompting a user to download an image 1. Create a PHP file called download.php in the filesystem folder. The full listing is given in the next step. You can also find it in download.php in the files for this chapter. 2. Remove any default code created by your script editor, and insert the following code:
If you are searching for cheap webhost for your web application, please visit MySQL5 Web Hosting services.

USING PHP TO MANAGE FILES 6. Save fsockopen.php

Saturday, February 2nd, 2008

USING PHP TO MANAGE FILES 6. Save fsockopen.php and load it into a browser. As long as you re connected to the Internet, you ll probably see the friends of ED news feed displayed as continuous plain text. To get a better understanding of what you have received, open the browser s source code view. You should see something similar to Figure 7-2. Figure 7-2. Remote data sources accessed with fsockopen() include the HTTP headers. The disadvantage of using fsockopen() to access a remote data source is that you get all the HTTP headers, in addition to the news feed. If you scroll to the bottom of the source code view, you may also see some unwanted characters after the closing XML tag, as shown in the following screenshot: 7. To get rid of the headers and any extraneous characters at the end of the feed, replace the line of code in step 5 with the following: // find beginning and end of news feed $start = strpos($received, ‘‘; $end = strpos($received, $endTag) + strlen($endTag); // extract news feed and display $clean = substr($received, $start, $end-$start); echo $clean; 205
We would like to recommend you tested and proved virtual web hosting services, which you will surely find to be of great quality.

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

Friday, February 1st, 2008

PHP SOLUTIONS: DYNAMIC WEB DESIGN MADE EASY 3. First of all, you need to prepare a request and send it to the remote server. Add the following code after the comment in the else clause: // otherwise communicate with remote server // prepare the request $out = “GET /news.php HTTP/1.1rn”; $out .= “Host: www.friendsofed.comrn”; $out .= “Connection: Closernrn”; // send the request fwrite($remote, $out); The request is stored in $out and consists of the following three elements: The page you want, presented in this format: GET /path_to_page HTTP/1.1 The URL that we plan to open is www.friendsofed.com/news.php, which becomes just /news.php. Note that it begins with a forward slash. If you want the default page of a site, use a forward slash on its own. Host, followed by a colon and the domain name. An instruction to close the connection after the response has been sent. Each part of the request must be followed by a carriage return and new line character (rn), and the final line by an extra carriage return and new line character (rnrn). Since these characters are PHP escape sequences, you need to use double quotes (see Using escape sequences with double quotes in Chapter 3). Once you have built the request, send it by passing $out to fwrite() with a reference to the remote connection that you have opened. 4. After sending the request, you need to capture the response in a variable, and then close the socket connection. Add the following code to the else clause immediately below the code in the previous step: // initialize a variable to capture the response $received = ‘’; // keep the connection open until the end of the response while (!feof($remote)) { $received .= fgets($remote, 1024); } // close the connection fclose($remote); This uses feof(), fgets(), and fclose() in the same way as with local files. The only difference is that I have added a second argument to fgets(). This tells the function how many bytes to retrieve at a time. The fgets() function gets one line at a time, but some XML files don t use new lines, so it s more resource-efficient to specify a length. 5. Finally, use echo to display the response from the remote server. Add the following line after the closing curly brace of the conditional statement: echo $received;
We highly recommend you visit web and email hosting services if you need stable and cheap web hosting platform for your web applications.