0
Well, I’m looking to do something that creates files with numbering, for example:
001.txt
, 002.txt
, 003.txt
[...] 200.txt
...
That is, if the 001.txt file exists, create the 002.txt and so on.
It is possible?
0
Well, I’m looking to do something that creates files with numbering, for example:
001.txt
, 002.txt
, 003.txt
[...] 200.txt
...
That is, if the 001.txt file exists, create the 002.txt and so on.
It is possible?
1
After seeing so much negativity in the other 2 comments (including mine) I decided to make a recursive function for this question.
This function can have 2 or 3 different uses and I think one of them is the answer to your question.
first use - You can check in an ex interval: [0..10]
if all files exist, if some of them do not exist, will be created.
function createFiles($manyfiles = 0, $path, $indice = 0, $count = 0){
$num = 0;
$count++;
for ($i = $indice; $i < $manyfiles; $i++) {
$namePathFile = $path . sprintf('%03d', $num) . ".txt";
// verifica se é a primeira ve que entra no loop
if($i == 0){
// verifica se o primeiro ficheiro existe
if(!file_exists($namePathFile)){
echo "Criar primeiro arquivo se não existe<br>";
$file = fopen($namePathFile, "w");
// vamos fechar o ficheiro para não ficar em memoria
fclose($file);
// chamamos novamente a função mas retirando uma posição de cada
return createFiles($manyfiles - 1, $path, $i, $count - 1);
}
}
$num ++;
//reformata o nome usando a variavel count
$namePathFile = $path . sprintf('%03d', $count) . ".txt";
// verifica se o ficheiro atual existe
if(!file_exists($namePathFile)){
echo "criar arquivo " . $namePathFile . " <br>";
$arquivo = fopen($namePathFile, "w") or die("Impossivel abrir o arquivo!");
fclose($arquivo);
//Cchama a função novamente retirando o valor que ja passou.
return createFiles($manyfiles - $num, $path, $num, $count);
}else{
return createFiles($manyfiles - $num , $path, 0, $count);
}
}
}
2nd Use - you have 3 example files:
001.txt
002.txt
004.txt
this function will add the missing file 003.txt
and a further number of files (manyFiles) less missing added files(003.txt
).
function createFiles($manyfiles = 0, $path, $indice = 0, $count = 0){
$num = 0;
$count++;
for ($i = $indice; $i < $manyfiles; $i++) {
$namePathFile = $path . sprintf('%03d', $num) . ".txt";
// verifica se é a primeira ve que entra no loop
if($i == 0){
// verifica se o primeiro ficheiro existe
if(!file_exists($namePathFile)){
echo "Criar primeiro arquivo se não existe<br>";
$file = fopen($namePathFile, "w");
// vamos fechar o ficheiro para não ficar em memoria
fclose($file);
// chamamos novamente a função mas retirando uma posição de cada
return createFiles($manyfiles - 1, $path, $i, $count - 1);
}
}
$num ++;
//reformata o nome usando a variavel count
$namePathFile = $path . sprintf('%03d', $count) . ".txt";
// verifica se o ficheiro atual existe
if(!file_exists($namePathFile)){
echo "criar arquivo " . $namePathFile . " <br>";
$arquivo = fopen($namePathFile, "w") or die("Impossivel abrir o arquivo!");
fclose($arquivo);
//Cchama a função novamente retirando o valor que ja passou.
return createFiles($manyfiles - $num, $path, $num, $count);
}else{
return createFiles($manyfiles , $path, 0, $count);
}
}
}
3rd Use - you have 3 example files:
001.txt
002.txt
004.txt
This function will create the file 003.txt
and at the next run (if all files in order exist) will add the file 005.txt
function createFiles($manyfiles = 0, $path, $indice = 0, $count = 0){
$num = 0;
$count++;
for ($i = $indice; $i <= $manyfiles; $i++) {
$namePathFile = $path . sprintf('%03d', $num) . ".txt";
// verifica se é a primeira ve que entra no loop
if($i == 0){
// verifica se o primeiro ficheiro existe
if(!file_exists($namePathFile)){
//echo "Criar primeiro arquivo se não existe<br>";
$file = fopen($namePathFile, "w");
// vamos fechar o ficheiro para não ficar em memoria
fclose($file);
return createFiles($manyfiles - 1, $path, $i, $count - 1);
}
}
//reformata o nome usando a variavel count
$namePathFile = $path . sprintf('%03d', $count ) . ".txt";
// verifica se o ficheiro atual existe
if(file_exists($namePathFile)){
$num ++;
echo "criar arquivo " . $namePathFile . " <br>";
$arquivo = fopen($namePathFile, "w") or die("Impossivel abrir o arquivo!");
fclose($arquivo);
//Cchama a função novamente retirando o valor que ja passou.
return createFiles($manyfiles - $num, $path, $num, $count);
}else{
$namePathFile = $path . sprintf('%03d', $count ) . ".txt";
$arquivo = fopen($namePathFile, "w") or die("Impossivel abrir o arquivo!");
fclose($arquivo);
return;
}
}
}
To execute one of these functions only 2 arguments, example:
createFiles(10, 'ficheiros/');
I hope I’ve helped.
It wasn’t me who negatived but, for us, it did a tremendous good in the answers, hahaha rs
Browser other questions tagged php
You are not signed in. Login or sign up in order to post.
You mean if the 001.txt file exists then create 002.txt file and so on .... or Voce wants to create multiple files in one shot?
– user60252
Yes, it is. Search by function
fopen
of PHP.– Woss
@Leocaracciolo Yes, if the file exists create the next.
– Darkton Hallysson
then you must use the function file_exists http://php.net/manual/en/function.file-exists.php
– user60252
you have already developed some code to create the file?
– user60252
@Leocaracciolo Sim.
– Darkton Hallysson
then edit your question and enter your code
– user60252