-
用php写 ftp文件上传函数教程
本篇教程是一款关于用php写 ftp文件上传函数教程,php功能很强大,可以用他自身带的函数来实例ftp的功能,实现文件上传功能,代码如下:
- <?php
- function phpftp_top() {
- echo "<!-- function phpftp_top --> ";
- echo "<html> ";
- echo "<head> ";
- echo "<title>PHP FTP Client v1.4</title> ";
- echo "</head> ";
- echo "<body bgcolor="#ffffff"> ";
- echo "<font face="courier"> ";
- }
- function phpftp_bottom() {
- echo " <!-- function phpftp_bottom --> ";
- echo "</font> ";
- echo "</body> ";
- echo "</html> ";
- }
- function phpftp_login() {
- phpftp_top();
- echo "<!-- function phpftp_login --> ";
- echo "<p> ";
- echo "<form action="ftp.php3" method=post> ";
- echo "Login: ";
- echo "<input name="phpftp_user" type="text"> ";
- echo "<p> ";
- echo "Password: ";
- echo "<input name="phpftp_passwd" type="password"> ";
- echo "<p> ";
- echo "Directory: ";
- echo "<input name="phpftp_dir" type="text"> ";
- echo "<p> ";
- echo "<input type="hidden" name="function" value="dir"> ";
- echo "<input type="submit" value="connect"> ";
- echo "</form> ";
- echo "<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 ) {
- $phpftp_host="localhost";
- $ftp = ftp_connect( $phpftp_host );
- if ( $ftp ) {
- if ( ftp_login( $ftp, $phpftp_user, $phpftp_passwd ) ) {
- return $ftp;
- }
- }
- }
- function phpftp_list( $phpftp_user, $phpftp_passwd, $phpftp_dir ) {
- phpftp_top();
- echo "<!-- function phpftp_list --> ";
- $ftp = @phpftp_connect( $phpftp_user, $phpftp_passwd );
- if ( ! $ftp ) {
- echo "<strong>FTP login failed!</strong> ";
- echo "<a href="ftp.php3">Start over?</a> ";
- phpftp_bottom();
- } else {
- if ( ! $phpftp_dir ) {
- $phpftp_dir=ftp_pwd( $ftp );
- }
- if ( ! @ftp_chdir( $ftp, $phpftp_dir ) ) {
- echo "<font color="#ff0000"><strong>Can't enter that directory!</strong></font><p><p> ";
- $phpftp_dir=ftp_pwd( $ftp );
- }
- echo "<strong>Current directory:</strong> " . $phpftp_dir . "
- ";
- if ( $phpftp_dir == "/" ) {
- $phpftp_dir="";
- }
- echo "<table border=0 cellspacing=20> ";
- if ( $contents = ftp_nlist( $ftp, "." ) ) {
- $d_i=0;
- $f_i=0;
- sort($contents);
- for ( $i=0; $i < count( $contents ); $i++ ) {
- $file_size = ftp_size( $ftp, $contents[$i] );
- if ( $file_size == -1 ) {
- $nlist_dirs[$d_i]=$contents[$i];
- $d_i++;
- } else {
- $nlist_files[$f_i]=$contents[$i];
- $nlist_filesize[$f_i]=$file_size;
- $f_i++;
- }
- }
- echo "<tr><td> ";
- echo "<font face="courier"><strong>Directories</strong></font>
- ";
- echo "<form action="ftp.php3" method=post> ";
- echo "<input type="hidden" name="function" value="cd"> ";
- echo "<input type="hidden" name="phpftp_user" value="" . $phpftp_user . ""> ";
- echo "<input type="hidden" name="phpftp_passwd" value="" . $phpftp_passwd . ""> ";
- echo "<input type="hidden" name="phpftp_dir" value="" . $phpftp_dir . ""> ";
- echo "<select name="select_directory" size="10" width="100"> ";
- for ( $i=0; $i < count( $nlist_dirs ); $i++ ) {
- echo "<option value="" . $nlist_dirs[$i] . "">" . $nlist_dirs[$i] . "</option> ";
- }
- echo "</select>
- ";
- echo "<input type="submit" value="Enter Directory"> ";
- echo "</form> ";
- echo "</td><td> ";
- echo "<font face="courier"><strong>Files</strong></font>
- ";
- echo "<form action="ftp.php3" method=post> ";
- echo "<input type="hidden" name="function" value="get"> ";
- echo "<input type="hidden" name="phpftp_user" value="" . $phpftp_user . ""> ";
- echo "<input type="hidden" name="phpftp_passwd" value="" . $phpftp_passwd . ""> ";
- echo "<input type="hidden" name="phpftp_dir" value="" . $phpftp_dir . ""> ";
- echo "<select name="select_file" size="10"> ";
- for ( $i=0; $i < count( $nlist_files ); $i++ ) {
- echo "<option value="" . $nlist_files[$i] . "">" . $nlist_files[$i] ." ($nlist_filesize[$i] bytes)". "</option> ";
- }
- echo "</select>
- ";
- echo "<input type="submit" value="Download File"> ";
- echo "</form> ";
- echo "</td></tr> ";
- } else {
- echo "<p><font color="#ff0000"><strong>Directory empty or not readable</strong></font><p> ";
- }
- echo "</table> ";
- echo "<p> ";
- echo "<form action="ftp.php3" method=post> ";
- $cdup=dirname( $phpftp_dir );
- if ( $cdup == "" ) {
- $cdup="/";
- }
- echo "<input type="hidden" name="function" value="dir"> ";
- echo "<input type="hidden" name="phpftp_user" value="" . $phpftp_user . ""> ";
- echo "<input type="hidden" name="phpftp_passwd" value="" . $phpftp_passwd . ""> ";
- echo "<input type="hidden" name="phpftp_dir" value="" . $cdup . ""> ";
- echo "<input type="submit" value="Go up one directory"> ";
- echo "</form> ";
- echo "<p> ";
- echo "<form enctype="multipart/form-data" action="ftp.php3" method=post> ";
- echo "<input type="hidden" name="max_file_size" value="1000000"> ";
- echo "<input type="hidden" name="phpftp_user" value="" . $phpftp_user . ""> ";
- echo "<input type="hidden" name="phpftp_passwd" value="" . $phpftp_passwd . ""> ";
- echo "<input type="hidden" name="phpftp_dir" value="" . $phpftp_dir . ""> ";
- echo "<input type="hidden" name="function" value="put"> ";
- echo "<input type="submit" value="Upload this:"> ";
- echo "<input name="userfile" type="file"> ";
- echo "</form> ";
- echo "<p> ";
- echo "<form action="ftp.php3" method=post> ";
- echo "<input type="hidden" name="function" value="mkdir"> ";
- echo "<input type="hidden" name="phpftp_user" value="" . $phpftp_user . ""> ";
- echo "<input type="hidden" name="phpftp_passwd" value="" . $phpftp_passwd . ""> ";
- echo "<input type="hidden" name="phpftp_dir" value="" . $phpftp_dir . ""> ";
- echo "<input type="submit" value="Make subdirectory:"> ";
- echo "<input name="new_dir" type="text"> ";
- ftp_quit( $ftp );
- phpftp_bottom();
- }
- }
- function phpftp_cd( $phpftp_user, $phpftp_passwd, $phpftp_dir, $select_directory ) {
- echo "<!-- 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 ) {
- echo "<!-- function phpftp_mkdir --> ";
- $ftp = @phpftp_connect( $phpftp_user, $phpftp_passwd );
- if ( $phpftp_dir == "" ) {
- $phpftp_dir="/";
- }
- if ( ! $ftp ) {
- @ftp_quit( $ftp );
- phpftp_top();
- echo "<font color="#ff0000"><strong>FTP login failed!</strong></font><p><p> ";
- echo "<a href="ftp.php3">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();
- echo "<font color="#ff0000"><strong>FTP login failed!</strong></font><p><p> ";
- echo "<a href="ftp.php3">Start over?</a> ";
- phpftp_bottom();
- } else {
- srand((double)microtime()*1000000);
- $randval = rand();
- $tmpfile="/var/tmp/xfers/" . $select_file . "." . $randval;
- if ( ! ftp_get( $ftp, $tmpfile, $select_file, FTP_BINARY ) ) {
- ftp_quit( $ftp );
- phpftp_top();
- echo "<font color="#ff0000"><strong>FTP get failed!</strong></font><p><p> <a href="ftp.php3">Start over?</a> ";
- phpftp_bottom();
- } else {
- ftp_quit( $ftp );
- header( "Content-Type: application/octet-stream" );
- 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="/var/tmp/xfers/" . $userfile_name . "." . $randval;
- if ( ! @copy( $userfile, $tmpfile ) ) {
- phpftp_top();
- 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> ";
- phpftp_bottom();
- } else {
- if ( ! $ftp = @phpftp_connect( $phpftp_user, $phpftp_passwd ) ) {
- unlink( $tmpfile );
- phpftp_top();
- echo "<font color="#ff0000"><strong>FTP login failed!</strong></font><p><p> <a href="ftp.php3">Start over?</a> ";
- phpftp_bottom();
- } else {
- 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 ); //开源代码phpfensi.com
- break;
- case "mkdir";
- phpftp_mkdir( $phpftp_user, $phpftp_passwd, $phpftp_dir, $new_dir );
- break;
- case "";
- phpftp_login();
- break;
- }
- ?>
出处:http://www.phpfensi.com/php/20140909/5148.html
栏目列表
最新更新
nodejs爬虫
Python正则表达式完全指南
爬取豆瓣Top250图书数据
shp 地图文件批量添加字段
爬虫小试牛刀(爬取学校通知公告)
【python基础】函数-初识函数
【python基础】函数-返回值
HTTP请求:requests模块基础使用必知必会
Python初学者友好丨详解参数传递类型
如何有效管理爬虫流量?
SQL SERVER中递归
2个场景实例讲解GaussDB(DWS)基表统计信息估
常用的 SQL Server 关键字及其含义
动手分析SQL Server中的事务中使用的锁
openGauss内核分析:SQL by pass & 经典执行
一招教你如何高效批量导入与更新数据
天天写SQL,这些神奇的特性你知道吗?
openGauss内核分析:执行计划生成
[IM002]Navicat ODBC驱动器管理器 未发现数据
初入Sql Server 之 存储过程的简单使用
这是目前我见过最好的跨域解决方案!
减少回流与重绘
减少回流与重绘
如何使用KrpanoToolJS在浏览器切图
performance.now() 与 Date.now() 对比
一款纯 JS 实现的轻量化图片编辑器
关于开发 VS Code 插件遇到的 workbench.scm.
前端设计模式——观察者模式
前端设计模式——中介者模式
创建型-原型模式