IsomorphicLocalPHP
Implemented by
Index
Properties
absoluteUrl
documentRoot
Methods
addEventListener
Adds an event listener for a PHP event.
Parameters
eventType: request.end | request.error | runtime.initialized | runtime.beforedestroy
The type of event to listen for.
listener: PHPEventListener
The listener function to be called when the event is triggered.
Returns void
chdir
Changes the current working directory in the PHP filesystem. This is the directory that will be used as the base for relative paths. For example, if the current working directory is
/root/php
, and the path isdata
, the absolute path will be/root/php/data
.Parameters
path: string
The new working directory.
Returns void
defineConstant
Defines a constant in the PHP runtime.
Parameters
key: string
The name of the constant.
value: null | string | number | boolean
The value of the constant.
Returns void
fileExists
Checks if a file (or a directory) exists in the PHP filesystem.
Parameters
path: string
The file path to check.
Returns boolean
True if the file exists, false otherwise.
internalUrlToPath
Parameters
internalUrl: string
Returns string
isDir
Checks if a directory exists in the PHP filesystem.
Parameters
path: string
– The path to check.
Returns boolean
True if the path is a directory, false otherwise.
listFiles
Lists the files and directories in the given directory.
Parameters
path: string
The directory path to list.
optionaloptions: ListFilesOptions
Options for the listing.
Returns string[]
The list of files and directories in the given directory.
mkdir
Recursively creates a directory with the given path in the PHP filesystem. For example, if the path is
/root/php/data
, and/root
already exists, it will create the directories/root/php
and/root/php/data
.Parameters
path: string
The directory path to create.
Returns void
mkdirTree
Parameters
path: string
Returns void
mv
Moves a file or directory in the PHP filesystem to a new location.
Parameters
oldPath: string
The path to rename.
newPath: string
The new path.
Returns void
onMessage
Listens to message sent by the PHP code.
To dispatch messages, call:
post_message_to_js(string $data)
Arguments:
$data (string) – Data to pass to JavaScript.Parameters
listener: MessageListener
Callback function to handle the message.
Returns void
pathToInternalUrl
Parameters
path: string
Returns string
readFileAsBuffer
Reads a file from the PHP filesystem and returns it as an array buffer.
Parameters
path: string
The file path to read.
Returns Uint8Array
The file contents.
readFileAsText
Reads a file from the PHP filesystem and returns it as a string.
Parameters
path: string
The file path to read.
Returns string
The file contents.
removeEventListener
Removes an event listener for a PHP event.
Parameters
eventType: request.end | request.error | runtime.initialized | runtime.beforedestroy
The type of event to remove the listener from.
listener: PHPEventListener
The listener function to be removed.
Returns void
request
Parameters
request: PHPRequest
Returns Promise<PHPResponse>
rmdir
Removes a directory from the PHP filesystem.
Parameters
path: string
The directory path to remove.
optionaloptions: RmDirOptions
Options for the removal.
Returns void
run
Runs PHP code.
This low-level method directly interacts with the WebAssembly PHP interpreter.
Every time you call run(), it prepares the PHP environment and:
- Resets the internal PHP state
- Populates superglobals ($_SERVER, $_GET, etc.)
- Handles file uploads
- Populates input streams (stdin, argv, etc.)
- Sets the current working directory
You can use run() in two primary modes:
Code snippet mode
In this mode, you pass a string containing PHP code to run.
const result = await php.run({
code: `<?php echo "Hello world!";`
});
// result.text === "Hello world!"In this mode, information like DIR or FILE isn’t very useful because the code is not associated with any file.
Under the hood, the PHP snippet is passed to the
zend_eval_string
C function.File mode
In the file mode, you pass a scriptPath and PHP executes a file found at a that path:
php.writeFile(
"/www/index.php",
`<?php echo "Hello world!";"`
);
const result = await php.run({
scriptPath: "/www/index.php"
});
// result.text === "Hello world!"In this mode, you can rely on path-related information like DIR or FILE.
Under the hood, the PHP file is executed with the
php_execute_script
C function.The
run()
method cannot be used in conjunction withcli()
.Parameters
options: PHPRunOptions
PHP runtime options.
Returns Promise<PHPResponse>
setPhpIniEntry
Sets a value for a specific key in the php.ini file for the PHP instance.
Parameters
key: string
The key to set the value for.
value: string
The value to set for the key.
Returns void
setPhpIniPath
Sets the path to the php.ini file to use for the PHP instance.
Parameters
path: string
The path to the php.ini file.
Returns void
setSapiName
Sets the SAPI name exposed by the PHP module.
Parameters
newName: string
The new SAPI name.
Returns void
unlink
Removes a file from the PHP filesystem.
Parameters
path: string
The file path to remove.
Returns void
writeFile
Overwrites data in a file in the PHP filesystem. Creates a new file if one doesn’t exist yet.
Parameters
path: string
The file path to write to.
data: string | Uint8Array
The data to write to the file.
Returns void
Use PHPRequestHandler instead.