【发布时间】:2019-02-24 01:04:19
【问题描述】:
如果字符串不是有效路径,我如何检查字符串是否是 PHP 中的有效路径而不发出警告?
当我将file_get_contents、is_file、realpath、file_exists 与不是路径的字符串一起使用时,我收到以下警告。
"function_name()" 期望参数 1 是有效路径,字符串在 [...] 中给出
那么如何判断字符串是否是有效路径。
你到底想做什么?你可能会问...
嗯,我想创建一个这样的函数。
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
}
有时$path_or_content 将是文件的有效路径,有时$path_or_content 将是文件本身的内容(例如,动态创建的图像的二进制数据甚至没有路径(至少还没有))。在后一种情况下,我上面提到的所有这些与字符串相关的函数(例如file_exists())都会引发警告(参见上面的引用)。
我想知道的事情。realpath('xyz') 不会发出警告,但realpath( file_get_contents('path/to/actual/image.jpg') ) 会...
所以realpath 和上面提到的其他函数区分字符串 或有效路径的字符串。 那么我们怎么能事先做呢?
【问题讨论】:
-
file_exists 函数?
-
亲爱的朋友们!请 - 我不会编码 3 天或 3 年 :)
-
你真的必须让你的用例更清晰。
-
二进制内容会抛出这个警告(特别是 NUL)。你还有其他具体的例子吗?
-
@Axel 很高兴你得到了答案。
标签: php validation path suppress-warnings