【问题标题】:I want to create a .html file in /public folder of Laravel我想在 Laravel 的 /public 文件夹中创建一个 .html 文件
【发布时间】:2019-05-30 07:44:00
【问题描述】:

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

fopen 在 PHP 中的方法将创建一个文件,如果该文件不存在但它在 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): failed to open stream: No such file or directory

谁能帮帮我?

【问题讨论】:

  • 目录/public/plugin_html_storage是否存在?
  • 没有那个文件夹不存在
  • 那么您的代码中的路径错误。看看答案,有给你的解决方案。
  • 大多数时候,“我想让 PHP 编写一个 .html 文件”表示对 PHP 应用程序的典型创建方式存在误解。您能否详细说明为什么您要这样做?
  • 我正在创建一个与此 @ceejayoz 相关的项目,我无法详细说明其他任何内容.....

标签: php fopen laravel-5.7


【解决方案1】:
file_put_contents(public_path()."plugin_html".time().".html", 
$request->input('plugin_html'));

【讨论】:

    【解决方案2】:

    你只需要添加相对路径。

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

    【讨论】:

      【解决方案3】:

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

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

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

      与:

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

      注意:plugin_html_storage目录应该在public目录下创建,以免操作失败。

      【讨论】:

        【解决方案4】:

        Laravel 提供了一个帮助器public_path()

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

        也许您需要添加完整路径。所以:

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

        另外,请确保您拥有写作特权。正如 PHP documentation 所说:

        文件必须是PHP可以访问的,所以需要确保文件访问权限允许这个访问。

        【讨论】:

          猜你喜欢
          • 2022-01-11
          • 1970-01-01
          • 2022-10-15
          • 1970-01-01
          • 2014-03-19
          • 1970-01-01
          • 1970-01-01
          • 2019-02-21
          • 1970-01-01
          相关资源
          最近更新 更多