PHP常用系统函数

1
2
3
4
5
6
7
8
9
10
chdir()	Changes the current directory
chroot() Changes the root directory
closedir() Closes a directory handle
dir() Returns an instance of the Directory class
getcwd() Returns the current working directory
opendir() Opens a directory handle
readdir() Returns an entry from a directory handle
rewinddir() Resets a directory handle
scandir() Returns an array of files and directories of a specified directory

1
2
3
4
5
6
7
8
9
10
11
12
debug_backtrace()	Generates a backtrace
debug_print_backtrace() Prints a backtrace
error_clear_last() Clears the last error
error_get_last() Returns the last error that occurred
error_log() Sends an error message to a log, to a file, or to a mail account
error_reporting() Specifies which errors are reported
restore_error_handler() Restores the previous error handler
restore_exception_handler() Restores the previous exception handler
set_error_handler() Sets a user-defined error handler function
set_exception_handler() Sets a user-defined exception handler function
trigger_error() Creates a user-level error message
user_error() Alias of trigger_error()
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
basename()	Returns the filename component of a path
chgrp() Changes the file group
chmod() Changes the file mode
chown() Changes the file owner
clearstatcache() Clears the file status cache
copy() Copies a file
delete() See unlink()
dirname() Returns the directory name component of a path
disk_free_space() Returns the free space of a filesystem or disk
disk_total_space() Returns the total size of a filesystem or disk
diskfreespace() Alias of disk_free_space()
fclose() Closes an open file
feof() Checks if the "end-of-file" (EOF) has been reached for an open file
fflush() Flushes buffered output to an open file
fgetc() Returns a single character from an open file
fgetcsv() Returns a line from an open CSV file
fgets() Returns a line from an open file
fgetss() Deprecated from PHP 7.3. Returns a line from an open file - stripped from HTML and PHP tags
file() Reads a file into an array
file_exists() Checks whether or not a file or directory exists
file_get_contents() Reads a file into a string
file_put_contents() Writes data to a file
fileatime() Returns the last access time of a file
filectime() Returns the last change time of a file
filegroup() Returns the group ID of a file
fileinode() Returns the inode number of a file
filemtime() Returns the last modification time of a file
fileowner() Returns the user ID (owner) of a file
fileperms() Returns the file's permissions '
filesize() Returns the file size
filetype() Returns the file type
flock() Locks or releases a file
fnmatch() Matches a filename or string against a specified pattern
fopen() Opens a file or URL
fpassthru() Reads from the current position in a file - until EOF, and writes the result to the output buffer
fputcsv() Formats a line as CSV and writes it to an open file
fputs() Alias of fwrite()
fread() Reads from an open file (binary-safe)
fscanf() Parses input from an open file according to a specified format
fseek() Seeks in an open file
fstat() Returns information about an open file
ftell() Returns the current position in an open file
ftruncate() Truncates an open file to a specified length
fwrite() Writes to an open file (binary-safe)
glob() Returns an array of filenames / directories matching a specified pattern
is_dir() Checks whether a file is a directory
is_executable() Checks whether a file is executable
is_file() Checks whether a file is a regular file
is_link() Checks whether a file is a link
is_readable() Checks whether a file is readable
is_uploaded_file() Checks whether a file was uploaded via HTTP POST
is_writable() Checks whether a file is writable
is_writeable() Alias of is_writable()
lchgrp() Changes the group ownership of a symbolic link
lchown() Changes the user ownership of a symbolic link
link() Creates a hard link
linkinfo() Returns information about a hard link
lstat() Returns information about a file or symbolic link
mkdir() Creates a directory
move_uploaded_file() Moves an uploaded file to a new location
parse_ini_file() Parses a configuration file
parse_ini_string() Parses a configuration string
pathinfo() Returns information about a file path
pclose() Closes a pipe opened by popen()
popen() Opens a pipe
readfile() Reads a file and writes it to the output buffer
readlink() Returns the target of a symbolic link
realpath() Returns the absolute pathname
realpath_cache_get() Returns realpath cache entries
realpath_cache_size() Returns realpath cache size
rename() Renames a file or directory
rewind() Rewinds a file pointer
rmdir() Removes an empty directory
set_file_buffer() Alias of stream_set_write_buffer(). Sets the buffer size for write operations on the given file
stat() Returns information about a file
symlink() Creates a symbolic link
tempnam() Creates a unique temporary file
tmpfile() Creates a unique temporary file
touch() Sets access and modification time of a file
umask() Changes file permissions for files
unlink() deletes a file

下面来看一些常用的函数的返回结果

可以执行系统命令的四个函数

1
2
3
4
shell_exec()
exec()
passthru()
system

image-20220502231730729

getcwd()返回当前目录

image-20220502231904336

file_exists()查看是否存在文件。

image-20220502232030565

pathinfo()查看文件详细信息,但是无返回,我们vardump()来看返回值。

image-20220502232333133

popen()两个参数,前面为bash命令,后面是打开模式,我们这里读取,r

image-20220502232649498

is_uploaded_file()查看是否为http post上传的文件

image-20220502232828144

file_get_contents()读取文件到字符串中。

image-20220502232959702

fgets()读取一行,应该是被禁用了。

image-20220502233038796

所以我们可以先尝试修改权限

chmod()需要两个参数

1
chmod('123456.php', 0777)

image-20220502233238963

查看文件所有者fileowner()

然后就可以修改文件所有者

image-20220502233339948

chown(文件)修改文件所有者。

还有很重要的syslog()函数,先不举例了。

image-20220502235852526

这个竟然没有禁用,,直接目录全穿。scandir()打印详细目录。

一些ftp函数

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
<?php
// connect and login to FTP server
$ftp_server = "ftp.example.com";
$ftp_conn = ftp_connect($ftp_server) or die("Could not connect to $ftp_server");
$login = ftp_login($ftp_conn, $ftp_username, $ftp_userpass);

$command = "ls-al > somefile.txt";

// execute command
if (ftp_exec($ftp_conn,$command))
{
echo "$command executed successfully.";
}
else
{
echo "Execution of $command failed.";
}

// close connection
ftp_close($ftp_conn);
?>

这是ftp_exec()执行函数,用来ftp远程连接后执行命令的函数,需要两个参数,套接字和命令。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
<?php
// connect and login to FTP server
$ftp_server = "ftp.example.com";
$ftp_conn = ftp_connect($ftp_server) or die("Could not connect to $ftp_server");
$login = ftp_login($ftp_conn, $ftp_username, $ftp_userpass);

$file = "php/test.txt";

// Try to set read and write for owner and read for everybody else
if (ftp_chmod($ftp_conn, 0644, $file) !== false)
{
echo "Successfully chmoded $file to 644.";
}
else
{
echo "chmod failed.";
}

// close connection
ftp_close($ftp_conn);
?>

这是ftp_chmod()函数,三个参数,套接字,权限,文件,用于修改远程文件权限。

curl命令集合

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
﹤?php
// 初始化一个 cURL 对象
$curl = curl_init();

// 设置你需要抓取的URL
curl_setopt($curl, CURLOPT_URL, 'http://www.cmx8.cn');

// 设置header
curl_setopt($curl, CURLOPT_HEADER, 1);

// 设置cURL 参数,要求结果保存到字符串中还是输出到屏幕上。
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);

// 运行cURL,请求网页
$data = curl_exec($curl);

// 关闭URL请求
curl_close($curl);

// 显示获得的数据
var_dump($data);

?>

可以看到这里就是套接字的连接。