【发布时间】:2014-09-09 06:06:57
【问题描述】:
我想用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;
}
}
?>
在我的 index.php 上,我通过 jQuery 解析代码:
function openFile(path) {
$.get("lib/includes/ajaxActions.php?action=openFile&path=" + path, function(data) {
console.log(data);
$('#editor').html(data);
$('#sourceCode').modal('show')
});
}
console.log() 返回正确的代码,但在 div#editor 上没有语法,所有的中断都消失了。
输出: console.log():
<?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);
}
div#editor:
<?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; } }
【问题讨论】:
-
使用
nl2br在所有换行符之前添加<br>标记。 -
echo nl2br(htmlentities($result));php.net/manual/en/function.nl2br.php