Php 我想在Laravel的/public文件夹中创建一个.html文件

Php 我想在Laravel的/public文件夹中创建一个.html文件,php,fopen,laravel-5.7,Php,Fopen,Laravel 5.7,我正在尝试创建一些HTML代码,并将其从文本区域存储到文件中 PHP中的fopen方法将创建一个文件,如果该文件不存在,但在Laravel 5.7中不起作用 这是我的演示代码 $plugin_html_file_nm = "plugin_html".time().".html"; $html_plugin = fopen("/public/plugin_html_storage/".$plugin_html_file_nm,"w"); fwrite($html_plugin,$request-&

我正在尝试创建一些HTML代码,并将其从文本区域存储到文件中

PHP中的
fopen
方法将创建一个文件,如果该文件不存在,但在Laravel 5.7中不起作用

这是我的演示代码

$plugin_html_file_nm = "plugin_html".time().".html";
$html_plugin = fopen("/public/plugin_html_storage/".$plugin_html_file_nm,"w");
fwrite($html_plugin,$request->input('plugin_html'));
$plugin->plugin_html = $plugin_html_file_nm;
此错误出现在
fopen(/public/plugin_html_storage/plugin_html1546535810.html):无法打开流:没有这样的文件或目录


有人能帮我吗?

Laravel提供了一个助手
public\u path()

public_path函数将完全限定的路径返回给 公共目录。您也可以使用public_path函数 生成公共目录中给定文件的完全限定路径 目录

也许你需要添加完整的路径。因此:

$html_plugin = fopen(public_path("plugin_html_storage/.$plugin_html_file_nm"),"w");
另外,确保你有写作的特权。正如PHP所述:

PHP必须可以访问该文件,因此需要确保文件访问权限允许此访问


Laravel提供了一个助手
public\u path()

public_path函数将完全限定的路径返回给 公共目录。您也可以使用public_path函数 生成公共目录中给定文件的完全限定路径 目录

也许你需要添加完整的路径。因此:

$html_plugin = fopen(public_path("plugin_html_storage/.$plugin_html_file_nm"),"w");
另外,确保你有写作的特权。正如PHP所述:

PHP必须可以访问该文件,因此需要确保文件访问权限允许此访问


默认情况下,您的laravel应用程序是从公共目录提供的

所以,为了实现你想要做的,替换

$html_plugin = fopen("/public/plugin_html_storage/".$plugin_html_file_nm,"w");
与:


注意:plugin\u html\u storage目录应该在public目录中创建,这样操作就不会失败。

默认情况下,您的laravel应用程序是从public目录提供的

所以,为了实现你想要做的,替换

$html_plugin = fopen("/public/plugin_html_storage/".$plugin_html_file_nm,"w");
与:


注意:plugin\u html\u storage目录应该在public目录中创建,这样操作就不会失败。

您只需要添加相对路径

$html_plugin = fopen("../public/plugin_html_storage/".$plugin_html_file_nm,"w");

您只需要添加相对路径

$html_plugin = fopen("../public/plugin_html_storage/".$plugin_html_file_nm,"w");

目录
/public/plugin\u html\u storage
是否存在?不,该文件夹不存在,则代码中的路径错误。看看答案,有一个解决方案提供给你。大多数时候,“我想让PHP写一个.html文件”表明对PHP应用程序通常是如何创建的误解。你能详细说明你为什么要这样做吗?我正在创建一个与@ceejayoz相关的项目,我不能详细说明其他任何事情…..目录
/public/plugin\u html\u存储是否存在?不,该文件夹不存在,那么你的代码中有一个错误的路径。看看答案,有一个解决方案提供给你。大多数时候,“我想让PHP写一个.html文件”表明对PHP应用程序通常是如何创建的误解。你能详细说明你为什么要这样做吗?我正在创建一个与@ceejayoz相关的项目,我不能详细说明其他任何事情。。。。。