I'm working on a website that will act as an online SFTP client for my home machine. The solution I have so far is an index (main) php file that contains the UI of the site, and an SFTP PHP convenience class that connects with phpseclib, the SFTP connection manager.
index.php
<?php
require_once "php/Membership.php";
require_once "php/ssh.php";
require_once "php/sftp.php";
$sftp = new SFTP();
error_reporting(E_ALL); // will report any errors your code may have
ini_set("display_errors", 1);
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<!ATTLIST td fileName CDATA #IMPLIED>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>SFTP</title>
<link href="index.css" rel="stylesheet" type="text/css" />
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<script src="index.js"></script>
</head>
<body>
<h1 id="welcome">Welcome</h1>
<div id="container">
<div id="content">
<!--SFTP Files-->
<div style="height:1000px; overflow:auto;">
<?php $sftp->set_table(NULL, NULL);?>
</div>
</div>
</div>
</body>
</html>
SFTP.php
<?php
include('Net/SFTP.php');
class SFTP {
private $sftp;
function __construct() {
$this->sftp = new Net_SFTP('99.99.9999.999');
if (!$this->sftp->login('user', 'pwd')) {
exit('Login Failed');
}
echo $this->sftp->pwd() . "\r\n";
}
function set_table($table, $directory) {
if (isset($directory)) {
$this->sftp->chdir($directory);
}
echo '<table id="sftpTable" style="border:1px solid;">';
$result = $this->sftp->nlist();
foreach ($result as $row) {
if (substr($row, 0, 1) != '.') {
echo "<tr>" . "<td class='columnSelect' id=" . $row . "><form method='post' action=''>" . $row . "<input type=\"hidden\" name=\"index\" value=\"" . $row . "\" /></form></td>";
if (strpos($row,'.') !== false)
echo '<td>'. $this->parseBytes($this->sftp->_size($row)) . '</td></tr>';
}
}
echo '</table>';
}
function parseBytes($bytes) {
if ($bytes / 1074000000 >= 1) {
return $bytes / 1074000000 . 'GB';
}
if ($bytes / 1048576 >= 1) {
return $bytes / 1048576 . 'MB';
}
if ($bytes / 1024 >= 1) {
return $bytes / 1024 . 'KB';
}
return $bytes . 'B';
}
}
?>
Now the problem I'm facing is one of what appears to be circular logic. My initial thinking was to have a system that worked in the following way:
- Establish a singleton connection to SFTP
- Make queries on that singleton
- Present results in UI
I plan to show a table with clickable rows representing the different items in a directory on the server. When the user clicks on one of those rows, I want the system to return the new listing of items for that directory, and to update the UI accordingly. In order to accomplish this, I tried adding a hidden field to each table row to hold the name of the listing for that cell. When the cell is clicked, I would then need to extract the value of that hidden field and reset the table with the new directory. Then arises the issue of replacing the table onscreen and not just echoing out a new one.
Therefore, my question is:
What is the best way to store the directory pertaining to each cell in such a way that when that cell is clicked, the SFTP singleton resets the one table based on the new directory?
Do note that the above code likely has logical errors that may make little sense to a new viewer, as many different attempts have been made. Also, to be clear, I am looking for a methodological point in the right direction, not someone to write code for me.
Thanks in advance.