简体   繁体   English

如何在不同的文件夹中创建多个具有不同名称的文件? 的PHP

[英]How to create multiple files with different names and in different folders? php

So, I have a .txt file with paths of my missing files. 因此,我有一个.txt文件,其中包含我丢失的文件的路径。 I want to write a little php script, that will just create those files and leave them blank. 我想编写一个小php脚本,它将只创建那些文件并将其留空。

xxx/yyy/xyxy/a/w/r/obvestilo.pdf
xxx/yyy/xyxy/b/print.pdf
xxx/yyy/xyxy/c/speach.doc

This is an example of how I have things in my .txt file of missing files. 这是我如何在丢失的.txt文件中保存文件的示例。 I would like the script to create me those files and also folders if they don't yet exist, but I have no clue where to begin. 我希望脚本可以为我创建那些文件和文件夹(如果尚不存在),但是我不知道从哪里开始。 I was thinking to transfer that .txt file to an Array and then loop throug all array elements creating them. 我当时正在考虑将该.txt文件传输到数组,然后循环遍历所有创建它们的数组元素。

Please try this 请尝试这个

$fp = fopen('files.txt','r');
while(($buffer = fgets($fp,4096)) !== false) {
    $directory = substr($buffer,0,strrpos($buffer,'/') + 1);
    mkdir($directory, 0755, true);
    touch(trim($buffer));
}

files.txt will have your files in the format you have in your post. files.txt将以您发布时使用的格式保存文件。

The mkdir($directory, 0755, true); mkdir($directory, 0755, true); will create the required directory recursively and the touch will create a blank file. 将以递归方式创建所需的目录, touch将创建一个空白文件。

Try something like the following: 尝试如下操作:

$data = explode("\n", file_get_contents('file_list.txt'));
foreach($data as $filename) {
  if(!file_exists(trim($filename))) {
    file_put_contents(trim($filename), '');
  }
}

That will write an empty string to each file in the list that doesn't already exist so you will get empty files. 这会将空字符串写入列表中尚不存在的每个文件,因此您将获得空文件。 It won't create directories for you though, if you want to do that you'll need to do something a bit more complicated... 但是,它不会为您创建目录,如果要执行此操作,则需要执行一些更复杂的操作...

I am assuming that the paths in your .txt file are absolute paths. 我假设您的.txt文件中的路径是绝对路径。 If not than you will have to append some sort of ROOT_DIRECTORY constant in front of the paths. 如果不是,则必须在路径前面附加某种ROOT_DIRECTORY常量。

Generally you would want to put this functionality in a class whose sole responsibility is to create these empty files: 通常,您希望将此功能放在一个类中,该类的唯一责任是创建这些空文件:

class EmptyFileCreater {
    const USE_RECUSRION = true;
    const DEFAULT_ACCESS = 0777;

    public function create($path) {
        $this->ensureDirectoryExists(dirname($path));
        $this->createEmptyFile($path);            
    }

    private function ensureDirectoryExists($directory) {
        if (!is_dir($directory)) {
            mkdir($directory, self::DEFAULT_ACCESS, self::USE_RECUSRION);
        }
    }

    private function createEmptyFile($path) {
        touch($path);
    }
}

Now you can use this class to generate all the files. 现在,您可以使用此类生成所有文件。

// Retrieve the .txt file as an array of the lines in the file
$paths = file('path/to/missing_file_paths.txt');

$empty_file_creater = new EmptyFileCreater();
foreach ($paths as $path) {
    $empty_file_creater->create($path);
}

This will work for you: 这将为您工作:

<?php 
$lines = file('test.txt'); ///path to your file
foreach ($lines as $line_num => $line) 
{
    $fp = fopen($line,"wb");
    fwrite($fp,'');
    fclose($fp);
}
?>

You can use this method to create random names of specific lengths. 您可以使用此方法来创建特定长度的随机名称。

/*
     *
     * Get Random number( 5 digit )
     * @param int: 5
     * @return alphnumeric string: length(5)
     */

    public function getRandomNumber($length, $charset = 'abcdefghijkmnopqrpdctvwxyz3456789') {
        $str = '';
        $count = strlen($charset);
        while ($length--) {
            $str .= $charset[mt_rand(0, $count - 1)];
        }
        return $str;
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM