Advanced IP-Chapter-3 - Lect-6, 7
Advanced IP-Chapter-3 - Lect-6, 7
Chapter 3 :
Working with
Files and Directories
include() and require() Functions
2
<?php
$var1=“Hello world!”;
?>
Now let's include that file into our main calling file by
doing the following:
7
<?php
include("my_file.php");
echo $var1;
?>
Hello World!
Here is an example of using each type:
8
<?php
include ("file1.php");
include_once ("file2.php");
require ("file3.php");
require_once ("file4.php");
// All 4 files would be included into your main
script
// file1.php and file3.php can be included
multiple times
// file2.php and file4.php cannot be included
multiple times
?>
Here are alternate ways of writing your include and
require statement values that all work the same:
9
<?php
// All of the lollowing syntax will work the same
include ("my_file.php");
include "my_file.php";
include 'my_file.php';
?>
Communicate Between Two Servers Using PHP
includes
10
Using the include or require statements we can also
perform cross domain scripting from one server, to a
completely different server.
This comes in handy when you are looking to create
PHP processing scripts and engines that nobody can
view the source to but you.
It remains on your server but you can allow other
sites to connect to it.
Maybe even for a fee if the application performs
some cool task that helps people's business.
Here is an example of placing a full URL to
perform PHP scripting between two servers.
11
<?php
// include a script into another script from another server
include "http://www.yourwebsite.com/yourScript.php";
?>
And here is an example of sending variables
through the URL string to the script waiting to
be included.
That script can then process and return values
<?php
// include a script into a caller script from another server, and send variables
include "http://www.yourwebsite.com/yourScript.php?name=John&pin=8558";
?>
Since the include, require, require_once, and
include_once statements all work in a similar way,
all four could be used to script from one server to
another using PHP.
Here are alternate ways of writing the inclusion.
13
<?php
include "http://www.yourwebsite.com/yourScript.php?
name=John&pin=8558";
include_once "http://www.yourwebsite.com/yourScript.php?
name=John&pin=8558";
require "http://www.yourwebsite.com/yourScript.php?
name=John&pin=8558";
require_once "http://www.yourwebsite.com/yourScript.php?
name=John&pin=8558";
14
<?php
// you can create any extension type file(html, php, xml, etc...)
$file_x = "my_file.txt";
$createFile = touch($file_x);
if (file_exists($file_x)) {
echo "The file has been created";
} else {
echo "The file has not been created";
}
?>
<?php
// you can delete any extension type file(html, php, jpg, gif,
flv, swf, etc...)
$file_x = "my_file.txt";
if (file_exists($file_x)) {
unlink($file_x); // delete it here only if it exists
echo "The file has been deleted";
} else {
echo "The file was not found and could not be deleted";
}
?>
The file has been deleted
Opening, Writing, Appending, Closing, and
Reading Files in PHP
21
w+ Reading and writing to a file. If the file does not exist, attempt to create it.
a Write to a file. If the file does not exist, attempt to create it.
a+ Reading and writing. If the file does not exist, attempt to create it.
r Reading file only.
r+ Reading and writing to a file.
Create and open for writing only. If the file already exists, fopen() will fail by returning
x
FALSE and generating an error. If the file does not exist, attempt to create it.
Create and open for reading and writing. If the file already exists, fopen() will fail by
x+
returning FALSE and generating an error. If the file does not exist, attempt to create it.
24
If an attempt to open a file fails then fopen returns
a value of false otherwise it returns a file pointer
which is used for further reading or writing to that
file.
After making a changes to the opened file it is
important to close it with the fclose() function.
The fclose() function requires a file pointer as its
argument and then returns true when the closure
succeeds or false if it fails.
25
This is the string data or code I want to place in the newly created file. Here is more data I want
to append to the file.
In the code example above we created a file, wrote to it, closed it, appended data to it, then
displayed its contents.
Reading a file
29
Once a file is opened using fopen() function it
can be read with a function called fread().
This function requires two arguments. These
<html> <head>
<title>Reading a file using PHP</title>
</head> <body>
<?php
$filename = “my_file.txt";
$file = fopen( $filename, "r" );
if( $file == false )
{
echo ( "Error in opening file" );
exit();
}
32
37
38
39
40
<?php
// Try any file type with any extensions(.jpg, .html, .doc, .pdf, etc...)
$targetFile = "test.html";
$targetCopy = "test_copy.html";
// Use the built in copy function now
copy($targetFile, $targetCopy);
// Now you can choose to run a check to see if the new copy exists,
// or you have the option to do nothing and assume it is made
if (file_exists($targetCopy)) {
echo "Success : $targetCopy has been made";
} else {
echo "Failure: $targetCopy does not exist";
}
?>
Cont…
54
if (file_exists(“sfweather.txt”)) {
if(is_dir(“history”)) {
if (copy(“sfweather.txt”,
“history\\sfweather01-27-2006.txt”))
echo “<p>File copied
successfully.</p>”;
else
echo “<p>Unable to copy the
file!</p>”;
}
else
echo (“<p>The directory does not
exist!</p>”);
}
else
echo (“<p>The file does not exist!</p>”);
Renaming Files and Directories
55
The third number specifies permissions for the owner's user group
Possible values (to set multiple permissions, add up the following numbers):
1 = execute permissions
2 = write permissions
4 = read permissions
Example
<?php
// Read and write for owner, nothing for everybody else
chmod("test.txt",0600);
// Read and write for owner, read for everybody else
chmod("test.txt",0644);
/ Everything for owner, read and execute for everybody else
chmod("test.txt",0755);
// Everything for owner, read for owner's group
chmod("test.txt",0740);
?>
Cont…
60