The fopen path problem in php I am a newbie when using thinkPHP, I want to use the fopen function to write data, but there are some problems as follows: $ file. menu. json; No. ① $ fileJS_URL.menu.json; No. ② $ fileC: UsersAdministratorDesktopqq. php; but this is a problem with the fopen path in php.
Newbie
When using thinkPHP, I want to use the fopen function to write data, but there are some problems as follows:
$ File = "./menu. json"; // No. ①
// $ File = JS_URL. 'menu. json'; this is not true. ②
// $ File = "C:/Users/Administrator/Desktop/qq. php"; however, data can be successfully written to ③
$ Fp = fopen ($ file, 'w ');
Fwrite ($ fp, $ str );
Fclose ($ fp );
JS_URL is as follows:
define("SITE_URL","http://myblog.com/");
define("CSS_URL",SITE_URL."Public/css/");
define("IMG_URL",SITE_URL."Public/images/");
define("JS_URL",SITE_URL."Public/js/");
------ Solution ----------------------
Reference:
Quote: reference:
You said, "I have tried all three methods for the same file." But I didn't see your computer. I don't know about your menu. the absolute path of the json file does not know the absolute path of your php file. although you know it very well, I do not know if you do not post it. I do not know the path and cannot clearly tell you where the error occurred .. ....
So I can use the same file, just the menu. json file.
The code is like this
$ File = "../View/Menu/menu. json ";
// Second path
$ File = "D:/myphp/Apache/htdocs/myblog/Application/Admin/View/Menu/menu. json"
// The third type
$ File = JS_URL. 'menu. json ';
$ Fp = fopen ($ file, 'w ');
Fwrite ($ fp, $ str );
Fclose ($ fp );
// JS_URL is the following
Define ("SITE_URL", "http://myblog.com /");
Define ("JS_URL", SITE_URL. "Application/Admin/View/Menu/menu. json ");
The problem is:
The first and third paths cannot write strings represented by $ str into the menu. json file, and the file is empty. However, the second path can be used to write files. This is the problem.
Define ("SITE_URL", "http://myblog.com/"); // This statement is equivalent to SITE_URL = http://myblog.com/
Define ("JS_URL", SITE_URL. "Application/Admin/View/Menu/menu. json ");
/* This statement is equivalent to JS_URL = SITE_URL. "Application/Admin/View/Menu/menu. json"
Equivalent to JS_URL = http://myblog.com/Application/Admin/View/Menu/menu.json
*/
So // The third
$ File = JS_URL. 'menu. json ';
That is, $ file = http://myblog.com/Application/Admin/View/Menu/menu.json
Of course, this value is not the same as the original path of the file.
// Second path
$ File = "D:/myphp/Apache/htdocs/myblog/Application/Admin/View/Menu/menu. json"
Do you think the two above will be equal ??!! Http://myblog.com/#can be equivalent to d://myphp/apache/htdocs/myblog. Even if you have configured the directory http://myblog.com/in the apache configuration file: d:/myphp/apache/htdocs/myblog. But it is in URL parsing. you are in the server background. Not a concept. You are confused.
So it should be correct.
$ File = "../View/Menu/menu. json ";
// Second path
$ File = "D:/myphp/Apache/htdocs/myblog/Application/Admin/View/Menu/menu. json"
// The third type
$ File = JS_URL. 'menu. json ';
$ Fp = fopen ($ file, 'w ');
Fwrite ($ fp, $ str );
Fclose ($ fp );
// JS_URL is the following
Define ("SITE_URL", "D:/myphp/Apache/htdocs/myblog ");
Define ("JS_URL", SITE_URL. "Application/Admin/View/Menu/menu. json ");
// -------------- I am a split line
As for the first method you mentioned, $ file = ".. /View/Menu/menu. json "; indicates the View/Menu/menu in the root directory of the current directory. json. But I don't know the path of your php (you didn't tell me how I could know it !!)
------ Solution ----------------------
Of course this is not the case. you can use the http path to write data.
It must be written in relative or absolute paths.
First, you need to define constants to specify them to the root directory.
Then add the path.
For example, write a file in the current directory.
$ File = dirname (_ FILE _). '/abc. json ';
$ Fp = fopen ($ file, 'w ');
Fwrite ($ fp, $ str );
Fclose ($ fp );
?>
Your echo dirname (_ FILE _) will know what the current directory is.