File
File copied to clipboard
Passing dir or file name "0" recognized as no pathname
The following code works fine:
$file = new File('1');
var_dump($file->getRealPath());
and if there is a file or directory named "1" in current directory it prints its full path, false otherwise. But if we will try to do the same thing with file or directory named "0" we will get exception "No pathname was Naucon\File\FileAbstract given":
$file = new File('0');
var_dump($file->getRealPath());
This is common pitfall for most novices. Try to avoid using empty()
function for string values because it recognizes string value "0" as empty, but it's not. Use non-strict comparison with empty string instead. This will not treat "0" string as empty but will treat as empty "" string, false and null. Just replace:
if (is_string($pathname) && !empty($pathname)) {
to
if (is_string($pathname) && $pathname != '') {
and do this replace in other places (rename()
, move()
, copy()
, etc.)