Showing posts with label php. Show all posts
Showing posts with label php. Show all posts

October 19, 2013

File Uploads in PHP

File Uploads in PHP

A file upload is a special case of getting form input. Half of the story is putting together
the correct HTML. File uploads are specified in RFC 1867. They are supported by
Netscape Navigator 2 and above, as well as Internet Explorer 4 and above. Placing an
input tag inside an HTML form with the type attribute set to file causes a text box and a
button for browsing the local file system to appear on the Web page. Browsers that do not
support uploads will likely render this as a text box, so it's best to present uploading
forms only to capable browsers. The forms must use the post method to allow for
uploads, and they must also contain the enctype attribute with a value of
multipart/form-data. A hidden form variable, MAX_FILE_SIZE, must precede the file
input tag. Its value is the maximum file size in bytes to be accepted.

When the form is submitted, PHP will detect the file upload. The file will be placed in a
temporary directory on the server, such as /var/tmp. Several variables will be created
based on the name of the file field. A variable with the same name as the file field will
contain the complete path to the file in the local file system. A variable with _name
appended to the file field name will contain the original file name as provided by the
browser. A variable with _size appended to the file field name will contain the size of
the file in bytes. Finally, a variable with _type appended to the file field name will
contain the MIME type of the file, if it was offered by the browser.

Listing 7.4 File Upload
<?
//check for file upload
if(isset($UploadedFile))
{
unlink($UploadedFile);
print("Local File: $UploadedFile <BR>\n");
print("Name: $UploadedFile_name <BR>\n");
print("Size: $UploadedFile_size <BR>\n");
print("Type: $UploadedFile_type <BR>\n");
print("<HR>\n");
}
?>
<FORM ENCTYPE="multipart/form-data"
ACTION="<? $PHP_SELF ?>" METHOD="post">
<INPUT TYPE="hidden" name="MAX_FILE_SIZE" value="4096">
<INPUT NAME="UploadedFile" TYPE="file">
<INPUT TYPE="submit" VALUE="Upload">

If you plan on using the file later, move the new file into a permanent spot. If you do not,
PHP will delete the file when it finishes executing the current page request. Listing 7.4 is
an example script that accepts uploads and immediately deletes them.

File uploads are limited in size by a directive in php.ini, upload_ max_filesize. It
defaults to two megabytes. If a file exceeds this limit, your script will execute as if no file
were uploaded. A warning will be generated, as well.

Like other form fields, the upload form field is treated like setting the value of a variable.
If you place square brackets at the end of the field name, an array will be created. As you
would expect, the size and type values will be placed in similarly named arrays. You can
take advantage of this to allow for multiple file upload fields.

Operators in PHP

Operators in PHP

When comparing or processing variables and other values you use operators. Without them, PHP would be more of a word jumble instead of a language. In some unique cases, operators slightly alter the relationship between two variables or their function within PHP. Without further adieu,
here they are..

Basic Operators
Add ( + ): $a = 1; $a = $a + 5; // $a is equal to 6
Subtract ( - ): $s = 10; $s = $s - 5; // $s is equal to 5
Multiply ( * ): $m = 2; $m = $m * 10; // $m is equal to 20
Divide ( / ): $d = 20; $d = $d / 5; // $d is equal to 4
Modulus ( % ) Provides the remainder after division:
$u = 5; $u = $u % 2; // $u is equal to 1

Assignment Operators
Add ( += ): $a = 1; $a += 5; // $a is equal to 6
Subtract ( -= ): $s = 10; $s -= 5; // $s is equal to 5
Multiply ( *= ): $m = 2; $m *= 10; // $m is equal to 20
Divide ( /= ): $d = 20; $d /= 5; // $d is equal to 4
Modulus ( %= ) Provides the remainder after division:
$u = 5; $u %= 2; // $u is equal to 1
Concatenate ( .= ) Join onto the end of a string:
$c = 5; $c .= 2; // $c is now a string, '52'
See Also:
Concatenate – Join together in succession

Comparison Operators
Greater Than ( > ): 2 > 1
Less Than ( < ): 1 < 2
Greater Than or Equal To ( >= ): 2 >= 2 3 >= 2
Less Than or Equal To ( <= ): 2 <= 2 2 <= 3

Short-Hand Plus or Minus one
Also known as:
Increment ( $integer++; )
Decrement ( $integer--; )
Example:
$a = 1;
$a = $a + 1; // $a is now equal to 2
$a++; // $a is now equal to 3
$a--; // $a is now equal to 2 again, same as $a = $a – 1;

@ - Suppress Errors
Placing the commercial at symbol (@) before a function tells PHP to suppress
any errors generated by that function.
Examples:
include('DoesNotExist.txt');
Warning: include(DoesNotExist.txt) [function.include]: failed to open
stream: No such file or directory
@include('DoesNotExist.txt');
// blank output below because the error was suppressed

& - Pass by Reference

References allow two variables to refer to the same content. In other words,
a variable points to its content (rather than becoming that content). Passing
by reference allows two variables to point to the same content under
different names. The ampersand ( & ) is placed before the variable to be
referenced.
Examples:
$a = 1;
$b = &$a; // $b references the same value as $a, currently 1
$b = $b + 1; // 1 is added to $b, which effects $a the same way
echo "b is equal to $b, and a is equal to $a";
b is equal to 2, and a is equal to 2

What is Session

What is Session ?

Web is stateless, which means a new instance of a web page class is re-created each time the page is posted to the server. As we all know, HTTP is a stateless protocol, it can't hold client information on a page. If the user inserts some information and move to the next page, that data will be lost and the user would not be able to retrieve that information. What do we need here? We need to store information. Session provides a facility to store information on server memory. It can support any type of object to store along with our own custom objects. For every client, session data is stored separately, which means session data is stored on a per client basis. Have a look at the following diagram:



Fig: For every client, session data is stored separately
State management using session is one of the best ASP.NET features, because it is secure, transparent from users, and we can store any kind of object in it. Along with these advantages, some times session can cause performance issues in high traffic sites because it is stored in server memory and clients read data from the server

How PHP Works with the Web Server

How PHP Works with the Web Server

The normal process a Web server goes through to deliver a page to a browser is as
follows. It all begins when a browser makes a request for a Web page. Based on the URL,
the browser resolves the address of the Web server, identifies the page it would like, and
gives any other information the Web server may need. Some of this information is about
the browser itself, like its name (Mozilla), its version (25.1), or the operating system
(Linux). Other information given the Web server could include text the user typed into
form fields.

If the request is for an HTML file, the Web server will simply find the file, tell the
browser to expect some HTML text, and then send the contents of the file. The browser
gets the contents and begins rendering the page based on the HTML code. If you have
been programming HTML for any length of time, this will be clear to you.

Hopefully you have also had some experience with CGI scripts. When a Web server gets
a request for a CGI, it can't just send the contents of the file. It must execute the script
first. The script will generate some HTML code, which then gets sent to the browser. As
far as the browser is concerned, it's just getting HTML. The Web server does a bunch of
work that it gets very little recognition for, but Web servers rarely get the respect they
deserve. The medium is definitely not the message.

When a PHP page is requested, it is processed exactly like a CGI, at least to the extent
that the script is not simply sent to the browser. It is first passed through the PHP engine,
which gives the Web server HTML text.
  • Blogger news

    This Blog Contains All Topics Related To Internet, Website Help, Interview Questions, News, Results From Various Resources, Visit Daily For More Interesting And Famous Topics.
  • Random Post

  • About

    We Provide All Information Which you Needed. We Maintain This Blog Very Carefully, If You Find Any Mistake or Any Suggestions Then Please Let Us Know, You Can Contact Us By Comments, On FB Page Or On Google+ Page. ~ Thank You