PHP 文件存在但无法运行?
我想检查一个页面是否存在。我的文件是article.php。文章的URL是article.php?id=1article.php?id=2等。但是当我这样检查时它不起作用:
$filecheck = "article.php?id=$id";
if (file_exists($filecheck)) {
echo "This article exists.";
} else {
echo "Sorry this article does not exist.";
}
但它总是返回“抱歉这篇文章不存在。” 我该如何解决这个问题?
I would like to check if a page exists. My file is article.php . The article's URLs are article.php?id=1 article.php?id=2 etc. But when I check it this way it doesn't work:
$filecheck = "article.php?id=$id";
if (file_exists($filecheck)) {
echo "This article exists.";
} else {
echo "Sorry this article does not exist.";
}
But it always returns "Sorry this article does not exist."
How could I fix this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
不要将查询字符串传递给它。
Don't pass the query string to it.
文件“article.php?id=$id”将不存在,因为它不是物理文件。
我假设您正在使用 $id 来查找数据库中存在的文章。如果是这种情况,那么 file_exists 函数不是您所需要的。
您需要做的是编写一个快速的 MySQL 语句来检查该文章是否存在,然后从那里开始。
也许是这样的:
我希望这会有所帮助。如果您还需要什么,请告诉我。
The file "article.php?id=$id" will not exist as it is not a physical file.
I am assuming that you are using the $id to find an article that exists in a database. If this is the case then the file_exists function is not what you need.
What you will need to do is write a quick MySQL statement to check if the article exists and then go from there.
Something like this perhaps:
I hope that helps. Let me know if you need anything else.
这是因为没有名为:article.php?id=$id 的文件,
但可能有一个名为:article.php 的文件:)
It's because there is no file called: article.php?id=$id
There probably is a file called: article.php though :)
如果它们是物理页面而不是动态创建的内容,请使用以下方式:
否则检查 ID 是否在数据库中。
If they are physical page instead of dynamically created content use this way:
Otherwise check the ID whether it is in the DB.
好吧,它找不到该文件的原因是因为其中有一个查询字符串。如果您偶然从其他来源获取此数据并且无法控制是否随其发送查询字符串,那么您可以执行以下操作:
Well the reason it is not finding the file is because you have a querystring in it. If you are by chance getting this data from some other source and can't control if a querystring is sent with it then you can do this: