This is an automated email from the ASF dual-hosted git repository.
Xuanwo pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/opendal.git
The following commit(s) were added to refs/heads/main by this push:
new af1ae15c9d fix(bindings/php): preserve bytes in Operator::write (#9296)
af1ae15c9d is described below
commit af1ae15c9d50be908082cd6014b070a6c6082a7a
Author: Xuanwo <[email protected]>
AuthorDate: Sat Nov 28 01:15:02 2026 +0800
fix(bindings/php): preserve bytes in Operator::write (#9296)
Read PHP string contents as bytes so write accepts binary data while
keeping the existing PHP signature.
---
bindings/php/opendal-php.stubs.php | 6 ++++--
bindings/php/src/lib.rs | 10 ++++++----
bindings/php/tests/Feature/BasicIOTest.php | 22 ++++++++++++++--------
bindings/php/tests/Unit/BasicTest.php | 8 ++++----
website/docs/20-bindings/php/01-overview.md | 3 +--
website/docs/20-bindings/php/02-getting-started.md | 15 ++++++++-------
6 files changed, 37 insertions(+), 27 deletions(-)
diff --git a/bindings/php/opendal-php.stubs.php
b/bindings/php/opendal-php.stubs.php
index ff28bb83c3..e064607d90 100644
--- a/bindings/php/opendal-php.stubs.php
+++ b/bindings/php/opendal-php.stubs.php
@@ -81,12 +81,14 @@ namespace OpenDAL {
public function __construct(string $scheme_str, array $config) {}
/**
- * Write string into given path.
+ * Write a PHP string to the given path, preserving every byte.
*/
public function write(string $path, string $content): mixed {}
/**
- * Write bytes into given path, binary safe.
+ * Write an array of bytes to the given path.
+ *
+ * Prefer `write` for binary strings.
*/
public function write_binary(string $path, array $content): mixed {}
diff --git a/bindings/php/src/lib.rs b/bindings/php/src/lib.rs
index 2185dd2e20..7333f83330 100644
--- a/bindings/php/src/lib.rs
+++ b/bindings/php/src/lib.rs
@@ -54,15 +54,17 @@ impl Operator {
Ok(Operator(op))
}
- /// Write string into given path.
- pub fn write(&self, path: &str, content: String) -> PhpResult<()> {
+ /// Write a PHP string to the given path, preserving every byte.
+ pub fn write(&self, path: &str, content: Binary<u8>) -> PhpResult<()> {
self.0
- .write(path, content)
+ .write(path, Vec::from(content))
.map(|_| ())
.map_err(format_php_err)
}
- /// Write bytes into given path, binary safe.
+ /// Write an array of bytes to the given path.
+ ///
+ /// Prefer `write` for binary strings.
pub fn write_binary(&self, path: &str, content: Vec<u8>) -> PhpResult<()> {
self.0
.write(path, content)
diff --git a/bindings/php/tests/Feature/BasicIOTest.php
b/bindings/php/tests/Feature/BasicIOTest.php
index 330e158e63..b53da40b71 100644
--- a/bindings/php/tests/Feature/BasicIOTest.php
+++ b/bindings/php/tests/Feature/BasicIOTest.php
@@ -69,9 +69,10 @@ describe('basic io with memory', function () {
});
it('write/read file', function () use ($op) {
- $op->write('test.txt', 'hello world');
+ $content = 'hello 🌰';
+ $op->write('test.txt', $content);
expect($op->is_exist('test.txt'))->toEqual(1)
- ->and($op->read('test.txt'))->toEqual('hello world');
+ ->and($op->read('test.txt'))->toBe($content);
});
it('write/read file overwrite', function () use ($op) {
@@ -103,31 +104,36 @@ describe('basic io with memory', function () {
describe('binary safe IO with fs', function () {
$op = new \OpenDAL\Operator('fs', ['root' => '/tmp']);
- it('write & read invalid UTF-8', function () use ($op) {
+ it('writes and reads a byte array', function () use ($op) {
$content = "hello 🌰 \x80\x80\x80 🍋";
$bytesArray = unpack('C*', $content);
expect($bytesArray)->toBeArray();
$op->write_binary('test.txt', $bytesArray);
- $content = $op->read('test.txt');
- expect($content)->toBeString()->toEqual($content);
+ expect($op->read('test.txt'))->toBe($content);
});
});
describe('binary safe IO with memory', function () {
$op = new \OpenDAL\Operator('memory', []);
- it('write & read invalid UTF-8', function () use ($op) {
+ it('writes and reads arbitrary string bytes', function () use ($op) {
+ $content = "prefix\0\x80\xff suffix";
+ $op->write('bytes.bin', $content);
+
+ expect($op->read('bytes.bin'))->toBe($content);
+ });
+
+ it('writes and reads a byte array', function () use ($op) {
$content = "hello 🌰 \x80\x80\x80 🍋";
$bytesArray = unpack('C*', $content);
expect($bytesArray)->toBeArray();
$op->write_binary('test.txt', $bytesArray);
- $content = $op->read('test.txt');
- expect($content)->toBeString()->toEqual($content);
+ expect($op->read('test.txt'))->toBe($content);
});
});
diff --git a/bindings/php/tests/Unit/BasicTest.php
b/bindings/php/tests/Unit/BasicTest.php
index 3169139816..20e7ad749c 100644
--- a/bindings/php/tests/Unit/BasicTest.php
+++ b/bindings/php/tests/Unit/BasicTest.php
@@ -76,8 +76,8 @@ it('initialization OpenDAL', function () {
->not->toThrow(Exception::class);
});
-it('invalid UTF-8 encoding', function () {
- $op = new \OpenDAL\Operator('fs', ['root' => '/tmp']);
+it('rejects a path with invalid UTF-8', function () {
+ $op = new \OpenDAL\Operator('memory', []);
- $op->write('test.txt', 'invalid UTF-8: '.chr(0x80));
-})->throws(TypeError::class)->expectExceptionMessage('Invalid value given for
argument `content`.');
+ $op->write("invalid path \x80", 'content');
+})->throws(TypeError::class)->expectExceptionMessage('Invalid value given for
argument `path`.');
diff --git a/website/docs/20-bindings/php/01-overview.md
b/website/docs/20-bindings/php/01-overview.md
index f0552c0cca..e6f25e89c4 100644
--- a/website/docs/20-bindings/php/01-overview.md
+++ b/website/docs/20-bindings/php/01-overview.md
@@ -25,8 +25,7 @@ without notice. It is not ready for production use.
## Capabilities
- **Synchronous API** — all calls block; there is no async variant.
-- **String and binary writes** — `write` for UTF-8 strings, `write_binary` for
- arbitrary byte arrays.
+- **Binary-safe writes** — `write` stores PHP strings byte for byte.
- **Metadata** — `stat` returns content length, content type, ETag, MD5, and
entry mode (file or directory).
- **Directory operations** — `create_dir` creates directories recursively.
diff --git a/website/docs/20-bindings/php/02-getting-started.md
b/website/docs/20-bindings/php/02-getting-started.md
index cf498388d2..a3afd4dc85 100644
--- a/website/docs/20-bindings/php/02-getting-started.md
+++ b/website/docs/20-bindings/php/02-getting-started.md
@@ -60,16 +60,18 @@ if ($op->is_exist("hello.txt") === 1) {
## Write binary data
-`write` requires valid UTF-8. For arbitrary bytes, convert with `unpack` first
-and use `write_binary`:
+Use `write` for text and binary data. It stores the bytes of the PHP string
+as provided.
```php
-$bytes = unpack("C*", $binaryContent); // array of unsigned byte values
-$op->write_binary("data.bin", $bytes);
+$binaryContent = "prefix\0\x80\xff suffix";
+$op->write("data.bin", $binaryContent);
-$content = $op->read("data.bin"); // returns a binary-safe string
+$content = $op->read("data.bin"); // same bytes as $binaryContent
```
+`write_binary` is still available for arrays of byte values.
+
## Create a directory
The trailing slash is required; omitting it produces a `NotADirectory` error.
@@ -98,5 +100,4 @@ try {
An unknown scheme, missing required configuration such as `"root"` for `fs`,
or a missing path can produce an OpenDAL error. PHP argument conversion can
-also throw separately. Use `write_binary` to write content that contains
-invalid UTF-8.
+also throw separately.