You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
$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.)
The text was updated successfully, but these errors were encountered:
gugglegum
changed the title
Passing dir or file name "0" recognizes as no pathname
Passing dir or file name "0" recognized as no pathname
Sep 22, 2018
The following code works fine:
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":
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:to
and do this replace in other places (
rename()
,move()
,copy()
, etc.)The text was updated successfully, but these errors were encountered: