用php写 ftp文件上传函数教程

本篇教程是一款关于用php写 ftp文件上传函数教程,php功能很强大,可以用他自身带的函数来实例ftp的功能,实现文件上传功能,代码如下:

  1. <?php
  2. function phpftp_top() {
  3. echo "<!-- function phpftp_top --> ";
  4. echo "<html> ";
  5. echo "<head> ";
  6. echo "<title>PHP FTP Client v1.4</title> ";
  7. echo "</head> ";
  8. echo "<body bgcolor="#ffffff"> ";
  9. echo "<font > ";
  10. }
  11. function phpftp_bottom() {
  12. echo " <!-- function phpftp_bottom --> ";
  13. echo "</font> ";
  14. echo "</body> ";
  15. echo "</html> ";
  16. }
  17. function phpftp_login() {
  18. phpftp_top();
  19. echo "<!-- function phpftp_login --> ";
  20. echo "<p> ";
  21. echo "<form action="ftp.php3" method=post> ";
  22. echo "Login: ";
  23. echo "<input name="phpftp_user" type="text"> ";
  24. echo "<p> ";
  25. echo "Password: ";
  26. echo "<input name="phpftp_passwd" type="password"> ";
  27. echo "<p> ";
  28. echo "Directory: ";
  29. echo "<input name="phpftp_dir" type="text"> ";
  30. echo "<p> ";
  31. echo "<input type="hidden" name="function" value="dir"> ";
  32. echo "<input type="submit" value="connect"> ";
  33. echo "</form> ";
  34. echo "<p> ";
  35. phpftp_bottom();
  36. }
  37. /* This function does not return TRUE/FALSE - it returns the value of
  38. $ftp, the current FTP stream. */
  39. function phpftp_connect( $phpftp_user, $phpftp_passwd ) {
  40. $phpftp_host="localhost";
  41. $ftp = ftp_connect( $phpftp_host );
  42. if ( $ftp ) {
  43. if ( ftp_login( $ftp, $phpftp_user, $phpftp_passwd ) ) {
  44. return $ftp;
  45. }
  46. }
  47. }
  48. function phpftp_list( $phpftp_user, $phpftp_passwd, $phpftp_dir ) {
  49. phpftp_top();
  50. echo "<!-- function phpftp_list --> ";
  51. $ftp = @phpftp_connect( $phpftp_user, $phpftp_passwd );
  52. if ( ! $ftp ) {
  53. echo "<strong>FTP login failed!</strong> ";
  54. echo "<a href="ftp.php3">Start over?</a> ";
  55. phpftp_bottom();
  56. } else {
  57. if ( ! $phpftp_dir ) {
  58. $phpftp_dir=ftp_pwd( $ftp );
  59. }
  60. if ( ! @ftp_chdir( $ftp, $phpftp_dir ) ) {
  61. echo "<font color="#ff0000"><strong>Can't enter that directory!</strong></font><p><p> ";
  62. $phpftp_dir=ftp_pwd( $ftp );
  63. }
  64. echo "<strong>Current directory:</strong> " . $phpftp_dir . "
  65. ";
  66. if ( $phpftp_dir == "/" ) {
  67. $phpftp_dir="";
  68. }
  69. echo "<table ;
  70. if ( $contents = ftp_nlist( $ftp, "." ) ) {
  71. $d_i=0;
  72. $f_i=0;
  73. sort($contents);
  74. for ( $i=0; $i < count( $contents ); $i++ ) {
  75. $file_size = ftp_size( $ftp, $contents[$i] );
  76. if ( $file_size == -1 ) {
  77. $nlist_dirs[$d_i]=$contents[$i];
  78. $d_i++;
  79. } else {
  80. $nlist_files[$f_i]=$contents[$i];
  81. $nlist_filesize[$f_i]=$file_size;
  82. $f_i++;
  83. }
  84. }
  85. echo "<tr><td> ";
  86. echo "<font ><strong>Directories</strong></font>
  87. ";
  88. echo "<form action="ftp.php3" method=post> ";
  89. echo "<input type="hidden" name="function" value="cd"> ";
  90. echo "<input type="hidden" name="phpftp_user" value="" . $phpftp_user . ""> ";
  91. echo "<input type="hidden" name="phpftp_passwd" value="" . $phpftp_passwd . ""> ";
  92. echo "<input type="hidden" name="phpftp_dir" value="" . $phpftp_dir . ""> ";
  93. echo "<select name="select_directory" size="10" width="100"> ";
  94. for ( $i=0; $i < count( $nlist_dirs ); $i++ ) {
  95. echo "<option value="" . $nlist_dirs[$i] . "">" . $nlist_dirs[$i] . "</option> ";
  96. }
  97. echo "</select>
  98. ";
  99. echo "<input type="submit" value="Enter Directory"> ";
  100. echo "</form> ";
  101. echo "</td><td> ";
  102. echo "<font ><strong>Files</strong></font>
  103. ";
  104. echo "<form action="ftp.php3" method=post> ";
  105. echo "<input type="hidden" name="function" value="get"> ";
  106. echo "<input type="hidden" name="phpftp_user" value="" . $phpftp_user . ""> ";
  107. echo "<input type="hidden" name="phpftp_passwd" value="" . $phpftp_passwd . ""> ";
  108. echo "<input type="hidden" name="phpftp_dir" value="" . $phpftp_dir . ""> ";
  109. echo "<select name="select_file" size="10"> ";
  110. for ( $i=0; $i < count( $nlist_files ); $i++ ) {
  111. echo "<option value="" . $nlist_files[$i] . "">" . $nlist_files[$i] ." ($nlist_filesize[$i] bytes)". "</option> ";
  112. }
  113. echo "</select>
  114. ";
  115. echo "<input type="submit" value="Download File"> ";
  116. echo "</form> ";
  117. echo "</td></tr> ";
  118. } else {
  119. echo "<p><font color="#ff0000"><strong>Directory empty or not readable</strong></font><p> ";
  120. }
  121. echo "</table> ";
  122. echo "<p> ";
  123. echo "<form action="ftp.php3" method=post> ";
  124. $cdup=dirname( $phpftp_dir );
  125. if ( $cdup == "" ) {
  126. $cdup="/";
  127. }
  128. echo "<input type="hidden" name="function" value="dir"> ";
  129. echo "<input type="hidden" name="phpftp_user" value="" . $phpftp_user . ""> ";
  130. echo "<input type="hidden" name="phpftp_passwd" value="" . $phpftp_passwd . ""> ";
  131. echo "<input type="hidden" name="phpftp_dir" value="" . $cdup . ""> ";
  132. echo "<input type="submit" value="Go up one directory"> ";
  133. echo "</form> ";
  134. echo "<p> ";
  135. echo "<form enctype="multipart/form-data" action="ftp.php3" method=post> ";
  136. echo "<input type="hidden" name="max_file_size" value="1000000"> ";
  137. echo "<input type="hidden" name="phpftp_user" value="" . $phpftp_user . ""> ";
  138. echo "<input type="hidden" name="phpftp_passwd" value="" . $phpftp_passwd . ""> ";
  139. echo "<input type="hidden" name="phpftp_dir" value="" . $phpftp_dir . ""> ";
  140. echo "<input type="hidden" name="function" value="put"> ";
  141. echo "<input type="submit" value="Upload this:"> ";
  142. echo "<input name="userfile" type="file"> ";
  143. echo "</form> ";
  144. echo "<p> ";
  145. echo "<form action="ftp.php3" method=post> ";
  146. echo "<input type="hidden" name="function" value="mkdir"> ";
  147. echo "<input type="hidden" name="phpftp_user" value="" . $phpftp_user . ""> ";
  148. echo "<input type="hidden" name="phpftp_passwd" value="" . $phpftp_passwd . ""> ";
  149. echo "<input type="hidden" name="phpftp_dir" value="" . $phpftp_dir . ""> ";
  150. echo "<input type="submit" value="Make subdirectory:"> ";
  151. echo "<input name="new_dir" type="text"> ";
  152. ftp_quit( $ftp );
  153. phpftp_bottom();
  154. }
  155. }
  156. function phpftp_cd( $phpftp_user, $phpftp_passwd, $phpftp_dir, $select_directory ) {
  157. echo "<!-- function phpftp_cd --> ";
  158. $new_directory=$phpftp_dir . "/" . $select_directory;
  159. phpftp_list( $phpftp_user, $phpftp_passwd, $new_directory );
  160. }
  161. function phpftp_mkdir( $phpftp_user, $phpftp_passwd, $phpftp_dir, $new_dir ) {
  162. echo "<!-- function phpftp_mkdir --> ";
  163. $ftp = @phpftp_connect( $phpftp_user, $phpftp_passwd );
  164. if ( $phpftp_dir == "" ) {
  165. $phpftp_dir="/";
  166. }
  167. if ( ! $ftp ) {
  168. @ftp_quit( $ftp );
  169. phpftp_top();
  170. echo "<font color="#ff0000"><strong>FTP login failed!</strong></font><p><p> ";
  171. echo "<a href="ftp.php3">Start over?</a> ";
  172. phpftp_bottom();
  173. } else {
  174. $dir_path = $phpftp_dir . "/" . $new_dir;
  175. @ftp_mkdir( $ftp, $dir_path );
  176. @ftp_quit( $ftp );
  177. phpftp_list( $phpftp_user, $phpftp_passwd, $phpftp_dir );
  178. }
  179. };
  180. function phpftp_get( $phpftp_user, $phpftp_passwd, $phpftp_dir, $select_file ) {
  181. $ftp = @phpftp_connect( $phpftp_user, $phpftp_passwd );
  182. if ( $phpftp_dir == "" ) {
  183. $phpftp_dir="/";
  184. }
  185. if ( ( ! $ftp ) || ( ! @ftp_chdir( $ftp, $phpftp_dir ) ) ) {
  186. @ftp_quit( $ftp );
  187. phpftp_top();
  188. echo "<font color="#ff0000"><strong>FTP login failed!</strong></font><p><p> ";
  189. echo "<a href="ftp.php3">Start over?</a> ";
  190. phpftp_bottom();
  191. } else {
  192. srand((double)microtime()*1000000);
  193. $randval = rand();
  194. $tmpfile="/var/tmp/xfers/" . $select_file . "." . $randval;
  195. if ( ! ftp_get( $ftp, $tmpfile, $select_file, FTP_BINARY ) ) {
  196. ftp_quit( $ftp );
  197. phpftp_top();
  198. echo "<font color="#ff0000"><strong>FTP get failed!</strong></font><p><p> <a href="ftp.php3">Start over?</a> ";
  199. phpftp_bottom();
  200. } else {
  201. ftp_quit( $ftp );
  202. header( "Content-Type: application/octet-stream" );
  203. header( "Content-Disposition: attachment; filename=" . $select_file );
  204. readfile( $tmpfile );
  205. }
  206. @unlink( $tmpfile );
  207. }
  208. }
  209. function phpftp_put( $phpftp_user, $phpftp_passwd, $phpftp_dir, $userfile, $userfile_name ) {
  210. srand((double)microtime()*1000000);
  211. $randval = rand();
  212. $tmpfile="/var/tmp/xfers/" . $userfile_name . "." . $randval;
  213. if ( ! @copy( $userfile, $tmpfile ) ) {
  214. phpftp_top();
  215. echo "<font color="#ff0000"><strong>Upload failed! Probably no space on server for temp file.</strong></font><p><p> <a href="ftp.php3">Start over?</a> ";
  216. phpftp_bottom();
  217. } else {
  218. if ( ! $ftp = @phpftp_connect( $phpftp_user, $phpftp_passwd ) ) {
  219. unlink( $tmpfile );
  220. phpftp_top();
  221. echo "<font color="#ff0000"><strong>FTP login failed!</strong></font><p><p> <a href="ftp.php3">Start over?</a> ";
  222. phpftp_bottom();
  223. } else {
  224. ftp_put( $ftp, $userfile_name, $tmpfile, FTP_BINARY );
  225. ftp_quit( $ftp );
  226. unlink( $tmpfile );
  227. phpftp_list( $phpftp_user, $phpftp_passwd, $phpftp_dir );
  228. }
  229. }
  230. }
  231. switch( $function ) {
  232. case "dir";
  233. phpftp_list( $phpftp_user, $phpftp_passwd, $phpftp_dir );
  234. break;
  235. case "cd";
  236. phpftp_cd( $phpftp_user, $phpftp_passwd, $phpftp_dir, $select_directory );
  237. break;
  238. case "get";
  239. phpftp_get( $phpftp_user, $phpftp_passwd, $phpftp_dir, $select_file );
  240. break;
  241. case "put";
  242. phpftp_put( $phpftp_user, $phpftp_passwd, $phpftp_dir, $userfile, $userfile_name ); //开源代码phpfensi.com
  243. break;
  244. case "mkdir";
  245. phpftp_mkdir( $phpftp_user, $phpftp_passwd, $phpftp_dir, $new_dir );
  246. break;
  247. case "";
  248. phpftp_login();
  249. break;
  250. }
  251. ?>