如何使用PHP代码创建文件夹?

Shi*_*lla 24 php directory file

我们可以用PHP代码创建文件夹吗?我希望每当新用户创建他的新帐户时,他的文件夹会自动创建并创建一个PHP文件.这可能吗?

Fun*_*ner 35

纯粹的基本文件夹创建

<?php mkdir("testing"); ?> <=这个,实际上创建了一个名为"testing"的文件夹.



基本文件创建

<?php
$file = fopen("test.txt","w");
echo fwrite($file,"Hello World. Testing!");
fclose($file);
?>
Run Code Online (Sandbox Code Playgroud)

使用aa+开关添加/附加到文件.



编辑:

此版本将同时创建一个文件和文件夹,然后在屏幕上显示.

<?php

// change the name below for the folder you want
$dir = "new_folder_name";

$file_to_write = 'test.txt';
$content_to_write = "The content";

if( is_dir($dir) === false )
{
    mkdir($dir);
}

$file = fopen($dir . '/' . $file_to_write,"w");

// a different way to write content into
// fwrite($file,"Hello World.");

fwrite($file, $content_to_write);

// closes the file
fclose($file);

// this will show the created file from the created folder on screen
include $dir . '/' . $file_to_write;

?>
Run Code Online (Sandbox Code Playgroud)


Sea*_*nWM 11

您可以使用mkdir()函数使用PHP创建目录.

mkdir("/path/to/my/dir", 0700);

您可以使用fopen()在该目录中使用该模式创建文件w.

fopen('myfile.txt', 'w');

w:仅供写作; 将文件指针放在文件的开头,并将文件截断为零长度.如果该文件不存在,请尝试创建它.


Din*_*ini 6

您可以轻松创建它:

$structure = './depth1/depth2/depth3/';
if (!mkdir($structure, 0, true)) {
die('Failed to create folders...');
}
Run Code Online (Sandbox Code Playgroud)