PHP 文件存在但无法运行?

发布于 2024-11-30 17:42:21 字数 307 浏览 1 评论 0原文

我想检查一个页面是否存在。我的文件是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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

萌梦深 2024-12-07 17:42:21

不要将查询字符串传递给它。

$filecheck = 'article.php';

Don't pass the query string to it.

$filecheck = 'article.php';
相守太难 2024-12-07 17:42:21

文件“article.php?id=$id”将不存在,因为它不是物理文件。

我假设您正在使用 $id 来查找数据库中存在的文章。如果是这种情况,那么 file_exists 函数不是您所需要的。

您需要做的是编写一个快速的 MySQL 语句来检查该文章是否存在,然后从那里开始。

也许是这样的:

$query = "SELECT * FROM articles WHERE id='$id'";
$result = mysql_query($query);

// Check if result is there (ie article exists)
if ($result) {
    echo "This article exists.";
} else {
    echo "Sorry this article does not exist.";
}

我希望这会有所帮助。如果您还需要什么,请告诉我。

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:

$query = "SELECT * FROM articles WHERE id='$id'";
$result = mysql_query($query);

// Check if result is there (ie article exists)
if ($result) {
    echo "This article exists.";
} else {
    echo "Sorry this article does not exist.";
}

I hope that helps. Let me know if you need anything else.

帅气尐潴 2024-12-07 17:42:21

这是因为没有名为: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 :)

打小就很酷 2024-12-07 17:42:21

如果它们是物理页面而不是动态创建的内容,请使用以下方式:

$filecheck = "article_1.php"

if (file_exists($filecheck)) {
echo "This article exists.";
} else {
echo "Sorry this article does not exist.";
}

否则检查 ID 是否在数据库中。

If they are physical page instead of dynamically created content use this way:

$filecheck = "article_1.php"

if (file_exists($filecheck)) {
echo "This article exists.";
} else {
echo "Sorry this article does not exist.";
}

Otherwise check the ID whether it is in the DB.

半﹌身腐败 2024-12-07 17:42:21

好吧,它找不到该文件的原因是因为其中有一个查询字符串。如果您偶然从其他来源获取此数据并且无法控制是否随其发送查询字符串,那么您可以执行以下操作:

$yourFile = 'article.php?id=$id'; // Or wherever you get this value from
$yourFile = strstr( $yourFile , '?' , TRUE );

echo $yourFile; // now has a value of article.php

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:

$yourFile = 'article.php?id=$id'; // Or wherever you get this value from
$yourFile = strstr( $yourFile , '?' , TRUE );

echo $yourFile; // now has a value of article.php
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文