Wednesday, July 27, 2011
South Indian Sexy Celebrity
Friday, January 22, 2010
name of actress in bollywood
Thursday, January 21, 2010
Monday, January 4, 2010
File Upload Forms and Scripts
So far we've looked at simple form input. Browsers Netscape 2 or better and Internet Explorer 4 or better all support file uploads, and so, of course, does PHP. In this section, you will examine the features that PHP makes available to deal with this kind of input.
First, we need to create the HTML. HTML forms that include file upload fields must include an ENCTYPE argument:
ENCTYPE="multipart/form-data"
PHP also works with an optional hidden field that you can insert before the file upload field. It should be called MAX_FILE_SIZE and should have a value representing the maximum size in bytes of the file that you are willing to accept. This size cannot override the maximum size set in the upload_max_filesize field in your php.ini file that defaults to 2MB. The MAX_FILE_SIZE field is obeyed at the browser's discretion, so you should rely upon the php.ini setting to cap unreasonable uploads. After the MAX_FILE_SIZE field is entered, you are ready to add the upload field itself. It is simply an input element with a type argument of "file". You can give it any name you want. Listing 10.11 brings all this work together into an HTML upload form.
$_FILE Elements Element
Contains
Example
$ FILES['fupload']['name']
Name of uploaded file
test.gif
$_FILES['fupload']['tmp_name']
Path to temporary file
/tmp/phprDfZvN
$_FILES['fupload']['size']
Size (in bytes) of uploaded file
6835
$_FILES['fupload']['error']
An error code corresponding to a PHP constant
UPLOAD_ERR_FORM_SIZE
$_FILES['fupload']['type']
MIME type of uploaded file (where given by client)
image/gif
You can use the error element of an element in $_FILES to diagnose the reason for a failed upload. Assuming a file upload named 'fupload', we would find the error code in
$_FILES['fupload']['error]
lists the possible error codes.
$_FILE Error Constants Constant Name
UPLOAD_ERR_OK
0
No problem
UPLOAD_ERR_INI_SIZE
1
File size exceeds php.ini limit set in upload_max_filesize
UPLOAD_ERR_FORM_SIZE
2
File size exceeds limit set in hidden element named MAX_FILE_SIZE
UPLOAD_ERR_PARTIAL
3
File only partially uploaded
UPLOAD_ERR_NO_FILE
4
File was not uploaded
Armed with this information, we can write a quick and dirty script that displays information about uploaded files (see Listing 10.12). If the uploaded file is in GIF format, the script will even attempt to display it.
Redirecting the User in php aand mysql
Redirecting the UserOur simple script still has one major drawback. The form is rewritten whether or not the user guesses correctly. The fact that the HTML is hard-coded makes it difficult to avoid writing the entire page. We can, however, redirect the user to a congratulations page, thereby sidestepping the issue altogether. When a server script communicates with a client, it must first send some headers that provide information about the document to follow. PHP usually handles this task for you automatically, but you can choose to send your own header lines with PHP's header() function. To call the header() function, you must be sure that no output has been sent to the browser. The first time that content is sent to the browser, PHP sends out headers and it is too late for you to send your own. Any output from your document, even a line break or a space outside of your script tags, causes headers to be sent. If you intend to use the header() function in a script, you must make certain that nothing precedes the PHP code that contains the function call. You should also check any libraries that you might be using. Listing 10.9 shows a request (lines 1 and 2) followed by typical response headers sent to the browser by PHP. Listing 10.9 A Request Prompts Response Headers from a PHP Script1: HEAD /phpbook/source/listing10.8.php HTTP/1.0 2: Host:matt.corrosive.co.uk 3: 4: HTTP/1.1 200 OK 5: Date: Wed, 03 Sep 2003 13:52:09 GMT 6: Server: Apache/2.0.47 (Unix) PHP/5.0.0b1 7: X-Powered-By: PHP/5.0.0b1 8: Connection: close 9: Content-Type: text/html; charset=ISO-8859-1 You Can Browse the Web with Telnet
By sending a "Location" header instead of PHP's default, you can cause the browser to be redirected to a new page: header( "Location: http://www.corrosive.co.uk" ); Assuming that we have created a suitably upbeat page called congrats.html, we can amend our number-guessing script to redirect the user if she guesses correctly, as shown in Listing 10.10. Listing 10.10 Using header() to Send Raw Headers1: <?php 2: $num_to_guess = 42; 3: $message = ""; 4: if ( ! isset( $_POST['guess'] ) ) { 5: $message = "Welcome to the guessing machine!"; 6: } else if ( $_POST['guess'] > $num_to_guess ) { 7: $message = $_POST['guess']." is too big! Try a smaller number"; 8: } else if ( $_POST['guess'] < $num_to_guess ) { 9: $message = $_POST['guess']." is too small! Try a larger number"; 10: } else { // must be equivalent 11: header("Location:congrats.html"); 12: exit; 13: } 14: $guess = (int) $_POST['guess']; 15: $num_tries = (int) $_POST['num_tries']; 16: $num_tries++; 17: ?> 18: <!DOCTYPE html PUBLIC 19: "-//W3C//DTD XHTML 1.0 Strict//EN" 20: "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> 21: <html> 22: <head> 23: <title>Listing 10.10 A PHP Number Guessing Script</title> 24: </head> 25: <body> 26: <div> 27: <h1> 28: <?php print $message ?> 29: </h1> 30: Guess number: <?php print $num_tries?><br/> 31: 32: <form method="post" action="<?php print $_SERVER['PHP_SELF']?>"> 33: <p> 34: <input type="hidden" name="num_tries" value="<?php print $num_tries?>" /> 35: Type your guess here: <input type="text" name="guess" 36: value="<?php print $guess?>"/> 37: </p> 38: </form> 39: </div> 40: </body> 41: </html> The else clause of our if statement on line 10 now causes the browser to request congrats.html. We ensure that all output from the current page is aborted with the exit statement on line 12, which immediately ends execution and output, whether HTML or PHP. Remember that sending content to the browser causes HTTP headers to be sent. If you then call the header() function, you cause an error. You code defensively by checking that headers have not been sent before calling header(): if ( ! headers_sent() ) { header( "Location: http://www.example.com" ); exit; } If headers have been sent, the headers_sent() function returns true. headers_sent() also optionally accepts two empty variables, into which it places the filename and line number, defining the point at which headers were sent. This function can be very useful for debugging: if ( headers_sent( $file, $num ) ) { print "headers were sent in file: $file on line: $line"; } |
Using Hidden Fields to Save State
Using Hidden Fields to Save State
The script in Listing 10.7 has no way of knowing how many guesses a user has made. We can use a hidden field to keep track of this. The mark-up for a hidden field is similar to that of a text field. From the user's perspective, however, it has no output. A user cannot see a hidden field, unless he views the HTML source of the document that contains it. Listing 10.8 adds a hidden field to the number-guessing script and some PHP to work with it.
Listing 10.8 Saving State with a Hidden Field
1: <?php 2: $num_to_guess = 42; 3: $message = ""; 4: if ( ! isset( $_POST['guess'] ) ) { 5: $message = "Welcome to the guessing machine!"; 6: } else if ( $_POST['guess'] > $num_to_guess ) { 7: $message = $_POST['guess']." is too big! Try a smaller number"; 8: } else if ( $_POST['guess'] < $num_to_guess ) { 9: $message = $_POST['guess']." is too small! Try a larger number"; 10: } else { // must be equivalent 11: $message = "Well done!"; 12: } 13: $guess = (int) $_POST['guess']; 14: $num_tries = (int) $_POST['num_tries']; 15: $num_tries++; 16: ?> 17: <!DOCTYPE html PUBLIC 18: "-//W3C//DTD XHTML 1.0 Strict//EN" 19: "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> 20: <html> 21: <head> 22: <title>Listing 10.8 A PHP Number Guessing Script</title> 23: </head> 24: <body> 25: <div> 26: <h1> 27: <?php print $message ?> 28: </h1> 29: Guess number: <?php print $num_tries?><br/> 30: 31: <form method="post" action="<?php print $_SERVER['PHP_SELF']?>"> 32: <p> 33: <input type="hidden" name="num_tries" value="<?php print $num_tries?>" /> 34: Type your guess here: <input type="text" name="guess" 35: value="<?php print $guess?>"/> 36: </p> 37: </form> 38: </div> 39: </body> 40: </html>
The hidden field on line 33 is given the name "num_tries". We also use PHP to write its value. While we're at it, we do the same for the "guess" field on line 27 so that the user can always see his last guess. This technique is useful for scripts that parse user input. If we were to reject a form submission for some reason, we can at least allow our user to edit his previous query.
You Can Automate print() with Short Opening Tags
When you need to output the value of an expression to the browser, you can of course use print() or echo(). When you are entering PHP mode explicitly to output such a value, you can also take advantage of a special extension to PHP's short opening tags. If you add an equals (=) sign to the short PHP opening tag, the value contained will be printed to the browser. Note the following line: <? print $test;?> <?=$test?> Remember, though, that the short open tag might be disabled on some sites and interfere with XML. |
The variables $guess and $num_tries were extracted from the $_POST array on lines 13 and 14. We cast the values to integers and add one to $num_tries. The $num_tries variable is written to the value of the hidden field named 'num_tries' on line 33. Every time the user submits the form, the $_POST['num_tries'] element will have been incremented.










