We are about to imple-
ment the file_open() function. After we open the file and receive a FILE *, we
need to register it with the resource mechanism. The main macro to achieve
this is
ZEND_REGISTER_RESOURCE(rsrc_result, rsrc_pointer, rsrc_type);
An Introduction to Writing PHP Extensions Chap. 15
See Table 15.4 for an explanation of the macro’s arguments.
Table 15.4 ZEND_REGISTER_RESOURCE Macro Arguments
Macro Argument
Parameter Type
rsrc_result
zval *, which should be set with the registered resource information.
rsrc_pointer
Pointer to our resource data.
rsrc_type
The resource id obtained when registering the resource type.
File Functions
Now that you know how to use the ZEND_REGISTER_
RESOURCE() macro, you’re almost ready to write file_open(). There’s only one
more subject we need to cover.
As PHP also runs under multi-threaded servers, you cannot use the stan-
dard C file access functions. This is because a running PHP script in one
thread might change the current working directory, thus leading an fopen()
call using a relative path in another thread failing to open the intended file. To
prevent such problems, the PHP framework provides VCWD (virtual current
working directory) macros that should be used instead of any file access func-
tions that rely on the current working directory. (Table 15.5 lists the available
macros.) The macros behave the same as the functions they replace, and
everything is handled for you transparently. Standard C library functions that
are not available on certain platforms are, therefore, not supported by the
VCWD framework. For example, chown(), which doesn’t exist on Win32, won’t
have a corresponding VCWD_CHOWN() macro defined.
Table 15.5 List of VCWD Macros
Standard C Library
VCWD Macro
Comment
getcwd()
VCWD_GETCWD()
fopen()
VCWD_FOPEN()
open()
VCWD_OPEN()
Used for the two-parameter version.
open()
VCWD_OPEN_MODE(
Used for the three-parameter version of
)
open().
creat()
VCWD_CREAT()
chdir()
VCWD_CHDIR()
getwd()
VCWD_GETWD()
realpath()
VCWD_REALPATH()
rename()
VCWD_RENAME()
stat()
VCWD_STAT()
lstat()
VCWD_LSTAT()
unlink()
VCWD_UNLINK()
mkdir()
VCWD_MKDIR()
rmdir()
VCWD_RMDIR()
opendir()
VCWD_OPENDIR()
popen()
VCWD_POPEN()
Table 15.5 List of VCWD Macros
Standard C Library
VCWD Macro
Comment
access()
VCWD_ACCESS()
utime()
VCWD_UTIME()
chmod()
VCWD_CHMOD()
chown()
VCWD_CHOWN()
