Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check if string could be a path (without warning)

How can I check if a string is a valid path in PHP without warning if string isn't a valid path?

When I use file_get_contents, is_file, realpath, file_exists with a string that isn't a path I get the following warning.

"function_name()" expects parameter 1 to be a valid path, string given in [...]

So how to determine whether the string could be a valid path or not.


What the hell do you want to do? You may ask...

Well, I want to create a function like this.

funtion my_smart_function( string $path_or_content )
{
    $content = is_file_without_warning_if_not_a_valid_path( $path_or_content )
             ? file_get_contents( $path_or_content )
             :                    $path_or_content;
    // do nice stuff with my $content
}

Sometimes $path_or_content will be a valid path to a file and sometimes $path_or_content will be the content of a file by itself (eg the binary data of an image created on the fly that doesn't even have a path (at least not yet)). In the latter case all these string related functions I mentioned above (eg file_exists()) will throw a warning (see quote above).


Something I'm wondering about.
realpath('xyz') doesn't throw a warning but
realpath( file_get_contents('path/to/actual/image.jpg') ) does...

So realpath and the other functions mentioned above distinguish between a string or a string that is a valid path. So how can we do either beforehand?

like image 937
Axel Avatar asked Sep 19 '18 19:09

Axel


People also ask

How do you check if a string is a file path in Python?

The Python isfile() method is used to find whether a given path is an existing regular file or not. It returns a boolean value true if the specific path is an existing file or else it returns false. It can be used by the syntax : os. path.

How do I check if Java path is correct?

Open a Command Prompt window (Win⊞ + R, type cmd, hit Enter). Enter the command echo %JAVA_HOME% . This should output the path to your Java installation folder. If it doesn't, your JAVA_HOME variable was not set correctly.


1 Answers

This may be a reasonable time to use the @ modifier to suppress errors.

funtion my_smart_function( string $path_or_content )
{
    $content =      @file_exists( $path_or_content )
             ? file_get_contents( $path_or_content )
             :                    $path_or_content;
}

If it's not a valid path, file_exists() will return a falsey value, and the @ will keep it from complaining about the bad string.

On Linux, the only character that's not allowed in a path is the null byte. So you could just check for this:

if (strpos($path_or_contents, "\0") === false) {
    return file_get_contents($path_or_contents);
} else {
    return $path_or_contents;
}
like image 157
Barmar Avatar answered Sep 20 '22 10:09

Barmar