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

发布于 2024-07-09 05:54:36 字数 326 浏览 8 评论 0原文

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

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

data/newfolder/10302008/log_for_Today.txt | 24234234

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

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

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.

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?

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

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

发布评论

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

评论(5

白况 2024-07-16 05:54:36
if(!file_exists(dirname($file)))
    mkdir(dirname($file), 0777, true);
//do stuff with $file.

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

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.

抚你发端 2024-07-16 05:54:36

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

可以分割路径以获得单个目录名称。 然后遍历数组并创建设置 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 );

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

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

http://www.php.net/FTP

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

$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.

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

http://www.php.net/FTP

剩一世无双 2024-07-16 05:54:36

有点太晚了,但我发现了这个问题,我有一个解决方案,这里是一个示例代码,无论你的文件有多深,它对我来说都很有效。 如果您在 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');

最好的问候,乔治!

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.

$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!

云淡风轻 2024-07-16 05:54:36

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

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

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

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

Create any missing folders using mkdir(), then create the empty file using 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.

池予 2024-07-16 05:54:36

难道你不能通过使用 mkdir 创建目录来做到这一点吗(http:// nl.php.net/manual/en/function.mkdir.php) 然后 chmod 它 777 (http://nl.php.net/manual/en/function.chmod.php) 使用 chdir 更改目录 (http://nl.php.net/manual/en/function.chdir.php),然后创建文件(触摸)?

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

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