Workaround for PHP file_exists on ZIP file contents

Recently, I was writing some PHP code, to check if a specific file existed in a ZIP file. PHP has this special feature called "stream wrappers", which basically is a system which enables PHP to do I/O operations on streams.

A stream can be a file, a socket, a SSH connection, ... Each of these streams has its own wrapper, which serves as an adapter between PHP and the underlying resource. This enables PHP to do, for example, a file_get_contents() on all sorts of streams.

Assuming regular PHP file functions would be sufficient, I coded the following:


if (file_exists('zip://')) { ... }


Knowing that the file readme.txt existed in the ZIP file, I was surprised that this function always returned false... What was even more surprising, is that a file_get_contents('zip://') returned readme.txt's data!

The reason for this is unknown to me, but I've written a (dirty) workaround that you can use:


function zipped_file_exists($pFileName = '') {
    if ($pFileName != '') {
        $fh = fopen($pFileName, 'r');
        $exists = ($fh !== false);

        return $exists;

    return false;


This is an imported post. It was imported from my old blog using an automated tool and may contain formatting errors and/or broken images.

Leave a Comment