写入 php 文件时无法打开文件
Unable to open file while writing php file
我正在尝试使用 fopen 和 fwrite
$book="http://bittotb.com/synthesis_study_material/student_admin/include/uploaded/epub/923960_hotel (1).epub";
$path=$book.".php";
$myfile =fopen($path, "w");
$txt = "<?php include('hello.php'); ?>";
fwrite($myfile, $txt);
fclose($myfile);
当我只在fopen中写文件名时
$myfile =fopen("abc.php", "w");
然后它在同一目录中创建一个文件,但我想在另一个目录中创建该文件。在使用路径时如果我回显 $path 则它不起作用然后我得到
http://bittotb.com/synthesis_study_material/student_admin/include/uploaded/epub/923960_hotel (1).epub.php
这是正确的文件名和路径,但仍然无法打开文件,我的文件夹权限显示 0777
您必须使用服务器上的路径,而不是页面的 URL。
例如,您的页面可以有 URL http://example.org/index.php
。该文件可以位于名为 /var/www/example.org/index.php
.
的服务器上
使用此代码确定您的目录:
<?php
echo getcwd();
如果上面的代码显示 /var/www/example.org/
,文件 http://example.org/test.php
的文件路径为 /var/www/example.org/test.php
。但是最好使用相对路径。 (见下文)
如果您有页面 http://example.org/index.php
并且您想要创建 http://example.org/test.php
,请使用:
$file = fopen("test.php", "w");
fwrite($file, "<?php echo 'Hello World'; ?>");
fflush($file);
fclose($file);
如果要从脚本 http://bittotb.com/synthesis_study_material/student_admin/module/corses/file.php
写入文件 http://bittotb.com/synthesis_study_material/student_admin/include/uploaded/epub/file.php
,请使用相对路径:
$file = fopen("../../include/uploaded/epub/file.php", "w");
// ...
我正在尝试使用 fopen 和 fwrite
$book="http://bittotb.com/synthesis_study_material/student_admin/include/uploaded/epub/923960_hotel (1).epub";
$path=$book.".php";
$myfile =fopen($path, "w");
$txt = "<?php include('hello.php'); ?>";
fwrite($myfile, $txt);
fclose($myfile);
当我只在fopen中写文件名时
$myfile =fopen("abc.php", "w");
然后它在同一目录中创建一个文件,但我想在另一个目录中创建该文件。在使用路径时如果我回显 $path 则它不起作用然后我得到
http://bittotb.com/synthesis_study_material/student_admin/include/uploaded/epub/923960_hotel (1).epub.php
这是正确的文件名和路径,但仍然无法打开文件,我的文件夹权限显示 0777
您必须使用服务器上的路径,而不是页面的 URL。
例如,您的页面可以有 URL http://example.org/index.php
。该文件可以位于名为 /var/www/example.org/index.php
.
使用此代码确定您的目录:
<?php
echo getcwd();
如果上面的代码显示 /var/www/example.org/
,文件 http://example.org/test.php
的文件路径为 /var/www/example.org/test.php
。但是最好使用相对路径。 (见下文)
如果您有页面 http://example.org/index.php
并且您想要创建 http://example.org/test.php
,请使用:
$file = fopen("test.php", "w");
fwrite($file, "<?php echo 'Hello World'; ?>");
fflush($file);
fclose($file);
如果要从脚本 http://bittotb.com/synthesis_study_material/student_admin/module/corses/file.php
写入文件 http://bittotb.com/synthesis_study_material/student_admin/include/uploaded/epub/file.php
,请使用相对路径:
$file = fopen("../../include/uploaded/epub/file.php", "w");
// ...