(new Soapbox())->shout(array_map('strtoupper', $opinions)); //Shaun's blog


Me, elsewhere

GitHub
parseword
Miscellaneous public code

Twitter
@parseword
I don't tweet much

XMPP chat
xmpp@shaunc.com
(Pidgin, Miranda, Swift, etc.)


Perfect is the enemy of good enough.

Resolving PHP error "Warning: filetype(): Lstat failed for..."

Posted July 10, 2016 by shaun

When working with PHP's filetype() function, you may encounter the following error:

Warning: filetype(): Lstat failed for [filename]

This warning means that the argument to filetype() doesn't exist. Consider the following code, which is intended to get the contents of the system's /tmp directory while ignoring any subdirectories:

<?php
function get_file_list($directory) {
    $files = [];
    if (is_dir($directory) && $handle = opendir($directory)) {
        while (($file = readdir($handle)) !== false) {
            if (filetype($directory . $file) === 'file') {
                $files[] = $directory . $file;
            }
        }
        closedir($handle);
    }
    return $files;
}

var_dump(get_file_list('/tmp'));

This code will generate one Warning: filetype(): Lstat failed... for every file in the /tmp directory, and the returned $files array will be empty. Can you spot the error? The value of the $directory variable doesn't end with a slash, so the filetype() function is being called with arguments like /tmpmylog.txt instead of /tmp/mylog.txt. Each time filetype() encounters a nonexistent path, it outputs a warning message.

To fix the problem, the code should guarantee that a valid directory separator exists between the directory name and the filename. Here, strpos() is used to detect when a missing slash must be added:

function get_file_list($directory) {
    $files = [];
    if (strpos($directory, '/', -1) === false) {
        $directory = $directory . '/';
    }
    if (is_dir($directory) && $handle = opendir($directory)) {
        while (($file = readdir($handle)) !== false) {
            if (filetype($directory . $file) === 'file') {
                $files[] = $directory . $file;
            }
        }
        closedir($handle);
    }
    return $files;
}

This code will work as intended, without Warning: filetype(): Lstat failed... errors.



Recent articles

📰 jisusaiche: Java's installer telemetry

📰 BIND client log error "query_find: query_getdb failed"

📰 Resolving "The lang/perl5.24 port has been deleted: Has expired" portmaster error

📰 Armagaddon2 interim fix for Firefox 56 and other old versions

📰 Strange DNS queries: qname "miep", qtype ANY

📰 Undeliverable as addressed: A massive broken spam campaign?

📰 Using WITH_META_MODE and ccache for FreeBSD build boosts

📰 Resolving subversion error E000013: Unable to create pristine install stream

📰 Enhancements to SmokePing's AnotherDNS probe

📰 Generating vanity DNSSEC key tags

📰 DDoS involving forged packets from 23.225.141.70

📰 Website integrity monitoring through version control

📰 SpamAssassin 3.4.2 fixes security problems, adds HashBL and phishing plugins

📰 Bug or turf war? ICQ via Pidgin now fails with "startOSCARSession: Request Timeout"

📰 🎂

▲ Back to top | Permalink to this page