开发者

File loop in PHP

I wrote some code to create a text file just once each time I execute the php file. Its idea is to check all existing files with a specific name then create a text file with the previous name +1

For example, if there is a file called filetext0.txt, my code will create a file called filetext1.txt and so on... Please help me to find the error in my code:

<?php
for ($i=0; $i=1000; $i=$i+1)
  {
      $handle = fopen("filetext".$i.".txt","r");
      if ($handle) {
         fclose($handle);
         $s=$i+1
         $handlex = fopen("filetext".$s.".txt","w+开发者_Go百科");
         fclose($handlex);
         break
        }
  }
?>


First of all you should use file_exists in the first step.

Then, your problem are missing semi-colon ';' at the end of lines. Check the error messages on your web pages next time ;)

And finally, your code create a file each file it found, not only one.

I'll suggest this code :

$i = 0;
while(true) {
    $filename = "filetext".$i.".txt";
    if(! file_exists($filename)) {
        touch($filename);
        break;
    }
    $i++
}


You do not have to open each and ever file to check if it exists. You should use PHP's directory functions.

// the maximum number
$maxnum = 0;

$d = dir(".");
while (false !== ($entry = $d->read())) {
    if (preg_match ('/filetext([0-9]+)\.txt/', $entry, $matches)) {
        if ($matches[1] > $maxnum) {
            $maxnum = $matches[1];
        }
    }
}
$d->close();

echo ("The biggest number is: " . $maxnum);

// increment maxnum
$maxnum++;

// creating the file
touch ("filetext" . $maxnum . ".txt");


You need a ; after each statement.


$fileNames = glob('filetext*.txt');

$latestNumber = -1;
foreach($fileNames as $fileName) {
    list($fileNumber) = sscanf($fileName,'filetext%d.txt');
    $latestNumber = max($latestNumber,$fileNumber);
}
if ($fileNumber > -1) {
    $fileName = 'filetext'.($fileNumber+1).'.txt';
    touch($fileName);
}


Leaving aside the syntax errors, the algorithm you are using does not scale well. A better solution would be a searching method something like:

function find_next($stub)
{
    $increment=1000; // depending on number of files
    $offset=0;
    for ($x=0; $x<500; $x++) {
       $offset+=$increment;
       if (file_exists($stub . $offset)) {
          if ($increment<0) {
             $increment=-1*((integer)($increment/2) ? $increment/2 : 1;
          } 
       } else {
          if (file_exists($stub . ($offset-1)) {
              return $offset;
          }
          if ($increment>0) {
             $increment=-1*((integer)($increment/2) ? $increment/2 : 1; 
          }
       }
    }
    return false; // too many files!
}

(NB I'm just typing this stuff - the above may be a bit buggy).

But it'd be a lot better to store a sequence number and increment it each time you add a file.

However do beware that storing transactional data for a multi-user system using files with PHP is a very bad idea.


Yes used file_exists function to find next name for the file.

The above code missed the brace in if condition.

here code for your problem

$i = 0;
while(true) {
    $myfile = "myfile".$i.".txt";
    if(!file_exists($myfile)) {
       $fh = fopen($myfile, 'w');
       fclose($fh);
       break;
    }
    $i++;
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜