Update README.md
[webFTP.git/.git] / index.php
1 <?php
2 session_start();
3 ob_start();
4 $conn_id = false;
5 if (!array_key_exists('path', $_SESSION) || !is_array($_SESSION['path'])) {
6     $_SESSION['path'] = array();
7 }
8
9 if (!$GLOBALS["conn_id"]) {
10     getLoginForm();
11 }
12
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"];
18     if (startFTP()) {
19         getFileTable("/");
20         endFTP();
21     }
22 }
23
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);
28 }
29
30 if (array_key_exists("directory", $_GET)) {
31     if (startFTP()) {
32         getFileTable($_GET["directory"]);
33         endFTP();
34     }
35 }
36
37 if (array_key_exists("file", $_GET)) {
38     $file = $_GET["file"];
39     if (startFTP()) {
40         if (!ftp_chdir($GLOBALS["conn_id"], $file)) {
41             if ($_GET["action"] == "get") {
42                 downloadFile($file);
43             }
44             if ($_GET["action"] == "delete") {
45                 deleteFile($file);
46                 getFileTable($_SESSION["path"][count($_SESSION["path"]) - 1]);
47             }
48         } else {
49             array_push($_SESSION["path"], $file);
50             getFileTable($_SESSION["path"][count($_SESSION["path"]) - 1]);
51         }
52         endFTP();
53     }
54 }
55
56 if (array_key_exists("up", $_GET)) {
57     up();
58     if (startFTP()) {
59         getFileTable($_SESSION["path"][count($_SESSION["path"]) - 1]);
60     }
61     endFTP();
62 }
63
64
65 if (isset($_DELETE) && array_key_exists("file", $_DELETE)) {
66     $file = $_DELETE["file"];
67     if (startFTP()) {
68         if (deleteFile($file)) {
69             getFileTable($_SESSION["path"][count($_SESSION["path"]) - 1]);
70         }
71         endFTP();
72     }
73 }
74
75 function startFTP()
76 {
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"]);
80     // check connection
81     if ((!$GLOBALS["conn_id"]) || (!$login_result)) {
82         echo "FTP connection has failed!";
83         return false;
84     }
85     for ($x = 0; $x < count($_SESSION["path"]); $x++) {
86         ftp_chdir($GLOBALS["conn_id"], $_SESSION["path"][$x]);
87     }
88     return true;
89 }
90
91 function up()
92 {
93     if (count($_SESSION["path"]) > 1) {
94         array_pop($_SESSION["path"]);
95     }
96 }
97
98 function endFTP()
99 {
100     ftp_close($GLOBALS["conn_id"]);
101 }
102
103 function getFileTable($directory)
104 {
105     ob_clean();
106     echo "Current Path: " . ftp_pwd($GLOBALS["conn_id"]) . " ";
107
108     $contents =
109         ftp_nlist($GLOBALS["conn_id"], $directory);
110
111     //table header
112     echo "<table>";
113     echo "<tr><th>Name</th><th colspan=\"2\">Action</th></tr>";
114
115     //upload file / go up row
116     echo "<tr>";
117     echo "<form action=\"\" method=\"post\" enctype=\"multipart/form-data\">";
118     echo "<td>";
119     echo "<input type=\"file\" name=\"fileToUpload\" id=\"fileToUpload\">";
120     echo "</td>";
121     echo "<td>";
122     echo "<input type=\"submit\" value=\"UPLOAD\" name=\"submit\">";
123     echo "</td>";
124     echo "</form>";
125     echo "<form method=\"get\" action=\"\">";
126     echo "<td>";
127     echo "<input type=\"submit\" name=\"submit\"value=\"GO UP\">";
128     echo "<input type=\"hidden\" name=\"up\" value=\"up\">";
129     echo "</td>";
130     echo "</form>";
131     echo "</tr>";
132     echo "<tr></tr>";
133
134
135     foreach ($contents as $file) {
136         //file/folder row
137         echo "<tr>";
138         echo "<td>$file</td>";
139         echo "<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\">";
144         echo "</form>";
145         echo "</td>";
146         echo "<td>";
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\">";
151         echo "</form>";
152         echo "</td>";
153         echo "</tr>";
154     }
155     echo "</table>";
156 }
157
158 function downloadFile($file)
159 {
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));
171         }
172     } else {
173         echo "There was a problem\n";
174     }
175 }
176
177 function uploadFile($temp_name, $target_file)
178 {
179     if (startFTP()) {
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]);
184             } else {
185                 echo "There was a problem while uploading $target_file\n";
186             }
187         }
188         endFTP();
189     } else {
190         echo "Sorry, there was an error uploading your file.";
191     }
192 }
193
194 function deleteFile($file)
195 {
196     if (ftp_delete($GLOBALS["conn_id"], $file)) {
197         return true;
198     }
199     echo "File not deleted!";
200 }
201
202 function getLoginForm()
203 {
204     echo "<form method=\"post\" action=\"\">";
205     echo "Host: <input type=\"text\" name=\"host\" required value=\"\">";
206     echo "<br><br>";
207     echo "Name: <input type=\"text\" name=\"name\" required value=\"\">";
208     echo "<br><br>";
209     echo "Password: <input type=\"text\" name=\"password\" required value=\"\">";
210     echo "<br><br>";
211     echo "<input type=\"submit\" name=\"submit\" value=\"LOGIN\">";
212     echo "</form>";
213 }