如何在php中查看php源代码?

如何在php中查看php源代码?,php,jquery,viewer,Php,Jquery,Viewer,我想用php输出一些php代码,但似乎无法获得正确的格式 我查看代码的功能如下: public function openFile($path, $save = false) { if($save == true) $filename = basename($path); else $filename = "tmp/".substr(hash("sha256", time()), 8, 25).".tmp";//Datei speichern #$fp = fopen($

我想用php输出一些php代码,但似乎无法获得正确的格式

我查看代码的功能如下:

public function openFile($path, $save = false)
    {
    if($save == true) $filename = basename($path); else $filename = "tmp/".substr(hash("sha256", time()), 8, 25).".tmp";//Datei speichern
    #$fp = fopen($filename, "wr");
    $url = "ftp://".$this->username.":".$this->password."@".$this->host.":".$this->port.$path;
    $handle = curl_init();
    curl_setopt($handle, CURLOPT_URL, $url);
    curl_setopt($handle, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($handle, CURLOPT_UPLOAD, false);
    curl_setopt($handle, CURLOPT_HEADER, true);
    #curl_setopt($handle, CURLOPT_HTTPHEADER, array("Content-Type: text/plain;"));
    #curl_setopt($handle, CURLOPT_FILE, $fp);
    $result = curl_exec($handle);
    $info = curl_getinfo ($handle);
    curl_close($handle);
    header("Content-Type: text/plain;");
    echo htmlentities($result);
    return [$result, $info];
    }
我有一个ajaxActions.php,处理所有操作:

<?php
require_once('../classes/ftp.class.php');
$webftp = new FTP("127.0.0.1", 21, "root", "123", false, true);


if(!empty($_REQUEST["action"]) && !empty($_REQUEST["path"]))
    {
    switch ($_REQUEST["action"])
        {
        case "getDirs":
            $dirs = $webftp->listDirectory($_REQUEST["path"]);
            echo "<tr data-type=\"dir\">";
            echo "<td></td>";
            echo "<td><a onClick=\"dirUp();\" href=\"#\">..</a></td>";
            echo "<td>".$webftp->getSize("..")."</td>";
            echo "<td></td>";
            echo "<td></td>";
            echo "</tr>";
            foreach ($dirs as $dir)
                {
                // Überprüfen ob der Pfad ein Verzeichnis ist, falls ja, kann man es betreten ansonsten wird die Datei angezeigt
                if($webftp->ftpIsDir($dir)==true) $data = "folder-close"; else $data = "file";

                $aTag = "<a onClick=\"reloadTable('".$dir."');\" href=\"#\">".$dir."</a>";
                if($data == "file") $aTag = "<a onClick=\"openFile('".$dir."');\" href=\"#\">".$dir."</a>";
                echo "<tr data-type=\"".$data."\">";
                echo "<td><i class=\"glyphicon glyphicon-".$data."\"></i></td>";
                echo "<td>".$aTag."</td>";
                echo "<td>".$webftp->getSize($dir)."</td>";
                echo "<td>".($data == "file" ? '<i class="glyphicon glyphicon-eye-open"></i>' : '')."<i class=\"glyphicon glyphicon-pencil\"></i><i class=\"glyphicon glyphicon-floppy-save\"></i><i class=\"glyphicon glyphicon-remove\"></i></td>";
                echo "<td></td>";
                echo "</tr>";
                }
            break;

        case "dirUp":
            $webftp->dirUp();
            break;

        case "openFile":
            $webftp->openFile($_REQUEST["path"]);
            break;

        default:
            # code...
            break;
        }
    }
?>
log返回正确的代码,但是在diveditor上没有语法,所有的中断都消失了

输出: console.log:

&lt;?php

    class FTP  {
    private $host = &quot;&quot;;
    private $port = &quot;&quot;;
    private $username = &quot;&quot;;
    private $password = &quot;&quot;;
    private $passive = false;
    private $connection = false;
    private $ssl = false;
    private $dir = false;


    public function __construct($host, $port = 21, $username, $password, $ssl = false, $passive = false)
        {
        if ( !extension_loaded('ftp') )
            {
            throw new Exception('FTP extension is not loaded!');
            }
        $this-&gt;host = $host;
        $this-&gt;port = $port;
        $this-&gt;username = $username;
        $this-&gt;password = $password;
        $this-&gt;ssl = $ssl;
        $this-&gt;passive = $passive;

        $this-&gt;connect($this-&gt;host, $this-&gt;port, $this-&gt;ssl, 90);
        $this-&gt;login($this-&gt;username, $this-&gt;password);
        }
编辑:

<?php class FTP { private $host = ""; private $port = ""; private $username = ""; private $password = ""; private $passive = false; private $connection = false; private $ssl = false; private $dir = false; public function __construct($host, $port = 21, $username, $password, $ssl = false, $passive = false) { if ( !extension_loaded('ftp') ) { throw new Exception('FTP extension is not loaded!'); } $this->host = $host; $this->port = $port; $this->username = $username; $this->password = $password; $this->ssl = $ssl; $this->passive = $passive; $this->connect($this->host, $this->port, $this->ssl, 90); $this->login($this->username, $this->password); } public function connect($host ,$port = 21, $ssl = false, $timeout = 90) { if ($ssl) { $this->connection = ftp_ssl_connect($host, $port, $timeout); } else { $this->connection = ftp_connect($host, $port, $timeout); } if ($this->connection == null) { throw new Exception('Unable to connect'); } else { return $this; } }
更改:

echo htmlentities($result);
致:


nl2br在所有换行符之前添加标记。并且添加应该保持代码的格式。虽然我不确定您是否可以正确地依赖于选项卡对齐-浏览器的选项卡停止可能与IDE不同。

要渲染的输出字符串必须正确转义。这意味着用它们的html共同响应者替换所有您的和可能的其他字符。 长话短说,不要多次使用echo,而是将所有文本分配给$var并执行echo htmlentities$var;。对于PHP标记,我认为您必须手动将其替换为URL编码的值。 我希望它至少有一些帮助。

我找到了解决方案: 我的javascript代码如下所示:

var editor = ace.edit("editor");
editor.setTheme("ace/theme/monokai");
editor.getSession().setMode("ace/mode/php");
editor.setOptions({
    maxLines: 50
});

function openFile(path) {
    $.get("lib/includes/ajaxActions.php?action=openFile&path=" + path, function(data) {
        editor.setValue(data);
        $('#sourceCode').modal('show')
    });
}
我的phpcode:

public function openFile($path, $save = false)
        {
        if($save == true) $filename = basename($path); else $filename = "tmp/".substr(hash("sha256", time()), 8, 25).".tmp";//Datei speichern
        #$fp = fopen($filename, "wr");
        $url = "ftp://".$this->username.":".$this->password."@".$this->host.":".$this->port.$path;
        $handle = curl_init();
        curl_setopt($handle, CURLOPT_URL, $url);
        curl_setopt($handle, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($handle, CURLOPT_UPLOAD, false);
        curl_setopt($handle, CURLOPT_HEADER, true);
        #curl_setopt($handle, CURLOPT_HTTPHEADER, array("Content-Type: text/plain;"));
        #curl_setopt($handle, CURLOPT_FILE, $fp);
        $result = curl_exec($handle);
        $info = curl_getinfo ($handle);
        curl_close($handle);
        #header("Content-Type: text/plain;");
        echo $result;
        return [$result, $info];
        }

使用nl2br在所有换行符之前添加标记。echo nl2brhtmlentities$result;为什么不使用或浏览器呈现到他正在使用echo htmlentities$result来实现这一点。但是选项卡是什么呢?所有标签都呈现为一个空字符,但不知道ace是什么。我无法使用$'editor'.htmldata;
var editor = ace.edit("editor");
editor.setTheme("ace/theme/monokai");
editor.getSession().setMode("ace/mode/php");
editor.setOptions({
    maxLines: 50
});

function openFile(path) {
    $.get("lib/includes/ajaxActions.php?action=openFile&path=" + path, function(data) {
        editor.setValue(data);
        $('#sourceCode').modal('show')
    });
}
public function openFile($path, $save = false)
        {
        if($save == true) $filename = basename($path); else $filename = "tmp/".substr(hash("sha256", time()), 8, 25).".tmp";//Datei speichern
        #$fp = fopen($filename, "wr");
        $url = "ftp://".$this->username.":".$this->password."@".$this->host.":".$this->port.$path;
        $handle = curl_init();
        curl_setopt($handle, CURLOPT_URL, $url);
        curl_setopt($handle, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($handle, CURLOPT_UPLOAD, false);
        curl_setopt($handle, CURLOPT_HEADER, true);
        #curl_setopt($handle, CURLOPT_HTTPHEADER, array("Content-Type: text/plain;"));
        #curl_setopt($handle, CURLOPT_FILE, $fp);
        $result = curl_exec($handle);
        $info = curl_getinfo ($handle);
        curl_close($handle);
        #header("Content-Type: text/plain;");
        echo $result;
        return [$result, $info];
        }