0

I have an a string declared at the top of my PHP script:

$dirname = "./rootfs/home/".$user."/";

After running the code, that string should be replace so it looks like this:

$dirname = "./rootfs/home/".$user."/testdir/";

Is there any way to replace the content? This is a dumb question for sure, but didn't found any way to do it, thanks! :D

Code I'm using: Main PHP:

<?php

//Variables
$rootfs = "rootfs/";
$user = "www";
$dirname = "./rootfs/home/".$user."/";

//Variables

//Commands on rootfs/bin/
include($rootfs."/bin/ls.php");
include($rootfs."/bin/about.php");
include($rootfs."/bin/echo.php");
include($rootfs."/bin/uname.php");
include($rootfs."/bin/apt-get.php");
include($rootfs."/bin/whoami.php");
include($rootfs."/bin/cd.php");
//Commands on rootfs/bin/

$command=$_POST['command'];

$commandexp = explode(" ", $command);

switch ($commandexp[0]) {
case "cd":
    //$dirname = "./rootfs/home/".$user."/";
    //$prompt = cd($dirname, $commandexp[1]);
    $dirname .= cd($dirname, $commandexp[1]);
    echo $dirname;
    break;
case "ls":
    switch ($commandexp[1]) {
    case "-la":
        //$dirname = "./rootfs/home/".$user."/";
        $files = ls($dirname);
        foreach($files as $value){
            $prompt .= substr(sprintf('%o', fileperms($dirname.$value)), -4)."   ".$user."   ".$value."<br>";
        }
        break;
    case "--help":
        $prompt = "Usage: ls [-la]";
        break;
    default:
    echo $dirname." ";
        $files = ls($dirname);
        foreach($files as $value){
            $prompt .= $value." ";
        }
        break;
    }
    break;
default:
    $prompt = "command not found";

}

echo $prompt;

?>

The cd.php

<?php
function cd($actualdir, $dir) {
    //echo $actualdir.$dir."<br>";
    if(file_exists($actualdir.$dir)) {
        echo $dir;
        return $dir;
    } else {
        return "no";
    }
}

?>

The ls.php

<?php
function ls ($directory) {

    // create an array to hold directory list
    $results = array();

    // create a handler for the directory
    $handler = opendir($directory);
    echo $handler;
    // open directory and walk through the filenames
    while ($file = readdir($handler)) {

        // if file isn't this directory or its parent, add it to the results
        if ($file != "." && $file != "..") {
            $results[] = $file;
        }

    }

    // tidy up: close the handler
    closedir($handler);

    // done!
    return $results;

}

?>

4 Answers 4

2

To add a string just do that:

$dirname .= "/testdir/";
Sign up to request clarification or add additional context in comments.

2 Comments

Yeah I tried, but when echoing it later, the string seems the same :/
Added main files, check it out
1

To concatenate one string to the end of another string: $dirname .= 'testdir/'
To reassign a totally new string to the variable: $dirname = 'This is the new string'
See String Operators

7 Comments

Yeah I tried, but when echoing it later, the string seems the same :/
Added main files, check it out
Does PHP reach the cd function line? what's the return value from cd?
./rootfs/home/www/testdir/ that's what cd return when writing cd testdir. But when writing lsit returns the contents of the old string always
@zad0xsis: That's because when writing ls, the PHP code doesn't change the value of $dirname
|
1

Is this what you want:

$dirname .= "testdir/";

1 Comment

Yeah I tried, but when echoing it later, the string seems the same :/
0

Finally used a session variable for storing the actual dir

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.