为什么包含(' ../ myscript.php')会导致错误?

时间:2015-07-02 22:19:24

标签: php ajax wordpress

我一直在尝试调试我的PHP脚本,并且已经将问题缩小到了行

include "../classes.php";

位于我的文件team_manager.php的顶部,您可以在下面看到它。

themes
    my_theme
        js
        management
             team_manager.php
             project_manager.php
        classes.php
        footer.php
        functions.php

我没有正确地走这条路吗?或者classes.php的内容可能有问题吗?如果包含文件可能存在问题,则下面是文件,如果有任何问题立即出现错误,请告诉我。

<?php

final class MySqlInfo
{
    const DBNAME = 'somedb';
    const USER = 'someuser';
    const PSSWD = 'somepassword';
    const TEAMTABLENAME = 'sometablename';

    public function getUser ( )
    {
        return self::USER;
    }

    public function getPassword ( ) 
    {
        return self::PSSWD; 
    }
}

final class MethodResult
{

    public $succeeded; 
    public $message; 

    public MethodResult ( $succeededInit = NULL, $messageInit = NULL )
    {
            this->$succeeded = $succeededInit;
            this->$message = $messageInit;
    }   

}

final class MySite 
{

    const ROOTURL = 'http://asite.com/subsite';

    function getRootUrl()
    {
        return self::ROOTURL;
    }


}

final class TeamManager
{

    private $dbcon;

    public function TeamManager ( )
    {
        $dbcon = mysqli_connect('localhost', MySqlInfo.getUser(), MySqlInfo::getPassword());
        $dbcon->select_db(MySqlInfo::DBNAME);
        // need to add error handling here
    }

    final public class TeamMember 
    {
        public $name; // team member name
        public $title; // team member title
        public $bio; // team member bio
        public $sord; // team member sort order
        public $picfn; // team member profile picture file name
    }

    public function addMember ( TeamMember $M )
    {
        if ($this->$dbcon->connect_error)
        {
            return new MethodResult(false, 'Not connected to database');
        }

        $q = "INSERT INTO " . MySqlInfo::TEAMTABLENAME . " (" . implode( ',' array($M->name, $M->title, $M->bio, $M->sord, $M->picfn) ) . ") VALUES ('" . implode('\',\'', array($_POST['fullname'], $_POST['title'], $_POST['bio'], $_POST['sord'], $targetFileName)) . "')";
        // ^ query for inserting member M to the database

        if (!mysqli_query(this->$dbcon, $q))
        {
            return new MethodResult(false, 'Query to insert new team member failed');
        }

        return new MethodResult(true, 'Successfully added new member' . $M->name);
    }
}

?>

1 个答案:

答案 0 :(得分:0)

如果找不到要包含的文件,

include()可能会导致错误。就这么简单。因此,在包含文件之前,必须确保该文件存在。此外,永远不要使用像../script.php这样的相对路径,因为它们会引入许多问题。一个主要问题是,由于安全原因,一些托管服务提供商不允许相对路径。

因此,为了确保可以包含该文件,只需检查其存在:

<?php

// dirname(__FILE__) returns an absolute path of the current script
// which is being executed

$file = dirname(__FILE__) . '/script.php';

if (is_file($file)) {
   include($file);
} else {
   echo 'File does not exist';
}

另外,我看到你把代码写成了一个老派。您可能需要查看PSR-FIG standards