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.