开发者

Replace a string content with other content PHP

开发者 https://www.devze.com 2023-03-25 07:53 出处:网络
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:

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.开发者_如何学Python"/";
        $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;

}

?>


To add a string just do that:

$dirname .= "/testdir/";


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


Is this what you want:

$dirname .= "testdir/";


Finally used a session variable for storing the actual dir

0

精彩评论

暂无评论...
验证码 换一张
取 消