php 在php中尚不存在的文件夹中创建文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/282093/
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-24 22:13:57  来源:igfitidea点击:

Creating a file inside a folder that doesn't exist yet in php

phpfile-iodirectory

提问by gnud

I want my php script to create an output file in a folder based on the date. The way I'm doing this is that its supposed to get the foldername/filename from a text file outputted by another program which I am unable to edit.

我希望我的 php 脚本根据日期在文件夹中创建一个输出文件。我这样做的方式是它应该从另一个我无法编辑的程序输出的文本文件中获取文件夹名/文件名。

So the file its grabbing the data from looks like this:

因此,它从中获取数据的文件如下所示:

data/newfolder/10302008/log_for_Today.txt | 24234234

Only with multiple lines, I just need the script to go through it line by line, grab the folder/filename and create an empty file with that name in that location.

只有多行,我只需要脚本逐行遍历它,获取文件夹/文件名并在该位置创建一个具有该名称的空文件。

The directories are all 777. Now I know how to create a new empty exe file in a folder but can't seem to figure out how to create the folder first then the exe inside of it, any ideas?

目录都是777。现在我知道如何在文件夹中创建一个新的空exe文件,但似乎无法弄清楚如何先创建文件夹,然后再创建其中的exe,有​​什么想法吗?

回答by gnud

if(!file_exists(dirname($file)))
    mkdir(dirname($file), 0777, true);
//do stuff with $file.

Use the third parameter to mkdir(), which makes it create directories recursively.

使用mkdir()的第三个参数,这使得它递归地创建目录。

回答by okoman

With

$directories = explode( '/', $path );

you can split the path to get single directory names. Then go through the array and create the directories setting chmod 777. (The system user, who executes php must have the ability to do that.)

您可以拆分路径以获取单个目录名称。然后遍历数组并创建目录设置 chmod 777。(执行 php 的系统用户必须有能力这样做。)

$file = array_pop( $directories );
$base = '/my/base/dir';

foreach( $directories as $dir )
{

   $path = sprintf( '%s/%s', $base, $dir )
   mkdir( $path ); 
   chmod( $path, 777 );
   $base = $path;

}

// file_put_contents or something similar
file_put_contents( sprintf( '%s/%s', $base, $file ), $data );

The problem here is that you might not set chmod from your php script.

这里的问题是您可能没有从 php 脚本中设置 chmod。

An alternative could be to use FTP. The user passes FTP login data to the script and it uses FTP functionality to manage files.

另一种方法是使用 FTP。用户将 FTP 登录数据传递给脚本,它使用 FTP 功能来管理文件。

http://www.php.net/FTP

http://www.php.net/FTP

回答by okoman

It's little too late but I found this question and I have a solution for this, here is an example code and it works good for me no matter how deep is your file. You should change directory separator for lines 1 and 3 if you're running it on Windows server.

有点太晚了,但我发现了这个问题,我有一个解决方案,这是一个示例代码,无论您的文件有多深,它都对我有用。如果您在 Windows 服务器上运行它,您应该更改第 1 行和第 3 行的目录分隔符。

$pathToFile = 'test1/test2/test3/test4/test.txt';
$fileName = basename($pathToFile);
$folders = explode('/', str_replace('/' . $fileName, '', $pathToFile));

$currentFolder = '';
foreach ($folders as $folder) {
    $currentFolder .= $folder . DIRECTORY_SEPARATOR;
    if (!file_exists($currentFolder)) {
        mkdir($currentFolder, 0755);
    }
}
file_put_contents($pathToFile, 'test');

Best regards, Georgi!

最好的问候,乔治!

回答by jishi

Create any missing folders using mkdir(), then create the empty file using touch().

使用 mkdir() 创建任何丢失的文件夹,然后使用 touch() 创建空文件。

You can use absolute paths in both cases, meaning:

您可以在这两种情况下使用绝对路径,这意味着:

mkdir('data');
mkdir('data/newfolder');
mkdir('data/newfolder/10302008');
touch('data/newfolder/10302008/log_for_Today.txt');

if you're curious about where it's starting-point it will be, you can use getcwd() to tell you the working directory.

如果您想知道它的起点在哪里,您可以使用 getcwd() 告诉您工作目录。

回答by Overbeeke

Can't you just do this by creating the dir with mkdir (http://nl.php.net/manual/en/function.mkdir.php) then chmod it 777 (http://nl.php.net/manual/en/function.chmod.php) the change directory with chdir (http://nl.php.net/manual/en/function.chdir.php) and then create the file (touch)?

你不能通过用 mkdir ( http://nl.php.net/manual/en/function.mkdir.php)创建目录然后 chmod it 777 ( http://nl.php.net/manual /en/function.chmod.php) 使用 chdir ( http://nl.php.net/manual/en/function.chdir.php)更改目录,然后创建文件 (touch)?