PHP code example of microscrap / posix
1. Go to this page and download the library: Download microscrap/posix library . Choose the download type require .
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
microscrap / posix example snippets
$fd = posix_open('/dev/null', O_RDWR);
posix_close($fd);
// O_RDONLY is platform-specific; 0 on Linux/glibc.
$fd = posix_open('/etc/hosts', 0);
if ($fd < 0) {
throw new RuntimeException('open failed');
}
$chunk = posix_read($fd, 256);
posix_close($fd);
// Linux/glibc: O_RDWR | O_CREAT | O_TRUNC
$flags = 2 | 64 | 512;
$fd = posix_open('/tmp/posix-demo.txt', $flags, 0644);
if ($fd < 0) {
throw new RuntimeException('open failed');
}
posix_write($fd, "hello\n", 6);
posix_close($fd);
$fd = posix_open('/dev/null', O_RDWR);
$result = posix_close($fd);
// $result === 0 on success
$path = '/tmp/posix-chmod-demo.txt';
$fd = posix_open($path, 2 | 64 | 512, 0644);
posix_close($fd);
if (posix_chmod($path, 0600) !== 0) {
throw new RuntimeException('chmod failed');
}
$path = '/tmp/owned-by-me.txt';
$fd = posix_open($path, 2 | 64 | 512, 0644);
posix_close($fd);
$uid = (int) posix_getuid();
$gid = (int) posix_getgid();
if (posix_chown($path, $uid, $gid) !== 0) {
throw new RuntimeException('chown failed');
}
$fd = posix_open('/tmp/posix-fchmod.txt', 2 | 64 | 512, 0644);
if (posix_fchmod($fd, 0600) !== 0) {
posix_close($fd);
throw new RuntimeException('fchmod failed');
}
posix_close($fd);
$fd = posix_open('/tmp/posix-fchown.txt', 2 | 64 | 512, 0644);
$uid = (int) posix_getuid();
$gid = (int) posix_getgid();
if (posix_fchown($fd, $uid, $gid) !== 0) {
posix_close($fd);
throw new RuntimeException('fchown failed');
}
posix_close($fd);
$fd = posix_open('/tmp/posix-demo.txt', 2 | 64 | 512, 0644);
$payload = "ping\n";
$written = posix_write($fd, $payload, strlen($payload));
// $written === 5 when all bytes were accepted
posix_close($fd);
$fd = posix_open('/dev/null', O_WRONLY);
posix_write($fd, str_repeat('x', 1024), 1024);
posix_close($fd);
$fd = posix_open('/etc/hosts', 0);
$data = posix_read($fd, 512);
if ($data === false) {
throw new RuntimeException('read failed');
}
echo $data;
posix_close($fd);
$fd = /* descriptor from pipe, socket, or device */;
while (true) {
$chunk = posix_read($fd, 4096);
if ($chunk === false) {
break;
}
if ($chunk === '') {
break; // EOF
}
// process $chunk
}
$uid = posix_getuid();
if (posix_setuid(65534) !== 0) {
// not privileged or operation denied
}
$previous = posix_umask(0077);
// restore prior mask
posix_umask($previous);
$fd = posix_open('/etc/hosts', 0);
$pos = posix_lseek($fd, 0, SEEK_SET);
posix_close($fd);
// $sock must be a connected socket FD.
// $data = posix_recv($sock, 4096, 0);
$fd = posix_open('/etc/hosts', 0);
$out = posix_readv($fd, [
['len' => 4],
['len' => 8],
]);
if ($out !== false) {
// $out['res'] — total bytes read
// $out['buffers'][0], $out['buffers'][1] — segment contents
}
posix_close($fd);
$fd = posix_open('/tmp/posix-demo.txt', 0);
$value = 0;
$res = fcntl($fd, F_GETFL, 0, $value);
// $res — fcntl status
// $value — current O_* flags as integer
$flags = $value;
posix_close($fd);
$fd = posix_open('/tmp/posix-demo.txt', 0);
$current = 0;
fcntl($fd, F_GETFL, 0, $current);
$newFlags = $current | O_NONBLOCK;
$ignored = 0;
fcntl($fd, F_SETFL, $newFlags, $ignored);
posix_close($fd);
$fd = posix_open('/tmp/posix-demo.txt', 2);
$value = 0;
$res = fcntl($fd, F_SETLK, [
'type' => F_WRLCK,
'whence' => SEEK_SET,
'start' => 0,
'len' => 0,
'pid' => 0,
], $value);
// $value contains flock fields after the operation
posix_close($fd);
$fd = posix_open('/dev/null', O_RDWR);
$value = 0;
$res = ioctl($fd, SOME_IOCTL_CMD, null, $value);
posix_close($fd);
$fd = posix_open('/dev/some-device', O_RDWR);
$value = 0;
$res = ioctl($fd, SOME_IOCTL_CMD, 0, $value);
posix_close($fd);
$fd = posix_open('/dev/some-device', O_RDWR);
$value = 0;
$res = ioctl($fd, SOME_IOCTL_CMD, ['value' => 1], $value);
// $value holds the updated integer after ioctl
$out = $value;
posix_close($fd);
$fd = posix_open('/dev/some-device', O_RDWR);
$buffer = str_repeat("\0", 32);
$value = 0;
$res = ioctl($fd, SOME_IOCTL_CMD, ['data' => $buffer], $value);
// $value is the buffer returned from the driver
$response = $value;
posix_close($fd);
$pid = pcntl_fork();
if ($pid === 0) {
exit(42);
}
$status = 0;
$got = posix_wait($status);
// decode with pcntl_wexitstatus($status) when pcntl_wifexited($status)
$status = 0;
$pid = posix_waitpid(-1, $status, WNOHANG);
$host = posix_hostname();
$info = posix_lstat('/tmp/my-symlink');
if ($info !== false) {
// $info['size'], $info['mode'], etc.
}