Web Way ftp_php Basics
Last Update:2017-01-18
Source: Internet
Author: User
<?php
/* $Id: Ftp.php,v 2.2 2002/03/12 23:21:28 Pauls EXP $ * *
/* This software came from http://inebria.com/* *
/* Copyright (c) 2000
Paul Southworth. All rights reserved.
Redistribution and use in source and binary forms, with or without
Modification, are permitted provided that following conditions
Are met:
1. Redistributions of source code must retain the above copyright
Notice, this list of conditions and the following disclaimer.
2. redistributions in binary form must reproduce the above copyright
Notice, this list of conditions and the following disclaimer in the
Documentation and/or materials provided with the distribution.
3. The name of the author May is used to endorse or promote
Products derived from this software without specific prior
Written permission.
This SOFTWARE was provided by the AUTHOR ' as is ' and any EXPRESS
or implied warranties, including, BUT not LIMITED to, the implied
Warranties of merchantability and FITNESS for A particular purpose
ARE disclaimed. In NO EVENT SHALL the AUTHOR is liable for any
DIRECT, INDIRECT, incidental, SPECIAL, exemplary, OR consequential
Damages (including, BUT not LIMITED to, procurement of substitute
goods or SERVICES; LOSS of Use, DATA, OR profits; OR BUSINESS
Interruption) HOWEVER caused and on any theory of liability,
WHETHER in CONTRACT, STRICT liability, OR TORT (including negligence
OR otherwise) arising in any WAY out of the "use of" this SOFTWARE,
Even IF advised of the possibility of SUCH DAMAGE. */
/* Configuration Options * *
$phpftp _host= "localhost";
$phpftp _version= "2.2";
/* Comment This out if you don ' t want the version footer * *
$show _version_footer=1;
/* How large a file would you accept? Also need to edit your
php.ini file and Change upload_max_filesize appropriately * *
$max _file_size= "1000000";
/* The temporary directory $phpftp _tmpdir must exist and be writable
By your Web server.
Hint:mkdir/var/tmp/xfers && chmod 1777/var/tmp/xfers *
$phpftp _tmpdir= "/var/tmp/xfers";
/* $use _mime_lookup
Turning this on creates a dependency upon the
Http://www.inebria.com/mime_lookup/MIME Type Lookup Library.
Setting this variable to "1" enables it. "0" disables.
If you are turn it, put the mime_lookup.php file in the same directory
As ftp.php and uncomment the ' include ("mime_lookup.php"); ' statement. */
$use _mime_lookup= "0";
/* Include ("mime_lookup.php"); */
/* We enclose the top and bottom in functions because sometimes
We might not send them (ie, in a file-download situation) * *
function Phpftp_top () {
Global $phpftp _version;
?>
<!--function Phpftp_top-->
<html>
<head>
<title>php FTP Client <?php echo $phpftp _version;?></title>
</head>
<body bgcolor= "#ffffff" >
<?php
}
function Phpftp_bottom () {
Global $phpftp _version;
Global $show _version_footer;
?>
<!--function Phpftp_bottom-->
<?php
if (Isset ($show _version_footer)) {
?>
<p><font Size=-2>this is <a href= "http://inebria.com/phpftp/" >php
Version <?php echo $phpftp _version;?></font></p>
<?php
}
?>
</body>
</html>
<?php
}
/* This are the form used for initially collecting USERNAME/PASSWD * *
function Phpftp_login () {
Phpftp_top ();
?>
<!--function Phpftp_login-->
<p>
<form action= "ftp.php" method=post>
<p><table border=0>
<tr><td>
Login:
</td><td>
<input name= "Phpftp_user" type= "text" >
</td></tr>
<tr><td>
Password:
</td><td>
<input name= "phpftp_passwd" type= "password" >
</td></tr>
<tr><td>
Directory:
</td><td>
<input name= "Phpftp_dir" type= "text" >
</td></tr>
</table>
</p><p>
<input type= "hidden" name= "function" value= "dir" >
<input type= "Submit" value= "Connect" >
</p>
</form>
<p>
<?php
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 ();
?>
<!--function Phpftp_list-->
<?php
$ftp = @phpftp_connect ($phpftp _user, $phpftp _passwd);
if (! $ftp) {
?>
<STRONG>FTP Login Failed!</strong>
<a href= "ftp.php" >start over?</a>
<?php
Phpftp_bottom ();
} else {
if (! $phpftp _dir) {
$phpftp _dir=ftp_pwd ($FTP);
}
if (! @ftp_chdir ($ftp, $phpftp _dir)) {
?>
<font color= "#ff0000" ><strong>can ' t enter that directory!</strong></font><p><p >
<?php
$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 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 a 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 directory * *
$nlist _dirs[$d _i]=trim ($EPLF [3]);
$d _i++;
}
}/* Ignore all others * *
$i + +;
}
?>
<table border=0 cellspacing=20>
<?php
if (count ($nlist _dirs) >0) {
?>
<TR><TD Align=left valign=top>
<strong>Directories</strong><br>
<form action= "ftp.php" method=post>
<input type= "hidden" name= "function" value= "CD" >
<input type= "hidden" name= "Phpftp_user" value= "<?php echo $phpftp _user;?>" >
<input type= "hidden" name= "phpftp_passwd" value= "<?php echo $phpftp _passwd;?>" >
<input type= "hidden" name= "Phpftp_dir" value= "<?php echo $phpftp _dir;?>" >
<select name= "Select_directory" size= "width=" >
<?php
For ($i =0 $i < count ($nlist _dirs); $i + +) {
echo "<option value=\" ". $nlist _dirs[$i]. ">". $nlist _dirs[$i]. "</option>\n";
}
?>
</select><br>
<input type= "Submit" value= "Enter Directory" >
</form>
</td>
<?php
}
if (count ($nlist _files) >0) {
?>
<TD Align=left valign=top>
<strong>Files</strong><br>
<form action= "ftp.php" method=post>
<input type= "hidden" name= "function" value= "get" >
<input type= "hidden" name= "Phpftp_user" value= "<?php echo $phpftp _user;?>" >
<input type= "hidden" name= "phpftp_passwd" value= "<?php echo $phpftp _passwd;?>" >
<input type= "hidden" name= "Phpftp_dir" value= "<?php echo $phpftp _dir;?>" >
<select name= "Select_file" size= "ten" >
<?php
For ($i =0 $i < count ($nlist _files); $i + +) {
echo "<option value=\" ". $nlist _files[$i]. ">". $nlist _files[$i]. " ($nlist _filesize[$i] bytes) ". "</option>\n";
}
?>
</select><br>
<input type= "Submit" value= "Download File" >
</form>
</td></tr>
<?php
}
} else {
?>
<p><font color= "#ff0000" ><strong>directory empty or not readable</strong></font><p >
<?php
}
?>
</table>
<p>
<form action= "ftp.php" method=post>
<?php
$cdup =dirname ($phpftp _dir);
if ($cdup = = "") {
$cdup = "/";
}
?>
<input type= "hidden" name= "function" value= "dir" >
<input type= "hidden" name= "Phpftp_user" value= "<?php echo $phpftp _user;?>" >
<input type= "hidden" name= "phpftp_passwd" value= "<?php echo $phpftp _passwd;?>" >
<input type= "hidden" name= "Phpftp_dir" value= "<?php echo $cdup;?>" >
<input type= "Submit" value= "go up one Directory" >
</form>
<p>
<form enctype= "Multipart/form-data" action= "ftp.php" method=post>
<input type= "hidden" name= "max_file_size" value= "<?php echo $max _file_size?>" >
<input type= "hidden" name= "Phpftp_user" value= "<?php echo $phpftp _user;?>" >
<input type= "hidden" name= "phpftp_passwd" value= "<?php echo $phpftp _passwd;?>" >
<input type= "hidden" name= "Phpftp_dir" value= "<?php echo $phpftp _dir;?>" >
<input type= "hidden" name= "function" value= "put" >
<input type= "Submit" value= "Upload this:" >
<input name= "UserFile" type= "File" >
</form>
<p>
<form action= "ftp.php" method=post>
<input type= "hidden" name= "function" value= "mkdir" >
<input type= "hidden" name= "Phpftp_user" value= "<?php echo $phpftp _user;?>" >
<input type= "hidden" name= "phpftp_passwd" value= "<?php echo $phpftp _passwd;?>" >
<input type= "hidden" name= "Phpftp_dir" value= "<?php echo $phpftp _dir;?>" >
<input type= "Submit" value= "Make subdirectory:" >
<input name= "New_dir" type= "text" >
<?php
Ftp_quit ($FTP);
Phpftp_bottom ();
}
}
function Phpftp_cd ($phpftp _user, $phpftp _passwd, $phpftp _dir, $select _directory) {
?>
<!--function Phpftp_cd-->
<?php
$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) {
?>
<!--function Phpftp_mkdir-->
<?php
$ftp = @phpftp_connect ($phpftp _user, $phpftp _passwd);
if ($phpftp _dir = "") {
$phpftp _dir= "/";
}
if (! $ftp) {
@ftp_quit ($FTP);
Phpftp_top ();
?>
<font color= "#ff0000" ><strong>ftp login failed!</strong></font><p><p>
<a href= "ftp.php" >start over?</a>
<?php
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 ();
?>
<font color= "#ff0000" ><strong>ftp login failed!</strong></font><p><p>
<a href= "ftp.php" >start over?</a>
<?php
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 ();
?>
<font color= "#ff0000" ><strong>ftp get failed!</strong></font><p><p>
<a href= "ftp.php" >start over?</a>
<?php
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 ();
?>
<font color= "#ff0000" ><strong>upload failed! Can ' t Create temp file?</strong></font>
<p><p>
<a href= "ftp.php" >start over?</a>
<?php
Phpftp_bottom ();
} else {
if (! $ftp = @phpftp_connect ($phpftp _user, $phpftp _passwd)) {
Unlink ($tmpfile);
Phpftp_top ();
?>
<font color= "#ff0000" ><strong>ftp login failed!</strong></font><p><p>
<a href= "ftp.php" >start over?</a>
<?php
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
}
?>