【问题标题】:Create folder using PHP [closed]使用 PHP 创建文件夹 [关闭]
【发布时间】:2013-08-15 12:45:51
【问题描述】:

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

【问题讨论】:

  • 使用mkdir 是可能的——但是你所做的在很多方面听起来都是“错误的”
  • 3) 三重威胁=mkdir - 糟糕,把 Stevie 搞混了。现在谈谈“双重麻烦”之类的。
  • 呵呵 thnx alot ...它解决了所有问题..现在告诉锄头用 PHP 代码创建 php 文件?
  • 来吧伙计,你可以谷歌这个。它不会杀了你。税收会解决这个问题。

标签: php file directory


【解决方案1】:

纯粹的基本文件夹创建

<?php mkdir("testing"); ?>



基本文件创建

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

使用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;

?>

【讨论】:

  • 好的,它创建了文件夹,现在我如何在该文件夹中创建由 PHP 代码在单个文件中创建的文件。
  • @ShikhilBhalla 没错。这些是文件和文件夹创建的纯基本示例。可以在 PHP.net 上通过我为您提供的链接查看更多示例/选项。干杯(和平
【解决方案2】:

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

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

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

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

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

【讨论】:

  • thnx 我解决了所有问题。你能告诉我如何使用 PHP 代码创建文件吗?
【解决方案3】:

您可以轻松创建它:

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

【讨论】:

    【解决方案4】:

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

    【讨论】:

    • 用example.Thnx提前告诉我整个脚本
    【解决方案5】:

    在回答如何在 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");
        }
    

    【讨论】:

      猜你喜欢
      • 2013-01-06
      • 2013-05-06
      • 2013-03-18
      • 1970-01-01
      • 2013-11-12
      • 1970-01-01
      • 1970-01-01
      • 2018-07-18
      • 1970-01-01
      相关资源
      最近更新 更多