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

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

How to create folder with PHP code?

phpfiledirectory

提问by Shikhil Bhalla

Can we create folder with PHP code? I want that whenever a new user create his new account his folder automatically creates and a PHP file also created.Is this possible?

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

回答by Funk Forty Niner

Purely basic folder creation

纯粹的基本文件夹创建

<?php mkdir("testing"); ?><= this, actually creates a folder called "testing".

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





Basic file creation

基本文件创建

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

Use the aor a+switch to add/append to file.

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





EDIT:

编辑:

This version will create a file and folder at the same time and show it on screen after.

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

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

?>

回答by SeanWM

You can create a directory with PHP using the mkdir()function.

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

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

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

You can use fopen()to create a file inside that directory with the use of the mode w.

您可以使用fopen()使用 mode 在该目录中创建一个文件w

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

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

w: Open for writing only; place the file pointer at the beginning of the file and truncate the file to zero length. If the file does not exist, attempt to create it.

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

回答by Dinesh Saini

You can create it easily:

您可以轻松创建它:

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

回答by Si8

In answer to the question in how to write to a file in PHP you can use the following as an example:

在回答如何在 PHP 中写入文件的问题时,您可以使用以下示例:

    $fp = fopen ($filename, "a"); # a = append to the file. w = write to the file (create new if doesn't exist)
    if ($fp) {
        fwrite ($fp, $text); //$text is what you are writing to the file
        fclose ($fp);
        $writeSuccess = "Yes";
        #echo ("File written");
    }
    else {
    $writeSuccess = "No";
    #echo ("File was not written");
    }

回答by Joe Z

... You can then use copy()to duplicate a PHP file, although this sounds incredibly inefficient.

... 然后您可以使用copy()复制 PHP 文件,尽管这听起来非常低效。