5 if (!array_key_exists('path', $_SESSION) || !is_array($_SESSION['path'])) {
6 $_SESSION['path'] = array();
9 if (!$GLOBALS["conn_id"]) {
13 if (isset($_POST) && array_key_exists("host", $_POST)) {
14 $_SESSION['path'] = ["/"];
15 $_SESSION["host"] = $_POST["host"];
16 $_SESSION["name"] = $_POST["name"];
17 $_SESSION["password"] = $_POST["password"];
24 if (array_key_exists("fileToUpload", $_FILES)) {
25 $target_file = basename($_FILES["fileToUpload"]["name"]);
26 $temp_name = $_FILES["fileToUpload"]["tmp_name"];
27 uploadFile($temp_name, $target_file);
30 if (array_key_exists("directory", $_GET)) {
32 getFileTable($_GET["directory"]);
37 if (array_key_exists("file", $_GET)) {
38 $file = $_GET["file"];
40 if (!ftp_chdir($GLOBALS["conn_id"], $file)) {
41 if ($_GET["action"] == "get") {
44 if ($_GET["action"] == "delete") {
46 getFileTable($_SESSION["path"][count($_SESSION["path"]) - 1]);
49 array_push($_SESSION["path"], $file);
50 getFileTable($_SESSION["path"][count($_SESSION["path"]) - 1]);
56 if (array_key_exists("up", $_GET)) {
59 getFileTable($_SESSION["path"][count($_SESSION["path"]) - 1]);
65 if (isset($_DELETE) && array_key_exists("file", $_DELETE)) {
66 $file = $_DELETE["file"];
68 if (deleteFile($file)) {
69 getFileTable($_SESSION["path"][count($_SESSION["path"]) - 1]);
77 $GLOBALS["conn_id"] = ftp_connect($_SESSION["host"]);
78 // login with username and password
79 $login_result = ftp_login($GLOBALS["conn_id"], $_SESSION["name"], $_SESSION["password"]);
81 if ((!$GLOBALS["conn_id"]) || (!$login_result)) {
82 echo "FTP connection has failed!";
85 for ($x = 0; $x < count($_SESSION["path"]); $x++) {
86 ftp_chdir($GLOBALS["conn_id"], $_SESSION["path"][$x]);
93 if (count($_SESSION["path"]) > 1) {
94 array_pop($_SESSION["path"]);
100 ftp_close($GLOBALS["conn_id"]);
103 function getFileTable($directory)
106 echo "Current Path: " . ftp_pwd($GLOBALS["conn_id"]) . " ";
109 ftp_nlist($GLOBALS["conn_id"], $directory);
113 echo "<tr><th>Name</th><th colspan=\"2\">Action</th></tr>";
115 //upload file / go up row
117 echo "<form action=\"\" method=\"post\" enctype=\"multipart/form-data\">";
119 echo "<input type=\"file\" name=\"fileToUpload\" id=\"fileToUpload\">";
122 echo "<input type=\"submit\" value=\"UPLOAD\" name=\"submit\">";
125 echo "<form method=\"get\" action=\"\">";
127 echo "<input type=\"submit\" name=\"submit\"value=\"GO UP\">";
128 echo "<input type=\"hidden\" name=\"up\" value=\"up\">";
135 foreach ($contents as $file) {
138 echo "<td>$file</td>";
140 echo "<form method=\"get\" action=\"\">";
141 echo "<input type=\"submit\" name=\"submit\" value=\"GET\">";
142 echo "<input type=\"hidden\" name=\"file\" value=\"$file\">";
143 echo "<input type=\"hidden\" name=\"action\" value=\"get\">";
147 echo "<form method=\"get\" action=\"\">";
148 echo "<input type=\"submit\" name=\"submit\" value=\"DELETE\">";
149 echo "<input type=\"hidden\" name=\"file\" value=\"$file\">";
150 echo "<input type=\"hidden\" name=\"action\" value=\"delete\">";
158 function downloadFile($file)
160 if (ftp_get($GLOBALS["conn_id"], basename($file), basename($file), FTP_BINARY)) {
161 if (basename($file)) {
162 header('Content-Description: File Transfer');
163 header('Content-Type: application/octet-stream');
164 header('Content-Disposition: attachment; filename="' . basename(substr(basename($file), 1)) . '"');
165 header('Expires: 0');
166 header('Cache-Control: must-revalidate');
167 header('Pragma: public');
168 header('Content-Length: ' . filesize(substr(basename($file), 1)));
169 readfile(substr(basename($file), 1));
170 unlink(basename($file));
173 echo "There was a problem\n";
177 function uploadFile($temp_name, $target_file)
180 if (move_uploaded_file($temp_name, $target_file)) {
181 if (ftp_put($GLOBALS["conn_id"], $target_file, $target_file, FTP_BINARY)) {
182 unlink(basename($target_file));
183 getFileTable($_SESSION["path"][count($_SESSION["path"]) - 1]);
185 echo "There was a problem while uploading $target_file\n";
190 echo "Sorry, there was an error uploading your file.";
194 function deleteFile($file)
196 if (ftp_delete($GLOBALS["conn_id"], $file)) {
199 echo "File not deleted!";
202 function getLoginForm()
204 echo "<form method=\"post\" action=\"\">";
205 echo "Host: <input type=\"text\" name=\"host\" required value=\"\">";
207 echo "Name: <input type=\"text\" name=\"name\" required value=\"\">";
209 echo "Password: <input type=\"text\" name=\"password\" required value=\"\">";
211 echo "<input type=\"submit\" name=\"submit\" value=\"LOGIN\">";