ZF-12251: readdir() doesn't return expected results when using Zend_Service_Amazon_S3_Stream wrapper

Issue Type: Bug Created: 2012-05-25T16:33:16.000+0000 Last Updated: 2012-05-25T16:39:49.000+0000 Status: Open Fix version(s): Reporter: Shane Iseminger (shaneiseminger) Assignee: Enrico Zimuel (zimuel) Tags: - Zend_Service_Amazon

  • amazon

Related issues: Attachments:


When using the S3 stream wrapper:

Regardless of path passed to opendir(), readdir() iterates through all files in the bucket, and returns a full pathname for each file. Zend_Service_Amazon_S3_Stream should pass 'prefix' and 'delimiter' to $s3client->getObjectsByBucket() to restrict listing to path passed to $opendir.

Also normal behavior of readdir() is to return filename only. Having this consistency would enable more seamless switching between wrappers and local filesystem.


Posted by Shane Iseminger (shaneiseminger) on 2012-05-25T16:39:49.000+0000

Fix would be in opendir(), something like the following. I'm using this successfully in a class that extends Zend_Service_Amazon_S3_Stream.

public function dir_opendir($path, $options)

    if (preg_match('@^([a-z0-9+.]|-)+://$@', $path)) {
        $this->_bucketList = $this->_getS3Client($path)->getBuckets();
    else {
        $host = parse_url($path, PHP_URL_HOST);

        // format $dir so it works correctly with S3 bucket
        $dir  = trim(parse_url($path, PHP_URL_PATH), '/') . '/';
        $this->_bucketList = $this->_getS3Client($path)->getObjectsByBucket($host, array( 'delimiter' => '/', 'prefix' => $dir ));

        // use basenames only so that values look like you would normally expect with readdir()
        if (is_array($this->_bucketList)) {
            for ($i = 0; $i < count($this->_bucketList); $i++) {
                $this->_bucketList[$i] = basename($this->_bucketList[$i]);

    return ($this->_bucketList !== false);

Have you found an issue?

See the Overview section for more details.


© 2006-2016 by Zend, a Rogue Wave Company. Made with by awesome contributors.

This website is built using zend-expressive and it runs on PHP 7.