创建继续到下一个数字的目录


Creating a directory that continues to the next number?

我想让这个按钮创建一个目录"item1",当按钮再次点击时,它会创建"item2"等等,无限次

到目前为止,我有html (basic):
    <!DOCTYPE html>
<html>  
   <head>
      <title></title>
      <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
      <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.4/jquery.min.js"></script>
   </head>
   <body>
      <form id="create_item" action="createfile.php" method="POST" enctype="multipart/form-data">
         <input id="submit_button" type="submit" value="Create Item!" />
      </form>     
   </body>
</html>  

和我的php代码:

<?php
define("PATH", "/usr/www/tfgwebsite/public/misc/phptest");
$number = 1;
$_POST["dirname"] = "item" . $number;
$test = "set";
if (isset($test)) {
    $dir = $_POST['dirname'];
}
$targetfilename = PATH . '/' . $dir;
if (!is_file($dir) && !is_dir($dir)) {
    mkdir($dir);
    chmod($targetfilename, 0777);
    echo "Created " . $dir . " successfully!";
}
else
{
    echo "File already exists!";
}?>

我已经尝试了大约80种不同的for循环和while语句的组合,试图让$number在找到一个现有目录时增加1,所有这些最终都创建了item1,然后在我身上无限循环。

任何帮助将不胜感激,我会张贴所有的东西,我已经试图向你展示,我确实已经尝试过,但他们是如此丑陋,我甚至不能忍受显示这样可怕的代码。

编辑:

<?php
define("PATH", "/usr/www/tfgwebsite/public/misc/phptest");
$items = fopen("items.txt", "r") or die("Unable to open file!");
$number = fgets($items);
fclose($items);
$_POST["dirname"] = "item" . $number;
$test = "set";
if (isset($test)) {
    $dir = $_POST['dirname'];
}
$targetfilename = PATH . '/' . $dir;
if (!is_file($dir) && !is_dir($dir)) {
    mkdir($dir);
    chmod($targetfilename, 0777);
    echo "Created " . $dir . " successfully!";
    $create = fopen("items.txt", "w+") or die("Unable to open file!");
    fwrite($create, $number + 1);
    fclose($create);
}
else
{
    echo "File already exists!";
}

?>

我这样做了,它确实有效。但是我的服务器里有个很尴尬的。txt只是在计数,哈哈。

如果有更好的方法请分享。

请看一下这段代码。

——已更新代码——

<?php
if (!empty($_POST)) { //print_r($_POST);die;
    define("PATH", $_SERVER['DOCUMENT_ROOT'] . "/create_folder/");
    $files2 = array_diff(scandir($_SERVER['DOCUMENT_ROOT'] . "/create_folder/"), array('..', '.', '.svn'));
    $count = count($files2);
    $count_plus = $count + 1;
    if (!is_file(PATH . "item$count_plus") && !is_dir(PATH . "item$count_plus")) {
        mkdir(PATH . "item$count_plus");
        chmod(PATH . "item$count_plus", 0777);
        echo "Created item" . $count_plus . " successfully!";
    } else {
        echo "File already exists!";
    }
}
?>
<!DOCTYPE html>
<html>  
    <head>
        <title></title>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
        <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.4/jquery.min.js"></script>
    </head>
    <body>
        <form id="create_item" action="" method="POST" >
            <input id="submit_button" type="submit" name ="create_folder" value="Create Item!" />
        </form>     
    </body>
</html> 

这段代码是这样工作的-

It will define PATH constant for location where folders are getting created.
Then it will scan that folder
Then it will check if next folder is present in that directory OR not.
if not then it will create that folder otherwise will give an error.