Directory structure:
project
|---App
|---Public
|---blog-frontend
|---Home
|---js
|---css
|---img
|---xxx.md
|---ThinkPHP
|---index.php
The content of the entry file index.php
is as follows:
if(version_compare(PHP_VERSION,'5.3.0','<')) die('require PHP > 5.3.0 !');
// Turn on debugging mode. It is recommended to turn it on during the development stage. Comment or set it to false during the deployment stage.
define('APP_DEBUG',true);
//Define application directory
define('APP_PATH','./App/');
//Introduce ThinkPHP entry file
require './ThinkPHP/ThinkPHP.php';
// Dear ^_^ You don’t need any code later, it’s that simple
Configured in public config.php
'TMPL_PARSE_STRING' => array(
'__STATIC__' => __ROOT__ . '/Public/blog-frontend',
),
And use resources using Public
in the view: introduce static resources
<script src="__STATIC__/Home/js/jquery.min.js"></script>
There is no problem with the above code and it was introduced successfully.
However, in the controller
function Xaction(){
$file = "/Public/blog-frontend/Home/xxx.md";
$fopen = fopen($file,'r');
var_dump($fopen);
}
Output
false
Strange, I tried fopen in the view again
$file = "__STATIC__/Home/xxx.md";
$fopen = fopen($file,'r');
var_dump($fopen);
Output
false
I tried again fopen
and replaced it with file_get_contents
. The result was still false
whether it was the output in the controller or the view. So I changed the suffix .md
to .txt
and the result was still false
.
How to solve it?
Supplement:
Thank you for your help. Through @jiny's method, the problem has been solved.
For some reason, I print it in IndexController.class.php
var_dump(__ROOT__);
var_dump(__PUBLIC__);
(The above is the default, I have not configured it.)
output:
string(0) ""
string(10) "__PUBLIC__" // if var_dump(__ABCDE__);// string(9) "__ABCDE__"
曾经蜡笔没有小新2017-05-16 13:11:39
function Xaction(){
$file = $_SERVER['DOCUMENT_ROOT']."/Public/blog-frontend/Home/xxx.md";
$fopen = fopen($file,'r');
var_dump($fopen);
}
//在控制器里要这么写 $_SERVER['DOCUMENT_ROOT']
習慣沉默2017-05-16 13:11:39
First you need to understand the directory where PHP
执行 Xaction
这个方法的时候对应的路径是什么?ThinkPHP
中的入口文件担当着入口的作用,意味着方法的执行实际上都是在这一文件中去完成的(各种 require
),所以执行 Xaction
的方法所在路径即为 index.php
is located
It is recommended to use relative paths
Now that you know the path of the current script, it is very clear to obtain the resource files under Public
$file = './Public/blog-frontend/Home/xxx.md'
迷茫2017-05-16 13:11:39
Default __PUBLIC__
You can print it and see.
I don’t think the way you tested is correct. Don’t rush to use multiple ways to prove your idea. Organize a method and go step by step.
某草草2017-05-16 13:11:39
When using fopen, check whether the directory file is configured with corresponding permissions. This may be the reason.
天蓬老师2017-05-16 13:11:39
Thanks for the invitation, tp provides the system constant _PUBLIC_, which can be directly referenced by defining the path under config