在其他文件夹中用 PHP 创建文件

发布于 2024-09-09 05:39:14 字数 404 浏览 0 评论 0原文

$ourFileName = "testFile.txt";
$ourFileHandle = fopen($ourFileName, 'w') or die("can't open file");
fclose($ourFileHandle);

文件“testFile.txt”应该创建在该 PHP 代码所在的同一目录中。现在有什么方法可以在其他文件夹中创建该文件。我尝试过这样的事情 -

$ourFileName = "http://abc.com/folder1/folder2/testFile.txt";
    $ourFileHandle = fopen($ourFileName, 'w') or die("can't open file");

但它没有用。

$ourFileName = "testFile.txt";
$ourFileHandle = fopen($ourFileName, 'w') or die("can't open file");
fclose($ourFileHandle);

The file "testFile.txt" should be created in the same directory where this PHP code resides.Now is there any method to create this file in some other folder. I tried something like this -

$ourFileName = "http://abc.com/folder1/folder2/testFile.txt";
    $ourFileHandle = fopen($ourFileName, 'w') or die("can't open file");

But it did not work.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

深海蓝天 2024-09-16 05:39:14

现在有什么方法可以在其他文件夹中创建此文件。

当然。您只需指定文件系统路径,而不是 URL。

$ourFileName = "/path/to/myfile/test.txt";
$ourFileName = "../myfile/test.txt";

Now is there any method to create this file in some other folder.

Sure. You just need to specify a filesystem path, not a URL.

$ourFileName = "/path/to/myfile/test.txt";
$ourFileName = "../myfile/test.txt";
ゞ记忆︶ㄣ 2024-09-16 05:39:14

testfile.txt 将放置在同一文件夹中。

就像./testfile.txt一样。如果您想使用相对路径,您可以执行以下操作:

./../../testfile.txt,这将在目录树中向上两级。

您还可以使用绝对路径来选择目录:/root/somefolder/inner/file.txt

testfile.txt will be placed in the same folder.

Just as ./testfile.txt. If you want to use relative path you can do something like:

./../../testfile.txt which will be 2 levels up in the directory tree.

You can also use absolute path in order to select the directory: /root/somefolder/inner/file.txt

够运 2024-09-16 05:39:14

使用 .. 上升一级,

例如:
$ourFileName = "../folder1/testFile.txt";

use .. to go up one level

for example:
$ourFileName = "../folder1/testFile.txt";

韬韬不绝 2024-09-16 05:39:14

不要使用 URL,而使用本地路径。就像 $ourFileName = 'folder1/folder2/testFile.txt';

理解这一点 - 当 PHP 代码在服务器上执行时,它与在服务器本地执行的任何其他程序没有什么不同。这意味着它有一些运行的进程,该进程有执行时的凭据、命令行等。还有当前路径,由 PHP 设置为与所请求的文件相同。浏览器。

Don't use an URL, use a local path. Like $ourFileName = 'folder1/folder2/testFile.txt';

Understand this - when the PHP code is executed on the server it's no different than any other program which is executed locally on the server. That means it has some process under which it runs, that process has credentials with which it is executing, command line, etc. And also the current path, which is set by PHP to be the same as the file which is being requested by the browser.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文