Reputation: 762
So, I have a .txt file with paths of my missing files. I want to write a little php script, that will just create those files and leave them blank.
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. 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.
Upvotes: 1
Views: 1383
Reputation: 2995
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);
}
?>
Upvotes: 1
Reputation: 1812
I am assuming that the paths in your .txt file are absolute paths. If not than you will have to append some sort of ROOT_DIRECTORY
constant in front of the paths.
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);
}
Upvotes: 1
Reputation: 749
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...
Upvotes: 1
Reputation: 2165
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.
The mkdir($directory, 0755, true);
will create the required directory recursively and the touch
will create a blank file.
Upvotes: 1
Reputation: 706
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;
}
Upvotes: -1