无法打开流:在...中没有这样的目录文件... PHP fopen

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24232877/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 17:13:41  来源:igfitidea点击:

Failed to open stream: No such file of directory in... PHP fopen

phpfilecookiesdirectoryfopen

提问by Nik Olson

this is my code

这是我的代码

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);

when the code runs I want it to create a file with the directory /../../users/[username]where [username]is stored in the $namevariable. When I run my code it just gives me a lousy error and it doesn't run any code after line 6 from above.

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

It won't create the file and the error is...

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

Warning: fopen(/../../users/codemaster7/index.php) [function.fopen]: failed to open stream: No such file or directory in /nfs/c02/h10/mnt/45563/domains/pizzascripters.com/html/projects/nik/Champion/register/index.php on line 107 Cannot open file: /../../users/codemaster7/index.php

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

Does anyone have an idea on whats going on and how I can fix it?

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

采纳答案by Shyam

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);

I update the directory of file to just a file name in line 4. if you wanna use the directory as a parameter then use the function file($myfile); which return the array of data from your file.

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

回答by PowerGamer

fopen can only create a file at the specified path but it cannot create any directories. You will have to use something like mkdirto create a new directory before you call fopen.

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

回答by Bashir ahmad

This error often occurs when you miss or misspell something like here I did it first

当您遗漏或拼写错误时,经常会发生此错误,例如我先做的

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

Here the spell is uploadbut it should be uploadsas in my project it is uploads. the wrong spell gave me this error. so this often occurs when the path is unknown

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

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