2. What we learn?
Random access to file data
Getting information on file
Ownership and permissions
MONICA DESHMANE(H.V.DESAI COLLEGE,PUNE) 2
3. Random access to file data
1.Ftell()
Tells current position of file pointer
$pos=ftell($fp);
2.Rewind()
To set fp at beginning
Rewind($fp);
3.Fseek()
MONICA DESHMANE(H.V.DESAI COLLEGE,PUNE) 3
4. Fseek($fp,offset,[position])
To set fp to perticulat position in file
Ex. Fseek($fp,100,SEEK_CUR);
$data=fread($fp,10);
echo $s;
3 positions-
1. SEEK_SET(1)-bydefault / beginning to offset
2. SEEK_CUR(0)-from current position to offset
3. SEEK_END(2)-from end position to offset
MONICA DESHMANE(H.V.DESAI COLLEGE,PUNE) 4
5. Getting information about file
•Files contain much information about themselves, such as their size,
when they were modified, who owns them, and so forth.
•PHP includes the stat() function to enable you to capture information
about a file by providing the filename as an argument to the function.
MONICA DESHMANE(H.V.DESAI COLLEGE,PUNE) 5
6. Gettting information of file
stat() function contains
0 Dev Device number
1 Ino Inode number
2 Mode Inode protection mode
3 nlink Number of links
4 uid Userid of owner
5 gid Group id of owner
6 rdev Device type, if inode device
7 size Size in bytes
8 atime Time of last access
9 mtime Time of last modification
10 ctime Time of last change
11 blksize Blocksize of file system
12 blocks Number of blocks allocated
7. MONICA DESHMANE(H.V.DESAI COLLEGE,PUNE) 7
To get size of file-f.php
<?php
$arr=stat("abc.txt");
echo $arr[7];
echo $arr['size'];
$size=filesize("abc.txt");
echo $size;
?>
9. Ownership and permissions
1)posix_getpwuid() returns information of uid.
$arr=posix_getpwuid(int uid)
Information can be –
Name Password
Uid Gid
Dir shell
MONICA DESHMANE(H.V.DESAI COLLEGE,PUNE) 9
10. 2)posix_getgrgid()
returns array of information of group
$arr=posix_getpwuid(int gid)
Information can be –
Name Password
Gid Members
<?php
$groupid = posix_getegid();
$groupinfo = posix_getgrgid($groupid);
print_r($groupinfo);
?>
MONICA DESHMANE(H.V.DESAI COLLEGE,PUNE) 10
11. Flock()
flock(file, lock, block)
By allowing so many users at a time to script files may corrupt so for
locking flock() used.
flock($file,LOCK_UN);
MONICA DESHMANE(H.V.DESAI COLLEGE,PUNE) 11
14. Opendir()
opendir(path, context)
Dir handler returned
$dh=opendir(dirname);
<?php
$dir = "/images/";
// Open a directory, and read its contents
if (is_dir($dir)){
if ($dh = opendir($dir)){
//operations
}
closedir($dh);
}
}
?> MONICA DESHMANE(H.V.DESAI COLLEGE,PUNE) 14
20. Difference between require() & include()
MONICA DESHMANE(H.V.DESAI COLLEGE,PUNE) 20
Require() Include()
1 Require(“a,txt”); Include(“a.txt”);
2 Use require() only when file required May require or not
3 Generate error E_COMPILE_ERROR if
file not found
If no file found no error,gives
E_WARNING
4 & stop execution if error & just continue execution though error
21. Show files with specific extenstion
f.html
<html>
<body>
<form method=get action=“f.php">
enter file extenstion<input type=text name="txt"><br>
<input type=submit value="show file">
</form>
</body>
</html>
MONICA DESHMANE(H.V.DESAI COLLEGE,PUNE) 21