PHP Unit 5 English
PHP Unit 5 English
Include and Require Files
The include() and require() statement allow you to include the code contained in a PHP file within another PHP file. Including a file
produces the same result as copying the script from the file specified and pasted into the location where it is called.
You can save a lot of time and work through including files — Just store a block of code in a separate file and include it wherever you want
using the include() and require() statements instead of typing the entire block of code multiple times. A typical example is including the
header, footer and menu file within all the pages of a website.
<?php
include ("external_file.php");
?>
require() function
<?php
include ("external_file.php");
?>
The only difference is — the include() statement will only generate a PHP warning but allow script execution to continue if the file to be
included can't be
Opening a file
Reading a file
Writing a file
Closing a file
fopen() – PHP fopen() function is used to open a file. First parameter of fopen() contains name of the file which is to be opened
and second parameter tells about mode in which file needs to be opened, e.g.,
<?php
$file = fopen(“demo.txt”,'w');
?>
$s=fread($fp,20);
echo $s;
?>
3) fwrite() – New file can be created or text can be appended to an existing file using fwrite() function. Arguments for
fwrite() function are file pointer and text that is to written to file. It can contain optional third argument where length of
text to written is specified.
For Example
<?php
?>
fclose() – file is closed using fclose() function. Its argument is file which needs to be closed, e.g.,
<?php
fclose($file);
?>
<?php
?>
Delete
The rmdir() function in PHP is an inbuilt function which is used to remove an empty
directory. It is mandatory for the directory to be empty, and it must have the relevant
The directory to be deleted is sent as a parameter to the rmdir() function and it returns
<?php
?>
changing a directory
The chdir() function is used to change the current working directory to a specified dir.
<?php
chdir('test');
echo getcwd() ;
?>
PHP File Upload
PHP allows you to upload single and multiple files through few lines of code only.
PHP file upload features allows you to upload binary and text files both. Moreover, you can have the full control over the file to be uploaded
through PHP authentication and file operation functions.
PHP $_FILES
The PHP global $_FILES contains all the information of file. By the help of $_FILES global, we can get file name, file type, file size, temp file
name and errors associated with file.
$_FILES['filename']['name']
$_FILES['filename']['type']
$_FILES['filename']['size']
$_FILES['filename']['tmp_name']
returns temporary file name of the file which was stored on the server.
$_FILES['filename']['error']
move_uploaded_file() function
The move_uploaded_file() function moves the uploaded file to a new location. The move_uploaded_file() function checks internally if the file is
uploaded thorough the POST request. It moves the file if it is uploaded through the POST request.
uploadform.html
<form action="uploader.php" method="post" enctype="multipart/form-data">
Select File:
<input type="file" name="fileToUpload"/>
<input type="submit" value="Upload Image" name="submit"/>
</form>
uploader.php
<?php
$target_path = "e:/";
$target_path = $target_path.basename( $_FILES['fileToUpload']['name']);
if(move_uploaded_file($_FILES['fileToUpload']['tmp_name'], $target_path)) {
echo "File uploaded successfully!";
} else{
echo "Sorry, file not uploaded, please try again!";
}
?>
OBJECT-ORIENTED PROGRAMMING IN
PHP
PHP is a server-side scripting language, mainly used for web development but also used as a general-purpose programming language. Object-Oriented
Programming (PHP OOP), is a type of programming language principle added to php5,
that helps in building complex, reusable web applications.
<?php
class Test
private $x,$y,$z;
$this->x=10;
$this->y=20;
$this->z=$this->x+$this->y;
echo "z=".$this->z;
$ob=new Test();
$ob->getData();
$ob->sum();
?>
CONSTRUCTOR
<?php
class Test
{
private $x,$y,$z;
public function __construct() // constructor
{
$this->x=10;
$this->y=20;
}
public function sum()
{
$this->z=$this->x+$this->y;
echo "z=".$this->z;
}
}
$ob=new Test();
$ob->sum();
?>