开发者

Do While loop - generating a filename and checking if the file exists

I need to be able to do a while loop that generates a filename and checks if it does not exist in the directory then should continue with the script, e.g.

do {
      $filename = random(18) . $fileEx;
} while(!file_exists($filename));

$move_file = move_uploaded_file($request_file['tmp_na开发者_JAVA百科me'][$i], 'files/' . $filename);

if($move_file)
{
   // ...

But when I do this, it just goes into an infinite loop - no errors are thrown using E_ALL.


You should run your loop as long as the file exists, not as long as it not exists (which will be forever if you have a unique file name). Change it into

} while(file_exists($filename));


This is the wrong way to do it. You should use a function that ensures you create files in an atomic way.

php has tempnam

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜