CH 4
CH 4
08/10/2024
….coun’t
3
<?php
$a=fopen('ka.txt', 'w');
fwrite($a,'hello information technology' );
?>
<?php
readfile('ka.txt');
?>
08/10/2024
…coun’t
5
08/10/2024
File Modes
6
08/10/2024
..coun’t
7
08/10/2024
…count
8
<?php
file_put_contents('kassa.txt', ‘Easy Programming ');
echo file_get_contents('kassa.txt');
?>// Easy Programming
File_put_contents function used to write php file like
that of fwrite.
File_get_contents also used to read file from the
dirctory.
08/10/2024
Reading from a File
9
08/10/2024
….coun’t
11
<?php
echo readfile("web.txt");
?>
<?php
$myfile = fopen("web.txt", "r") or die("Unable to open file!");
echo fgets($myfile);
//read single line from the file
fclose($myfile);
?>
<?php
$myfile = fopen("web.txt", "r") or die("Unable to open file!");
// Output one character until end-of-file
while(!feof($myfile)) {
echo fgets($myfile);
//can read whole file using while loop and feof
}
?>
08/10/2024
..count
12
<?php
$myfile = fopen("web.txt", "r") or die("Unable to
open file!");
// Output one character until end-of-file
while(!feof($myfile)) {
echo fgetc($myfile);
}
fclose($myfile);
?>
// After a call to the fgetc() function, the file pointer
moves to the next character.
08/10/2024
Creating and Removing a Directory
13
08/10/2024
Renaming and Deleting Files
14
The rename() function is used to give a new name for your file.
rename (“old name”, “new name”).
<?php
rename("/tmp/tmp_file.txt", "/home/user/login/docs/my_file.txt");
?>
If you want to delete the order file after the orders have been processed, you can do so
by using unlink(). (here
is no function called delete)
unlink($fp)
<?php
$fh=fopen('test.txt','a');
fwrite($fh,'Hello world!');
fclose($fh);
unlink('test.txt');
?>
08/10/2024
…coun’t
15
<?php
if(is_dir("NewDirectory")){
rmdir("NewDirectory");
echo "the new directory is removed!!";
}
?>
Like in files the rename () function is used to
rename the name of directories.
08/10/2024
Overview of PHP file system function
16
08/10/2024
Upload files
17
08/10/2024
File uploading
18
</body>
</html>
08/10/2024
Tips …
19
08/10/2024
Php script to upload
20
Create The Upload File PHP Script
The "upload.php" file contains the code for uploading a file:
<?php
$target_dir = "uploads/";
$target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
$uploadOk = 1;
$imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);
// Check if image file is a actual image or fake image
if(isset($_POST["submit"])) {
$check = getimagesize($_FILES["fileToUpload"]["tmp_name"]);
if($check !== false) {
echo "File is an image - " . $check["mime"] . ".";
$uploadOk = 1;
} else {
echo "File is not an image.";
$uploadOk = 0;
}
}
?>
08/10/2024
tips
21
08/10/2024
Problems with Using Flat Files
22
08/10/2024