PHP的FTP学习(四) 

By Vikram Vaswani
Melonfire
November 07, 2000
以下是代码列表:
--------------------------------------------------------------------------------

 1<html>
 2<head>
 3<basefont face="arial"/>
 4</head>
 5<body>
 6<table align="center" border="0">
 7<form action="actions.php" method="post">
 8<input name="action" type="hidden" value="CWD"/>
 9<tr>
10<td>   
11Server   
12</td>
13<td>
14<input name="server" type="text"/>
15</td>
16</tr>
17<tr>
18<td>   
19User   
20</td>
21<td>
22<input name="username" type="text"/>
23</td>
24</tr>
25<tr>
26<td>   
27Password   
28</td>
29<td>
30<input name="password" type="password"/>
31</td>
32</tr>
33<tr>
34<td align="center" colspan="2">
35<input type="submit" value="Beam Me Up, Scotty!"/>
36</td>
37</tr>
38</form>
39</table>
40</body>
41</html>

--------------------------------------------------------------------------------
--------------------------------------------------------------------------------

  1<html>
  2<head>
  3<basefont face="Arial"/>
  4</head>
  5<body>
  6<?   
  7/*   
  8\--------------------------------------------------------------------------------   
  9DISCLAIMER:   
 10  
 11This is use-at-your-own-risk code.   
 12  
 13It is meant only for illustrative purposes and is not meant for production environments. No warranties of any kind are provided to the user.   
 14  
 15You have been warned!   
 16  
 17All code copyright Melonfire, 2000. Visit us at http://www.melonfire.com   
 18\--------------------------------------------------------------------------------   
 19*/   
 20  
 21// function to connect to FTP server   
 22function connect()   
 23{   
 24global $server, $username, $password;   
 25$conn = ftp_connect($server);   
 26ftp_login($conn, $username, $password);   
 27return $conn;   
 28}   
 29  
 30  
 31// main program begins   
 32  
 33// check for valid form entries else print error   
 34if (!$server || !$username || !$password)   
 35{   
 36echo "Form data incomplete!";   
 37}   
 38else   
 39{   
 40  
 41  
 42// connect   
 43$result = connect();   
 44  
 45// action: change directory   
 46if ($action == "CWD")   
 47{   
 48  
 49// at initial stage $rdir does not exist   
 50// so assume default directory   
 51if (!$rdir)   
 52{   
 53$path = ".";   
 54}   
 55// get current location $cdir and add it to requested directory $rdir   
 56else   
 57{   
 58$path = $cdir . "/" . $rdir;   
 59}   
 60  
 61// change to requested directory   
 62ftp_chdir($result, $path);   
 63  
 64}   
 65  
 66// action: delete file(s)   
 67else if ($action == "Delete")   
 68{   
 69  
 70ftp_chdir($result, $cdir);   
 71  
 72// loop through selected files and delete   
 73for ($x=0; $x<sizeof($dfile); $x++)   
 74{   
 75ftp_delete($result, $cdir . "/" . $dfile[$x]);   
 76}   
 77  
 78}   
 79// action: download files   
 80else if ($action == "Download")   
 81{   
 82  
 83ftp_chdir($result, $cdir);   
 84  
 85// download selected files   
 86// IMPORTANT: you should specify a different download location here!!   
 87for ($x=0; $x<sizeof($dfile); $x++)   
 88{   
 89ftp_get($result, $dfile[$x], $dfile[$x], FTP_BINARY);   
 90}   
 91  
 92}   
 93// action: upload file   
 94else if ($action == "Upload")   
 95{   
 96  
 97ftp_chdir($result, $cdir);   
 98  
 99// put file   
100  
101/*   
102a better idea would be to use   
103$res_code = ftp_put($result, $HTTP_POST_FILES["upfile"]["name"],   
104$HTTP_POST_FILES["upfile"]["tmp_name"], FTP_BINARY);   
105as it offers greater security   
106*/   
107$res_code = ftp_put($result, $upfile_name, $upfile, FTP_BINARY);   
108  
109  
110// check status and display   
111if ($res_code == 1)   
112{   
113$status = "Upload successful!";   
114}   
115else   
116{   
117$status = "Upload error!";   
118}   
119  
120}   
121  
122// create file list   
123$filelist = ftp_nlist($result, ".");   
124  
125// and display interface   
126include("include.php");   
127  
128// close connection   
129ftp_quit($result);   
130  
131}   
132?>
133</body>
134</html>

--------------------------------------------------------------------------------
--------------------------------------------------------------------------------

1<center>   
2You are currently working in <b><? echo $here; ?></b>
3<br/>
4<!-- status message for upload function -->
5<? echo $status; ?>
6</center>
1<hr/>
 1<p>
 2<!-- directory listing in drop-down list -->   
 3Available directories:   
 4<form action="actions.php" method="post">
 5<!-- these values are passed hidden every time -->
 6<!-- a more optimal solution might be to place these in session   
 7variables -->
 8<input $username;="" ?="" echo="" name="username" type="hidden" value="&lt;?"/>&gt;   
 9<input $password;="" ?="" echo="" name="password" type="hidden" value="&lt;?"/>&gt;   
10<input $server;="" ?="" echo="" name="server" type="hidden" value="&lt;?"/>&gt;   
11<input $here;="" ?="" echo="" name="cdir" type="hidden" value="&lt;?"/>&gt;   
12  
13<!-- action to take when THIS form is submitted -->
14<input name="action" type="hidden" value="CWD"/>
15<!-- dir listing begins; first item is for parent dir -->
16<select name="rdir">
17<option value=".."><parent directory=""></parent></option>
18<?   
19  
20for ($x=0; $x<sizeof($dir_list); $x++)   
21{   
22echo "<option value=" . $dir_list[$x] . ">" . $dir_list[$x] . "";   
23  
24}   
25?&gt;   
26  
27</select>
28<input type="submit" value="Go"/>
29</form>
30<hr/>
31<!-- file listing begins -->   
32  
33Available files:   
34<form action="actions.php" method="post">
35<!-- these values are passed hidden every time -->
36<input $server;="" ?="" echo="" name="server" type="hidden" value="&lt;?"/>&gt;   
37<input $username;="" ?="" echo="" name="username" type="hidden" value="&lt;?"/>&gt;   
38<input $password;="" ?="" echo="" name="password" type="hidden" value="&lt;?"/>&gt;   
39<input $here;="" ?="" echo="" name="cdir" type="hidden" value="&lt;?"/>&gt;   
40  
41<table border="0" width="100%">
42<?   
43  
44// display file listing with checkboxes and sizes   
45for ($y=0; $y<sizeof($files); $y++)   
46{   
47echo "<tr><td><input name="dfile[]" type="checkbox" value=" . $files[$y] .   
48"/>". $files[$y] . " <i>(" . $file_sizes[$y] . " bytes)</i><td>";   
49}   
50  
51?&gt;   
52</td></td></table>
53<!-- actions for this form -->
54<center>
55<input name="action" type="submit" value="Delete"/>
56<input name="action" type="submit" value="Download"/>
57</center>
58</form>
59<p>
60<hr/>
61<!-- file upload form -->   
62File upload:   
63<form action="actions.php" enctype="multipart/form-data" method="post">
64<!-- these values are passed hidden every time -->
65<input $username;="" ?="" echo="" name="username" type="hidden" value="&lt;?"/>&gt;   
66<input $password;="" ?="" echo="" name="password" type="hidden" value="&lt;?"/>&gt;   
67<input $server;="" ?="" echo="" name="server" type="hidden" value="&lt;?"/>&gt;   
68<input $here;="" ?="" echo="" name="cdir" type="hidden" value="&lt;?"/>&gt;   
69  
70<table>
71<tr>
72<td>
73<!-- file selection box -->
74<input name="upfile" type="file"/>
75</td>
76</tr>
77<tr>
78<td>
79<!-- action for this form -->
80<input name="action" type="submit" value="Upload"/>
81</td>
82</tr>
83</table>
84</form>
85<!-- code for include.php ends here --></p></p>
Published At
Categories with Web编程
Tagged with
comments powered by Disqus