w3resource logo


PHP Tutorial

PHP : $_FILES, $_ENV, $_COOKIE, $_SESSION

Secondary Nav

PHP : $_FILES

Description

$_FILES is a super global variable which can be used to upload files. Here we will see an example in which our php script checks if the form to upload the file is being submitted and generates a message if true.

Here is the html code (upload.php): 

<html>
<body>
<form action="upload_file.php" method="post"
enctype="multipart/form-data">
<label for="file">Filename:</label>
<input type="file" name="file" id="file" />
<br />
<input type="submit" name="submit" value="Submit" />
</form>
</body>
</html> 

Code of files.php file:

<?php
if ($_FILES["file"] > 0)
  {
  echo "You have selected a file to upload";
  }
?>

PHP : $_ENV

Description

$_ENV is used to return the environment variables from the web server.

<?php
echo $_ENV['username'];
?>

Example of $_ENV

<?php
echo "my username is ".$_ENV['username'];
?>

PHP : $_COOKIE

Description

Cookies are small text files loaded from a server to a client computer storing some information regarding the client computer, so that when the same page from the server is visited by the user, necessary information can be collected from the cookie itself, decreasing the latency to open the page.

$_COOKIE retrieves those cookies.

<?php
setrawcookie();
print_r($_COOKIE);
?>

PHP : $_SESSION

Description

Sessions are wonderful ways to pass variables. All you need to do is start a session by session_start();Then all the variables you store within a $_SESSION, you can access it from anywhere on the server. Here is an example:  

Example of $_SESSION

<?php
session_start();
$_SESSION['w3resource']='The largest online tutorial';
echo $_SESSION['w3resource'];
?>


Join our Question Answer community to learn and share your programming knowledge.

Help the community:

PHP: Loop

JavaScript: Delete duplicates in an array

Oracle: Round the number