web方式ftp

 1   
 2  
 3/* $Id: ftp.php,v 2.2 2002/03/12 23:21:28 pauls Exp $ */   
 4  
 5/* This software came from http://inebria.com/ */   
 6  
 7/* Copyright (c) 2000   
 8Paul Southworth. All rights reserved.   
 9  
10Redistribution and use in source and binary forms, with or without   
11modification, are permitted provided that the following conditions   
12are met:   
131\. Redistributions of source code must retain the above copyright   
14notice, this list of conditions and the following disclaimer.   
152\. Redistributions in binary form must reproduce the above copyright   
16notice, this list of conditions and the following disclaimer in the   
17documentation and/or other materials provided with the distribution.   
183\. The name of the author may not be used to endorse or promote   
19products derived from this software without specific prior   
20written permission.   
21  
22THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS   
23OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED   
24WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE   
25ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY   
26DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL   
27DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE   
28GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS   
29INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,   
30WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE   
31OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,   
32EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */   
33  
34/* Configuration Options */   
35  
36$phpftp_host="localhost";   
37$phpftp_version="2.2";   
38  
39/* Comment this out if you don't want the version footer */   
40  
41$show_version_footer=1;   
42  
43/* How large a file will you accept? You may also need to edit your   
44php.ini file and change upload_max_filesize appropriately */   
45  
46$max_file_size="1000000";   
47  
48/* The temporary directory $phpftp_tmpdir must exist and be writable   
49by your web server.   
50Hint: mkdir /var/tmp/xfers && chmod 1777 /var/tmp/xfers */   
51  
52$phpftp_tmpdir="/var/tmp/xfers";   
53  
54/* $use_mime_lookup   
55Turning this on creates a dependency upon the   
56http://www.inebria.com/mime_lookup/ MIME type lookup library.   
57Setting this variable to "1" enables it. "0" disables.   
58If you turn it on, put the mime_lookup.php file in the same directory   
59as ftp.php and uncomment the 'include("mime_lookup.php");' statement. */   
60  
61$use_mime_lookup="0";   
62/* include("mime_lookup.php"); */   
63  
64/* We enclose the top and bottom in functions because sometimes   
65we might not send them (ie, in a file-download situation) */   
66  
67function phpftp_top() {   
68global $phpftp_version;   
1<html>
2<head>
3<title>PHP FTP Client ```
4 echo $phpftp_version; 
5```</title>
6</head>
7<body bgcolor="#ffffff">   

}

function phpftp_bottom() {
global $phpftp_version;
global $show_version_footer;

1<!-- function phpftp_bottom -->   

if (isset($show_version_footer)) {

1<p><font size="-2">This is <a href="http://inebria.com/phpftp/">PHP FTP</a>   
2version ```
3 echo $phpftp_version; 
4```</font></p>   

}

1</body>
2</html>
1   
2}   
3  
4/* This is the form used for initially collecting username/passwd */   
5  
6function phpftp_login() {   
7phpftp_top();   
 1<p>
 2<form action="ftp.php" method="post">
 3<p><table border="0">
 4<tr><td>   
 5Login:   
 6</td><td>
 7<input name="phpftp_user" type="text"/>
 8</td></tr>
 9<tr><td>   
10Password:   
11</td><td>
12<input name="phpftp_passwd" type="password"/>
13</td></tr>
14<tr><td>   
15Directory:   
16</td><td>
17<input name="phpftp_dir" type="text"/>
18</td></tr>
19</table>
20</p><p>
21<input name="function" type="hidden" value="dir"/>
22<input type="submit" value="connect"/>
23</p>
24</form>
25<p>   

phpftp_bottom();
}

/* This function does not return TRUE/FALSE - it returns the value of
$ftp, the current FTP stream. */

function phpftp_connect($phpftp_user,$phpftp_passwd) {
global $phpftp_host;
$ftp = ftp_connect($phpftp_host);
if ($ftp) {
if (ftp_login($ftp,$phpftp_user,urldecode($phpftp_passwd))) {
return $ftp;
}
}
}

function phpftp_list($phpftp_user,$phpftp_passwd,$phpftp_dir) {
global $phpftp_host;
phpftp_top();

1<!-- function phpftp_list -->   

$ftp = @phpftp_connect($phpftp_user,$phpftp_passwd);
if (!$ftp) {

1<strong>FTP login failed!</strong>
2<a href="ftp.php">Start over?</a>   

phpftp_bottom();
} else {
if (!$phpftp_dir) {
$phpftp_dir=ftp_pwd($ftp);
}
if (!@ftp_chdir($ftp,$phpftp_dir)) {

1<font color="#ff0000"><strong>Can't enter that directory!</strong></font><p><p>   

$phpftp_dir=ftp_pwd($ftp);
}
echo "<strong>Current host:</strong> " . $phpftp_host . "<br/>\n";
echo "<strong>Current directory:</strong> " . $phpftp_dir . "<br/>\n";
if ($phpftp_dir == "/") {
$phpftp_dir="";
}

if ($contents = ftp_rawlist($ftp,"")) {
$d_i=0;
$f_i=0;
$l_i=0;
$i=0;
while ($contents[$i]) {
$item[] = split("[ ]+",$contents[$i],9);
$item_type=substr($item[$i][0],0,1);
if ($item_type == "d") {
/* it's a directory /
$nlist_dirs[$d_i]=$item[$i][8];
$d_i++;
} elseif ($item_type == "l") {
/
it's a symlink /
$nlist_links[$l_i]=$item[$i][8];
$l_i++;
} elseif ($item_type == "-") {
/
it's a file /
$nlist_files[$f_i]=$item[$i][8];
$nlist_filesize[$f_i]=$item[$i][4];
$f_i++;
} elseif ($item_type == "+") {
/
it's something on an anonftp server /
$eplf=split(",",implode(" ",$item[$i]),5);
if ($eplf[2] == "r") {
/
it's a file /
$nlist_files[$f_i]=trim($eplf[4]);
$nlist_filesize[$f_i]=substr($eplf[3],1);
$f_i++;
} elseif ($eplf[2] == "/") {
/
it's a directory /
$nlist_dirs[$d_i]=trim($eplf[3]);
$d_i++;
}
} /
ignore all others */
$i++;
}

1<table border="0" cellspacing="20">   

if (count($nlist_dirs)&gt;0) {

 1<tr><td align="left" valign="top">
 2<strong>Directories</strong><br/>
 3<form action="ftp.php" method="post">
 4<input name="function" type="hidden" value="cd"/>
 5<input name="phpftp_user" type="hidden" value="```
 6 echo $phpftp_user; 
 7```"/>
 8<input name="phpftp_passwd" type="hidden" value="```
 9 echo $phpftp_passwd; 
10```"/>
11<input name="phpftp_dir" type="hidden" value="```
12 echo $phpftp_dir; 
13```"/>
14<select name="select_directory" size="10" width="100">   

for ($i=0; $i &lt; count($nlist_dirs); $i++) {
echo "<option ""="" $nlist_dirs[$i]="" .="" value='""'>" . $nlist_dirs[$i] . "</option>\n";
}

1</select><br/>
2<input type="submit" value="Enter Directory"/>
3</form>
4</td>   

}

if (count($nlist_files)&gt;0) {

 1<td align="left" valign="top">
 2<strong>Files</strong><br/>
 3<form action="ftp.php" method="post">
 4<input name="function" type="hidden" value="get"/>
 5<input name="phpftp_user" type="hidden" value="```
 6 echo $phpftp_user; 
 7```"/>
 8<input name="phpftp_passwd" type="hidden" value="```
 9 echo $phpftp_passwd; 
10```"/>
11<input name="phpftp_dir" type="hidden" value="```
12 echo $phpftp_dir; 
13```"/>
14<select name="select_file" size="10">   

for ($i=0; $i &lt; count($nlist_files); $i++) {
echo "<option ""="" $nlist_files[$i]="" .="" value='""'>" . $nlist_files[$i] ." ($nlist_filesize[$i] bytes)". "</option>\n";
}

1</select><br/>
2<input type="submit" value="Download File"/>
3</form>
4</td></tr>   

}
} else {

1<p><font color="#ff0000"><strong>Directory empty or not readable</strong></font><p>   

}

1</p></p></table>
2<p>
3<form action="ftp.php" method="post">   

$cdup=dirname($phpftp_dir);
if ($cdup == "") {
$cdup="/";
}

 1<input name="function" type="hidden" value="dir"/>
 2<input name="phpftp_user" type="hidden" value="```
 3 echo $phpftp_user; 
 4```"/>
 5<input name="phpftp_passwd" type="hidden" value="```
 6 echo $phpftp_passwd; 
 7```"/>
 8<input name="phpftp_dir" type="hidden" value="```
 9 echo $cdup; 
10```"/>
11<input type="submit" value="Go up one directory"/>
12</form>
13<p>
14<form action="ftp.php" enctype="multipart/form-data" method="post">
15<input name="max_file_size" type="hidden" value="```
16 echo $max_file_size 
17```"/>
18<input name="phpftp_user" type="hidden" value="```
19 echo $phpftp_user; 
20```"/>
21<input name="phpftp_passwd" type="hidden" value="```
22 echo $phpftp_passwd; 
23```"/>
24<input name="phpftp_dir" type="hidden" value="```
25 echo $phpftp_dir; 
26```"/>
27<input name="function" type="hidden" value="put"/>
28<input type="submit" value="Upload this:"/>
29<input name="userfile" type="file"/>
30</form>
31<p>
32<form action="ftp.php" method="post">
33<input name="function" type="hidden" value="mkdir"/>
34<input name="phpftp_user" type="hidden" value="```
35 echo $phpftp_user; 
36```"/>
37<input name="phpftp_passwd" type="hidden" value="```
38 echo $phpftp_passwd; 
39```"/>
40<input name="phpftp_dir" type="hidden" value="```
41 echo $phpftp_dir; 
42```"/>
43<input type="submit" value="Make subdirectory:"/>
44<input name="new_dir" type="text"/>   

ftp_quit($ftp);
phpftp_bottom();
}
}

function phpftp_cd($phpftp_user,$phpftp_passwd,$phpftp_dir,$select_directory) {

1<!-- function phpftp_cd -->   

$new_directory=$phpftp_dir . "/" . $select_directory;
phpftp_list($phpftp_user,$phpftp_passwd,$new_directory);
}

function phpftp_mkdir($phpftp_user,$phpftp_passwd,$phpftp_dir,$new_dir) {

1<!-- function phpftp_mkdir -->   

$ftp = @phpftp_connect($phpftp_user,$phpftp_passwd);
if ($phpftp_dir == "") {
$phpftp_dir="/";
}
if (!$ftp) {
@ftp_quit($ftp);
phpftp_top();

1<font color="#ff0000"><strong>FTP login failed!</strong></font><p><p>
2<a href="ftp.php">Start over?</a>   

phpftp_bottom();
} else {
$dir_path = $phpftp_dir . "/" . $new_dir;
@ftp_mkdir($ftp,$dir_path);
@ftp_quit($ftp);
phpftp_list($phpftp_user,$phpftp_passwd,$phpftp_dir);
}
};

function phpftp_get($phpftp_user,$phpftp_passwd,$phpftp_dir,$select_file) {
$ftp = @phpftp_connect($phpftp_user,$phpftp_passwd);
if ($phpftp_dir == "") {
$phpftp_dir="/";
}
if ((!$ftp) || (!@ftp_chdir($ftp,$phpftp_dir))) {
@ftp_quit($ftp);
phpftp_top();

1<font color="#ff0000"><strong>FTP login failed!</strong></font><p><p>
2<a href="ftp.php">Start over?</a>   

phpftp_bottom();
} else {
srand((double)microtime()*1000000);
$randval = rand();
$tmpfile=$phpftp_tmpdir . "/" . $select_file . "." . $randval;
if (!ftp_get($ftp,$tmpfile,$select_file,FTP_BINARY)) {
ftp_quit($ftp);
phpftp_top();

1<font color="#ff0000"><strong>FTP get failed!</strong></font><p><p>
2<a href="ftp.php">Start over?</a>   

phpftp_bottom();
} else {
ftp_quit($ftp);
global $use_mime_lookup;
if ($use_mime_lookup == "1") {
$file_mime_type=mime_lookup(substr(strrchr($select_file,"."),1));
}
if (!$file_mime_type) {
$file_mime_type="application/octet-stream";
}
header("Content-Type: " . $file_mime_type);
header("Content-Disposition: attachment; filename=" . $select_file);
readfile($tmpfile);
}
@unlink($tmpfile);
}
}

function phpftp_put($phpftp_user,$phpftp_passwd,$phpftp_dir,$userfile,$userfile_name) {
srand((double)microtime()*1000000);
$randval = rand();
$tmpfile=$phpftp_tmpdir . "/" . $userfile_name . "." . $randval;
if (!@move_uploaded_file($userfile,$tmpfile)) {
phpftp_top();

1<font color="#ff0000"><strong>Upload failed! Can't create temp file?</strong></font>
2<p><p>
3<a href="ftp.php">Start over?</a>   

phpftp_bottom();
} else {
if (!$ftp = @phpftp_connect($phpftp_user,$phpftp_passwd)) {
unlink($tmpfile);
phpftp_top();

1<font color="#ff0000"><strong>FTP login failed!</strong></font><p><p>
2<a href="ftp.php">Start over?</a>   

phpftp_bottom();
} else {
ftp_chdir($ftp,$phpftp_dir);
ftp_put($ftp,$userfile_name,$tmpfile,FTP_BINARY);
ftp_quit($ftp);
unlink($tmpfile);
phpftp_list($phpftp_user,$phpftp_passwd,$phpftp_dir);
}
}
}

switch($function) {
case "dir";
phpftp_list($phpftp_user,$phpftp_passwd,$phpftp_dir);
break;
case "cd";
phpftp_cd($phpftp_user,$phpftp_passwd,$phpftp_dir,$select_directory);
break;
case "get";
phpftp_get($phpftp_user,$phpftp_passwd,$phpftp_dir,$select_file);
break;
case "put";
phpftp_put($phpftp_user,$phpftp_passwd,$phpftp_dir,$userfile,$userfile_name);
break;
case "mkdir";
phpftp_mkdir($phpftp_user,$phpftp_passwd,$phpftp_dir,$new_dir);
break;
case "";
phpftp_login();
break;
}

Published At
Categories with Web编程
Tagged with
comments powered by Disqus