10 File Handling
10 File Handling
FILE HANDLING
Modes Description
r Open a file for read only.
Open a file for write only. creates a new file
w
even if it exists.
a Open a file in append mode
x Creates a new file for write only.
MODES OF OPENING A FILE
Modes Description
r+ Open a file for read/write.
Open a file for read/write. creates a new file
w+
even if it exists.
a+ Open a file for read/write in append mode.
x+ Creates a new file for read/write.
Open the file for writing, if it doesn’t exist.
c However, if it exists, it isn’t truncated (as in w
mode).
FOPEN() EXAMPLE
<?php
$handle = fopen("hello.txt", "w");
$handle =
fopen("c:/xampp/htdocs/welcome.png", "rb");
$handle = fopen("http://localhost/hello.txt", "r");
?>
CLOSING A FILE
Example:
fclose($handle);
PHP - READ FILE
FGETS() FUNCTION
<?php
$file = fopen("hello.txt", "r");
$str = fgets($file);
echo $str;
fclose($file);
?>
FGETS() EXAMPLE – READ FILE UNTIL END
OF FILE
<?php
$file = fopen("hello.txt", "r");
while(! feof($file)) {
echo fgets($file). "<br>";
}
fclose($file);
?>
FGETC() FUNCTION
<?php
$file = fopen("hello.txt", "r");
$str = fgetc($file);
echo $str;
fclose($file);
?>
FGETS() EXAMPLE – READ FILE UNTIL END
OF FILE
<?php
$file = fopen("hello.txt", "r");
while(! feof($file)) {
$char = fgetc($file);
if ($char == "\n")
echo "<br>";
echo $char;
}
fclose($file);
?>
FREAD() FUNCTION
<?php
$name = "hello.txt";
$file = fopen($name, "r");
$data = fread($file, filesize($name));
echo $data;
fclose($file);
?>
FSCANF() FUNCTION
<?php
$fp = fopen("employees.txt", "r");
while ($employee_info = fscanf($fp, "%s\t%s\t%s\t%d\n")) {
list ($name, $email, $post, $salary) = $employee_info;
echo "<b>Name</b>: $name <b>Email</b>:
$email <b>Salary</b>: Rs. $salary <br>";
}
fclose($fp);
?>
PHP - WRITE FILE
FPUTS() FUNCTION
<?php
$fp = fopen("hello.txt", "w");
$bytes = fputs($fp, "Hello World\n");
echo "bytes written: $bytes";
fclose($fp);
?>
FPUTS() EXAMPLE - APPEND
<?php
$fp = fopen("hello.txt", “a");
$bytes = fputs($fp, "Hello World\n");
echo "bytes written: $bytes";
fclose($fp);
?>
FPUTS() EXAMPLE – COPY FILE TO
ANOTHER
<?php
$file = fopen("hello.txt", "r");
$newfile = fopen("new.txt", "w");
while(! feof($file)) {
$str = fgets($file);
fputs($newfile, $str);
}
fclose($file);
fclose($newfile);
?>
FWRITE() FUNCTION
<?php
$file = fopen("/PhpProject/sample.txt", "w");
echo fwrite($file, "Hello using fwrite() function");
fclose($file);
?>
PHP - FILE EXISTENCE
FILE_EXISTS() FUNCTION
<?php
$filename = 'hello.txt';
if (file_exists($filename)) {
$message = "The file $filename exists";
} else {
$message = "The file $filename does not exist";
}
echo $message;
?>
IS_FILE() FUNCTION
is_readable() Function
• Sometimes, you may want to check before hand if
the file can be read from or not. The is_readable()
function can ascertain this fact.
is_writable() Function
• You can use the is_writable() function to can check if
a file exists and if it is possible to perform write
operation on the given file.
PHP – DELETE FILE
Example:
<?php
$file = "my_file.txt";
if (unlink($file)) {
echo "The file was deleted successfully.";
} else {
echo "The file could not be deleted.";
}
?>
RENAME A FILE IN PHP