Jack Maessen
Jack Maessen

Reputation: 1864

how to change content of variable with POST

For a filemanagement system, i use this variable in which is the folder that i use for a scandir:

$dir = 'uploads/sfm/'.$UserID';

Users can create their own directories inside their $UserID folder.

All files are outputted via a foreach. To catch the path of a folder form "uploads", i use this:

$Dirname = dirname($dir);
$Basename = basename($dir);

Lets say i user has created a folder "test" in his own folder. I catch that path with:

$Dirname.'/'.$Basename.'/'.$file;

when i echo this i get:

uploads/sfm/5/test

Now i want to change the $dir via a POST: Below each folder which is outputted i created a form andwhen submit, the dir should change.

I do it now like this:

<form class="chdir" action="" method="post">
            <input class="hidden" name="<?php echo $Dirname.'/'.$Basename.'/'.$file; ?>" />
            <input type="submit" name="chdir" value="open dir" />
        </form>

And i try to change the dir like this:

$dir = 'uploads/sfm/'.$UserID;

if($_POST['chdir']) {
$NewDirectory = $_POST["$Dirname.'/'.$Basename.'/'.$file;"];
$dir = $NewDirectory;
//echo $dir;

}

i know the syntax is wrong but i hope you understand what i am trying to achieve: The $dir should contain now a the new value; so instead of this: $dir = 'uploads/sfm/'.$UserID';

it should now contain this:

$dir = 'uploads/sfm/'.$UserID.'/test';

What is the correct way to achieve this?

Upvotes: 1

Views: 60

Answers (1)

Saty
Saty

Reputation: 22532

As there is no class="hidden" type in html you need to assign new directory to value of hidden field and add name into it as

<form class="chdir" action="" method="post">
            <input type="hidden" name="new_directory" value="<?php echo $Dirname.'/'.$Basename.'/'.$file; ?>"/>
            <input type="submit" name="chdir" value="open dir" />
        </form>

And get new directory as

$dir = 'uploads/sfm/'.$UserID;
if($_POST['chdir']) {
$NewDirectory = $_POST['new_directory'];// get your new_directory
$dir = $NewDirectory;
//echo $dir;

}

Upvotes: 2

Related Questions