【问题标题】:Failed to open stream: No such file of directory in... PHP fopen无法打开流:没有这样的目录文件... PHP fopen
【发布时间】:2014-06-15 18:40:39
【问题描述】:

这是我的代码

include 'connection.php';
setcookie('username',$name,time()+86400,"/");
echo $_COOKIE['username'];
$my_file = '/../../users/'.$name."/index.php";
$handle = fopen($my_file, 'w') or die('Cannot open file:  '.$my_file);

当代码运行时,我希望它创建一个目录为/../../users/[username] 的文件,其中[username] 存储在$name 变量中。当我运行我的代码时,它只会给我一个糟糕的错误,并且在上面的第 6 行之后它不会运行任何代码。

它不会创建文件,错误是......

警告:fopen(/../../users/codemaster7/index.php) [function.fopen]:无法打开流:/nfs/c02/h10/mnt/45563/ 中没有这样的文件或目录domain/pizzascripters.com/html/projects/nik/Champion/register/index.php 在第 107 行 无法打开文件:/../../users/codemaster7/index.php

有没有人知道发生了什么以及如何解决它?

【问题讨论】:

    标签: php file cookies directory fopen


    【解决方案1】:
    1 include 'connection.php';
    2 setcookie('username',$name,time()+86400,"/");
    3 echo $_COOKIE['username'];
    4 $my_file = 'index.php';
    5 $handle = fopen($my_file, 'w') or die('Cannot open file: '.$my_file);
    

    我在第 4 行将文件目录更新为一个文件名。如果你想使用目录作为参数,那么使用函数 file($myfile);从您的文件中返回数据数组。

    【讨论】:

    • 谢谢!我更改了代码,使其仅创建文件而不是目录。代码效果很好!
    【解决方案2】:

    fopen 只能在指定路径创建文件,但不能创建任何目录。在调用 fopen 之前,您必须使用 mkdir 之类的东西来创建一个新目录。

    【讨论】:

      【解决方案3】:

      当您错过或拼错类似here I did it first之类的内容时,通常会发生此错误

      $path = Yii::getAlias('@common') .'/upload/images/'. $model
      

      这里的拼写是 upload 但它应该是 uploads,因为在我的项目中它是 上传。错误的咒语给了我这个错误。所以这经常发生在路径未知的时候

      $path = Yii::getAlias('@common') .'/uploads/images/'. $model
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2016-06-05
        • 2015-09-07
        • 2019-09-30
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-11-13
        • 2016-08-03
        相关资源
        最近更新 更多