Chapter 3 _ PHP File
Chapter 3 _ PHP File
Information in the phpinfo.php page describes the temporary directory that is used for file
uploads as upload_tmp_dir and the maximum permitted size of files that can be uploaded is
stated as upload_max_filesize. These parameters are set into PHP configuration file php.ini
For creating HTML form to upload a file , method attribute must be set to post and enctype
attribute is set to multipart/form-data.
There is one global PHP variable called $_FILES. This variable is an associate double dimension
array and keeps all the information related to uploaded file. So if the value assigned to the
input's name attribute in uploading form was file, then PHP would create following five
variables −
Example 1 :
<html>
<body>
<br>
</form>
<body>
</html>
<?php
if(isset($_POST['b1']))
?>
<html>
<body>
<br>
3 Chapter 3 : PHP File
</form>
<body>
</html>
<?php
if(isset($_POST['b1']))
$s=$_FILES["f1"]["tmp_name"];
$t="upload/". $_FILES["f1"]["name"];
if(move_uploaded_file($s,$t))
else
?>
<html>
<body>
<br>
</form>
<body>
</html>
<?php
if(isset($_POST['b1']))
$type=$_FILES["f1"]["type"];
if($type=="image/gif" || $type=="image/jpeg")
$s=$_FILES["f1"]["tmp_name"];
$t="upload/". $_FILES["f1"]["name"];
if(move_uploaded_file($s,$t))
else
else
?>
<html>
<body>
<br>
</form>
<body>
</html>
<?php
if(isset($_POST['b1']))
$size=$_FILES["f1"]["size"];
if($size<=10240)
$s=$_FILES["f1"]["tmp_name"];
$t="upload/". $_FILES["f1"]["name"];
if(move_uploaded_file($s,$t))
else
6 Chapter 3 : PHP File
else
?>
header(“content-type:application/octet-stream”);
Content-type determines how you want to read the page. Application/octet-stream means
downloading a file.
The ob_clean() function deletes all of the contents of the topmost output buffer, preventing
them from getting sent to the browser.
At last readfile() is used to read a file and write to the output data.
<html>
<body>
<tr>
<th>Select file
7 Chapter 3 : PHP File
<tr>
</table>
</form>
</body>
</html>
<?php
if(isset($_POST['b1']))
$filename=$_FILES["f1"]["name"];
$newname=”kbssc”;
header("content-type:application/octet-stream");
header("content-disposition:attachment;filename=$newname.$ext”);
ob_clean();
readfile($filename);
?>
PHP cookie
Cookie is a text file stored in the client computer. Cookie is used to identify a user in php. We
can create, retrieve and delete cookie value.
Creating a cookie :
8 Chapter 3 : PHP File
Syntax : setcookie(name,value[,expire])
Expire : It specify a future time in second. After that time, cookie will become inaccessible. This
parameter is optional.
<?php
setcookie(“uname”,”kbssc”,time()+3600);
setcookie(“password”,”bscitbca”);
?>
PHP provides $_COOKIE associative array which is used to retrieve values of cookie variable.
<?php
?>
<?php
print_r($_COOKIE);
?>
setcookie("a",10);
if(isset($_COOKIE["a"]))
else
?>
When you want to delete a cookie variable, you should ensure that the expiry time is in the
past.
<?php
setcookie("uname","kbssc",time()-1);
?>
PHP session
PHP session variable is used to store information for the user.
Session variable stores information about a single user and it is ava ilable to all pages in the
application.
Session variable is temporary and will be deleted after the user left.
Starting a session :
Session_start() is used to start a session. Before you can store and retrieve session information,
you must first start a session with this function. Session_start() will register a user for storing
variable.
<?php
session_start();
$_SESSION["uanme"]="kbssc";
$_SESSION["password"]="bscitbca";
?>
<?php
session_start();
echo $_SESSION["uanme"];
echo "<br>";
echo $_SESSION["password"];
?>
If you want to delete session data, you can use unset() or session_destroy(). If you want to
destroy a particular session variable, unset() is used. If you want to destroy all session data,
session_destroy() is used.
<?php
session_start();
$_SESSION["uname"]="kbssc";
$_SESSION["password"]="bscbscit";
echo "<br>";
11 Chapter 3 : PHP File
echo $_SESSION["uname"];
echo $_SESSION["password"];
unset($_SESSION["password"]);
echo "<hr>";
echo $_SESSION["uname"];
session_destroy();
echo "<hr>";
?>
<?php
session_start();
if(isset($_SESSION["i"]))
$_SESSION["i"]=$_SESSION["i"]+1;
else
$_SESSION["i"]=1;
?>
Cookie Session
Cookie is a small text file stored in visitor’s Session is a small file that is saved on server.
12 Chapter 3 : PHP File
browser.
Cookie can have a long life time. Session can have limited life time because it is
expired when the browser is closed.
Cookies can be disabled if the visitor’s browser Session cannot be disabled because they are
does not allow. stored on server.
Cookies can be edited by the visitor. Session cannot be edited by the visitor.
In PHP, $_COOKIE associative array is used to In PHP, $_SESSION associative array is used to
manage cookie. manage session.
You don’t need to start because it is stored Before using $_SESSION, you have to start
local. session using session_start().
$_SERVER variable
$_SERVER is an associative array which stored information such as address, server name,
protocol and script location. $_SERVER is a super global variable.
Element Description
$_SERVER['SERVER_NAME']
Example :
<html>
<body>
<br>
</form>
</body>
</html>
<?php
if(isset($_GET['b1']))
?>
Output :
PHP_SELF : /check_php/server.php
SERVER_ADDR : 127.0.0.1
SERVER_NAME : localhost
SERVER_SOFTWARE : Apache/2.4.4 (Win32) PHP/5.4.16
SERVER_PROTOCOL : HTTP/1.1
REQUEST_METHOD : GET
QUERY_STRING : t1=kbssc&b1=Display
SCRIPT_NAME : /check_php/server.php
Environment variable
Environment variables are associative array passed to the current script.
1. $GLOBALS : It is the PHP global variable which is used to access global variables from
anywhere in PHP script.
2. $_GET : It returns the form field values passed using get method.
3. $_POST: It returns the form field values passed using post method.
4. $_REGUEST: It returns the form field values passed using either get or post method.
5. $_FILES: It is used in file upload. It is two dimensional array and returns the uploaded
file name, size, type and temporary name.
6. $_COOKIE: It is used to retrieve cookie variable.
7. $_SESSION: It is used to set and return session data.
8. $_SERVER: It returns the server identification information.