简体   繁体   English

PHP require在根文件夹中不起作用

[英]PHP require does not work in root folder

I am using this to require a file when the user clicks the submit button on the form. 当用户单击表单上的提交按钮时,我使用此文件来要求文件。 This code is locaded in blog.php. 该代码位于blog.php中。 When I have this file in the folder /includes/, the code works perfectly, but I want this file to be in the root folder. 当我在文件夹/ includes /中有此文件时,代码工作正常,但我希望此文件位于根文件夹中。 When I add it in the root folder, I get this error: 当我将其添加到根文件夹中时,出现以下错误:

Warning: require(../includes/addbloguser.php): failed to open stream: No such file or directory in C:\\xampp\\htdocs\\Pentaslash\\blog.php on line 27 警告:require(../ includes / addbloguser.php):无法打开流:第27行的C:\\ xampp \\ htdocs \\ Pentaslash \\ blog.php中没有此类文件或目录

Fatal error: require(): Failed opening required '../includes/addbloguser.php' (include_path='.;C:\\xampp\\php\\PEAR') in C:\\xampp\\htdocs\\test\\blog.php on line 27 致命错误:require():无法在C:\\ xampp \\ htdocs \\ test \\ blog.php中打开所需的'../includes/addbloguser.php'(include_path ='.; C:\\ xampp \\ php \\ PEAR')第27行

<?php
    if(isset($_POST["submit"])){

      require '../includes/addbloguser.php';
    }
?> 

According to your comment, shouldn't it be: 根据您的评论,应该不是:

require './addbloguser.php';

Try using 尝试使用

require __DIR__ . '/addbloguser.php'; 

or 要么

require dirname(__FILE__) . '/addbloguser.php';

so you can get better error messages and see what's missing. 这样你就可以得到更好的错误信息,看看有什么缺失。

This is a problem induced by using relative filepaths. 这是使用相对文件路径引起的问题。 Your relative filepath worked while the file blog.php was in the folder includes because with ../ you were going one directory up (to your root directory) and then back inside includes . 你的相对文件路径工作,而文件blog.php在文件夹includes因为../你是一个目录(到你的根目录)然后回到内部includes But since you moved the file one directory up (to your root directory) you are going with ../ one directory up, outside of your root directory and then try to go into the folder includes , but the folder doesn't exist in the directory above of your root directory (well, if you have luck it does, but the file doesn't exist or it does but the wrong one). 但是,由于您将文件上移了一个目录(到根目录),因此将../移了一个目录,在根目录之外,然后尝试进入includes文件夹,但是该文件夹在目录中不存在。你的根目录上面的目录(好吧,如果你运气好了,但文件不存在或者确实存在但错误的文件)。

To prevent this you should always use absolute filepaths, that way you can move blog.php everywhere as long as your file addbloguser.php stays in the same location. 为了防止这种情况,您应该始终使用绝对文件路径,这样只要您的文件addbloguser.php保留在同一位置,就可以将blog.php移动blog.php任何地方。

A easy way to get the absolute filepath to your project is to define a constant which evaluates to dirname(__FILE__)) in a global file in your project's root directory. 获取项目的绝对文件路径的一种简单方法是在项目的根目录下的全局文件中定义一个常数,该常数的值为dirname(__FILE__)) That way you can always use the constant to do things with files and prevent to modify files to correct relative filepaths if you change something. 这样,您可以始终使用常量来处理文件,并防止修改文件以更正相关文件路径。

Eg global.php is inside your root directory, then you will just write this 例如, global.php位于您的根目录中,那么您只需编写此代码

define('IN_DIR', dirname(__FILE__)); //__FILE__ is a magic constant and includes the absolute filepath to the current file -> global.php

And if you now include in blog.php said file global.php (or the other way around) you can use for your filepaths the defined constant and it will look like this 如果你现在包含在blog.php说文件global.php (或者blog.php说)你可以使用你的文件路径定义的常量,它看起来像这样

require IN_DIR.'/path/to/file.php'

In this case that would be 在这种情况下,这将是

require IN_DIR.'/includes/addbloguser.php'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM