PHP File Process Function
1. Get a file name, non path
String basename(string path);
Example
$path = “/home/httpd/html/index.php3”;
$filename = basename($path);
2. Change file group permission
Int chgrp(string filename, mixed group);
Notes: the user authority problem and in windows mode it will not change
anything but it will return true.
3. Change file permission
Int chmod(string filename, int mode)
chmod() function as same with linux chmod command
Notes: mode numbers it will not automatic change to octal number.
Example
chmod(“~/test.txt”, 644) //error
chmod(“~/test.txt”,0755)//true
4. Change file owner
Int chown(string filename, mixed user)
Notes: only superuser can use this function
5. Clear stat cache
Void clearstatcache(void)
6. Copy file
Int copy(string source, string dest);
Example
If(!copy(“questbook.html”, “guestbook.html.bak”)) {
Printf(“Backup guestbook.html failed…<BR>\n”);
}
7. Get file path
String dirname(string path);
8. Get directory free space
Float disk_free_space(string directory);
Example
<p>Free space: <?=disk_free_space(“/hone2”)?> bytes</p>
9. Get directory total space
Float disk_total_space(string directory);
10. Close file
Int fclose(int fp);
11. Check end of file
Int feof(int fp);
12. Read char in file
String fgetc(int fp);
Notes: if file is EOF, it will return false.
13. Read one line string(include space) in file
String fgets(int fp, int length);
Length = max string length
14. Read one line string(include space, except <HTML>,PHP) in file
String fgetss(int fp, int length);
15. Read one line string in file and input to array
Array file(string filename);
16. Decision file exists
Int file_exsits(string filename);
17. get last access time with file
Int fileatime(string filename);
18. get inode change time
Int filectime(string filename);
19. get file group ID
int filegroup(string filename);
20. get file inode number
int fileinode(string filename);
21. get last update time
int filemtime(string filename);
22. get file owner ID
int fileowner(string filename);
23. get file permission
int fileperms(string filename);
24. get file size
int filesize(string filename);
25. get file type
string filetype(string filename):
Include: fifo, char, dir, block, link, file, unknown
26. lock file
bool flock(int fp, int operation);
Operation:
1: shared lock
2: exclusive lock
3: releasing a lock
4: non-blocking shared lock
5: non-blocking exclusive lock
27. let buffer data to file
int fflush(int fp);
28. open localhost file or URL
int fopen(string filename, string mode);
Open URL file:
$fp_url = fopen(“http://o3.net/robots.txt”,”r”);
Open FTP file:
$fp_ftp = fopen(“ftp://o3.net/pub/OS/Linux/kernel-2.2.12.tar.bz2”,”w”);
Notes: open FTP server file can’t use rw mode
Open localhost file:
$fp = fopen(“/jollen/test.txt”,”r”);
Mode:
r : read
r+ : read/write
w : if file not exist, it will be open new file with only write
w+ :same by w function and assign read function
a : only read and it will direct to file end positive
a+ : same by a function and assign write
all mode can assign b to open a binary file
29. all data to standard output
int fpassthru(int fp);
Notes: finish output process, it will be close file
30. write string in file
int fputs(int fp, string str, int [length]);
length = write string length
Notes: if miss length argument, it will write all string in the file.
31. Read a binary file or text file with length
String fread(int fp, int length);
Example
<?
$filename = “/tmp/test.txt”;
$fd = fopen( $filename, “r”);
$contents = fread( $fd, filesize($filename));
Fclose($fd);
?>
32. Read file with format
Mixed fscanf(int handle, string format [ string var1]);
Example
<?
$fp = fopen(“users.txt”,”r”);
While($userinfo = fscanf($fp, “%s\t%s\t%s\n”)){
List ($name, $phone, $countrycode) = $userinfo;
}
Fclose($fp);
33. Move read/write point to another way
Int fseek(int fp, int offset);
Notes: fseek() function can’t move URL file
The offset over the file bytes, it will not return false
34. Return position of pointer
Int ftell(int fp);
35. Write a binary or text with length in file
Int fwrite(int fp, string string, int [length]);
36. Change file size
Int ftruncate(int fp, int size);
Example
$fp = fopen(“users.txt”, “w”);
ftruncate($fp, 0);
37. Check directory
Bool is_dir(string filename);
38. Check file executable
Bool is_executable(string filename);
39. Check file
Bool is_file(string filename);
40. Check symbolic link
Bool is_link(string filename);
41. Check file readable
Bool is_readable(string filename);
42. Check file writeable
Bool is_writeable(string filename);
43. Create hard link
Int link(string target, string link);
44. Check link exist
Int linkinfor(string path);
45. Create directory
Int mkdir(string pathname, int mode);
Example
mkdir(“/path/to/my/dir”, 0700);
46. Move upload file
Bool move_uploaded_file(string filename, string destination);
47. Close pipe
Int pclose(int fp);
48. Open pipe
Int popen(string command, string mode);
49. Return associative array with path information
Array pathinfo(string path);
Example
<?
$path = pathinfo(“/var/www/htdocs/index.html”);
Echo $path[“dirname”].”\n”;
Echo $path[“basename”].”\n”;
Echo $path[“extension”].”\n”;
?>
50. Output all
Int readfile(string filename);
51. Read full path with symbolic link
String readlink(string path);
52. Rename
int rename(string oldname, string newname);
53. Remove directory
Int rmdir(string dirname);
Notes: make sure the directory is empty
54. Get a absolute path
String realpath(string path);
<?
$real_path = realpath(“../../conf/httpd.conf”);
Echo $real_path;
?>
Output:
/usr/local/conf/http.conf
55. Get file information of array
Array stat(string filename);
Array:
Device
Inode
Number of links
User id of owner
Group id owner
Device type if inode device
Size in bytes
Time of last assess
Time of last modification
Time of last change
Blocksize for filesystem I/O ( only support in st_blksize OS, other OS default
-1 example Win32)
Number of blocks allocated
56. Get file or symbolic link information
Array lstat(string filename);
Notes: if the file is a symbolic link, it will be return link information
If it is a file, it will be return same as stat() function
57. Create symbolic link
Int symbolic (string target, string link);
58. Create file in directly path
String tempnam(string dir, string prefix);
Notes: if the dir is not exist, it will be create in /tmp directory
59. Update file time or create empty file
Int touch(string filename, int time);
Notes: if not a time, it will be use now time
60. Change umark
Int umark(int mask);
61. Delete file
Int unlink(string filename);
62. Change directory
Int chdir(string directory);
Function as same as linux command cd
63. Create a read directory object
New dir(string directory);
Object properties:
Handle directory file
Path this directory path
Method:
Read read file and directly to next file
Rewind …….
Close close the directory handle
<? $d = dir(“/etc”);
Echo “Handle: “ .$d->handle .”<br>”;
Echo “Path: “ .$d->path”.”<br>”;
While($entry=$d->read())
{
Echo $entry.”<br>”;
}
$d->close();
?>
64. Close directory handle with opendir()
Void closedir(int dir_handle);
65. Open directory handle
Int opendir(string path);
66. Read file with directory handle and go to next file
String readdir(int dir_handle);
67. Redefine directory handle
Void rewinddir(int dir_handle);