php file_exists() 期望参数 1 是一个有效的路径,给定的字符串

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14857080/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 08:05:22  来源:igfitidea点击:

file_exists() expects parameter 1 to be a valid path, string given

php

提问by Sean

I'm designing a web application that can be customized based on which retail location the end user is coming from. For example, if a user is coming from a store called Farmer's Market, there may be customized content or extra links available to that user, specific to that particular store. file_exists() is used to determine if there are any customized portions of the page that need to be imported.

我正在设计一个 Web 应用程序,该应用程序可以根据最终用户来自哪个零售地点进行自定义。例如,如果用户来自一家名为 Farmer's Market 的商店,则可能会有针对该特定商店的定制内容或额外链接可供该用户使用。file_exists() 用于确定页面是否有任何需要导入的自定义部分。

Up until now, we've been using a relatively insecure method, in which the item ID# and the store are simply passed in as GET parameters, and the system knows to apply them to each of the links within the page. However, we're switching to a reversible hash method, in which the store and item number are encrypted (to look something like "gd651hd8h41dg0h81"), and the pages simply decode them and assign the store and ID variables.

到目前为止,我们一直在使用一种相对不安全的方法,其中项目 ID# 和商店只是作为 GET 参数传入,系统知道将它们应用于页面中的每个链接。然而,我们正在切换到可逆散列方法,其中商店和商品编号被加密(看起来像“gd651hd8h41dg0h81”),页面简单地解码它们并分配商店和 ID 变量。

Since then, however, we've been running into an error that Googling extensively hasn't found me an answer for. There are several similar blocks of code, but they all look something like this:

然而,从那以后,我们一直遇到一个错误,谷歌搜索广泛没有找到我的答案。有几个类似的代码块,但它们看起来都像这样:

$buttons_first = "../stores/" . $store . "/buttons_first.php";

if(file_exists($buttons_first))
{
    include($buttons_first);
}

(The /stores/ directory is actually in the directory above the working one, hence the ../)

(/stores/ 目录实际上位于工作目录上方的目录中,因此是 ../)

Fairly straightforward. But despite working fine when a regular ID and store is passed in, using the encrypted ID throws this error for each one of those similar statements:

非常坦率的。但是,尽管在传入常规 ID 和存储时工作正常,但使用加密 ID 会为每个类似语句抛出此错误:

Warning: file_exists() expects parameter 1 to be a valid path, string given in [url removed] on line 11

警告:file_exists() 期望参数 1 是有效路径,字符串在第 11 行的 [url removed] 中给出

I've had the script spit back the full URL, and it appears to be assigning $store correctly. I'm running PHP 5.4.11 on 1&1 hosting (because I know they have some abnormalities in the way their servers work), if that helps any.

我已经让脚本回吐了完整的 URL,它似乎正确地分配了 $store。我在 1&1 主机上运行 PHP 5.4.11(因为我知道他们的服务器工作方式有一些异常),如果有帮助的话。

回答by naviciroel

I got the same error before but I don't know if this solution of mine works on your problem you need to remove the "\0" try replace it:

我之前遇到过同样的错误,但我不知道我的这个解决方案是否适用于您的问题,您需要删除“\0”尝试替换它:

$cleaned = strval(str_replace("##代码##", "", $buttons_first));

it worked on my case.

它适用于我的情况。

回答by Anonymous

Run a var_dump(strpos($buttons_first,"\0")), this warning could come up when a path has a null byte, for security reasons. If that doesn't work, check the length of the string and make sure it is what you'd expect, just in case there are other invisible bytes.

运行 var_dump(strpos($buttons_first,"\0")),出于安全原因,当路径具有空字节时,可能会出现此警告。如果这不起作用,请检查字符串的长度并确保它是您所期望的,以防万一还有其他不可见的字节。

回答by Gargron

It may be a problem with the path as it depends where you are running the script from. It's safer to use absolute paths. To get the path to the directory in which the current script is executing, you can use dirname(__FILE__).

路径可能有问题,因为它取决于您从何处运行脚本。使用绝对路径更安全。要获取当前脚本正在执行的目录的路径,您可以使用dirname(__FILE__).

回答by phpalix

Add /before stores/, you are better off using absolute paths.

添加/before stores/,最好使用绝对路径。