Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Passing dir or file name "0" recognized as no pathname #3

Open
gugglegum opened this issue Sep 22, 2018 · 0 comments
Open

Passing dir or file name "0" recognized as no pathname #3

gugglegum opened this issue Sep 22, 2018 · 0 comments

Comments

@gugglegum
Copy link

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.)

@gugglegum 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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant