dongling4288 2016-08-11 06:25
浏览 25

创建一个继续下一个号码的目录?

I'm Trying to have this button create a directory "item1" and when the button is clicked again, it creates "item2" and so on, for unlimited amount of times.

So far I have this for 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>  

And my php code:

<?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!";
}?>

I have tried literally about 80 different combinations of for loops and while statements to try to get $number to increase by 1 if it finds an existing directory, all of them end up creating item1, then infinite looping on me.

Any help would be appreciated, I would post all the things I've tried to show you that I have indeed tried, but they are so hideous, I can't even bear to show such horrible code.

EDIT:

<?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!";
}

?>

I did this, and it does work. But I do have this awkward .txt in my webserver just counting up, lol.

If there is a better method, please share.

  • 写回答

1条回答 默认 最新

  • dongqiang2358 2016-08-11 06:51
    关注

    Please take a look into this code .This is working for me.

    -- UPDATED CODE --

    <?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> 
    

    This code works like this-

    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.
    
    评论

报告相同问题?

悬赏问题

  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP