Search code examples
phpvariablesmkdir

Assign variable according to the form post


I am trying to create a file on my server with content in an other directory. I will select directory where the file should be created on form page. But I want to create a new directory if this directory is not in the list by writing it in the input field. The form.php is likte that;

<tr><td>Parent:</td><td> <select name="parent" ?>
<option value="../dekorasyon">Dekorasyon</option>
<option value="../aksesuarlar">Aksesuarlar</option>
<option value="../aydinlatmalar">Aydınlatmalar</option>
<option value="../dekorasyon-kampanlari">Kampanyalar</option>
<option value="../dekorasyon-fikirleri">Pratik Bilgiler</option>
<option value="../dekorasyon-trendleri">Trendler</option>
</select> </td></tr>
<tr><td>Child:</td><td> <select name="cat" ?>
<option value="">Alt kategori seçin...</option>
<option value="ev-dekorasyonu">Ev Dekorasyonu</option>
<option value="bahce-dekorasyonu">Bahçe Dekorasyonu</option>
<option value="ofis-dekorasyonu">Ofis Dekorasyonu</option>
<option value="cafe-bar-dekorasyonu">Cafe-Bar Dekorasyonu</option>
<option value="magaza-dekorasyonu">Mağaza Dekorasyonu</option>
<option value="otel-dekorasyonu">Otel Dekorasyonu</option>
<option value="yazlik-dekorasyonu">Yazlık Dekorasyonu</option>
</select> </td></tr>
<tr><td>New:</td><td><input type="text" name="newcat" /></td></tr>

the some part of the post.php is;

$cat = $_POST['cat'];
$newcat = $_POST['newcat']; 

mkdir($parent.'/'.$newcat, 0755);

$file1 = $parent.'/'.$cate.'/'.$file.'.html';

How I can set $cate as $cat if it is selected and as $newcat if $cat is not selected and valued nothing? In other words i want to create a file in the selected category. If I need a new category then it should first create the category and creaty the file in this new category.


Solution

  • First I would recommend you to validate the POST variables if you do not want to have a problem.

    Check $newcat != '' then create $newcat folder.

    If $newcat == '' then check $cat != ''. When it is not empty use $cat folder.

    If it is empty too, return error.