php能直接js实现ftp上传文件上传和下载为什么还要用ftp

php使用ftp实现文件上传代码片段:
class Ftp {
private $testServer = array(
'host' =& 'ip',
'port' =& 21,
'user' =& 'userName',
'pwd' =& 'password'
string $flag 服务器标识test
* @return mixed
0:服务器连接失败
1:服务器登录失败
resource 连接标识
public function openServer($flag = 'test'){
$config = $this-&getServerConfig($flag);
$connect = ftp_connect($config['host'], $config['port']);
if($connect == false) return 0;
if(!ftp_login($connect, $config['user'], $config['pwd'])) return 1;
ftp_pasv($connect, true);
return $connect;
resource $connect 连接标识
* @param string $dirPath 目录路径
* @return mixed
2:创建目录失败
true:创建目录成功
public function makeDir($connect, $dirPath){
$dirPath = '/' . trim($dirPath, '/');
$dirPath = explode('/', $dirPath);
foreach ($dirPath as $dir){
if($dir == '') $dir = '/';
if(@ftp_chdir($connect, $dir) == false){
if(@ftp_mkDir($connect, $dir) == false){
@ftp_chdir($connect, $dir);
return true;
resource $connect 连接标识
public function closeServer($connect){
if(!empty($connect)) ftp_close($connect);
string $flag 服务器标识
* @param string $local 上传文件的本地路径
* @param string $remote 上传文件的远程路径
* @return int
0:服务器连接失败
1:服务器登录失败
2:创建目录失败
3:上传文件失败
4:上传成功
public function upload($flag = 'test', $local, $remote){
$connect = $this-&openServer($flag);
if(($connect === 0) || ($connect === 1)) return $connect;
$mdr = $this-&makeDir($connect, dirname($remote));
if($mdr === 2) return 2;
$result = ftp_put($connect, basename($remote), $local, FTP_BINARY);
$this-&closeServer($connect);
return (!$result) ? 3 : 4;
string $flag 服务器标识
* @param string $remote 文件的远程路径
* @return int
0:服务器连接失败
1:服务器登录失败
2:删除失败
3:删除成功
public function delete($flag = 'test', $remote){
$connect = $this-&openServer($flag);
if(($connect === 0) || ($connect === 1)) return $connect;
$result = ftp_delete($connect, $remote);
$this-&closeServer($connect);
return (!$result) ? 2 : 3;
string $flag 服务器标识
* @param string $remote 文件的远程路径
* @return mixed
0:服务器连接失败
1:服务器登录失败
public function read($flag, $remote){
$connect = $this-&openServer($flag);
if(($connect === 0) || ($connect === 1)) return $connect;
$result = ftp_nlist($connect, $remote);
$this-&closeServer($connect);
foreach ($result as $key =& $value){
if(in_array($value, array('.', '..'))) unset($result[$key]);
return array_values($result);
string $flag 服务器标识test
* @return array ftp服务器连接配置
private function getServerConfig($flag = 'test'){
$flag = strtolower($flag);
if($flag == 'test') return $this-&testS
return $this-&testS
参考知识库
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
访问:19099次
排名:千里之外
原创:47篇
(4)(2)(13)(10)(7)(5)(5)(1)(1)(2)PHP的FTP类 - 为程序员服务
为程序员服务
PHP的FTP类
仿写CodeIgniter的FTP类 FTP基本操作:
登陆 connect
当前目录文件列表 filelist
目录改变 chgdir
重命名/移动 rename
创建文件夹 mkdir
删除 delete_dir/delete_file
上传 upload
下载 download
* 仿写CodeIgniter的FTP类
* FTP基本操作:
* 1) 登陆;
* 2) 当前目录文件列表;
* 3) 目录改变;
* 4) 重命名/移动;
* 5) 创建文件夹;
* 6) 删除;
delete_dir/delete_file
* 7) 上传;
* @author quanshuidingdang
class Ftp {
private $hostname
private $username
private $password
private $port
private $passive
private $debug
private $conn_id
* 构造函数
配置数组 : $config = array('hostname'=&'','username'=&'','password'=&'','port'=&''...);
public function __construct($config = array()) {
if(count($config) & 0) {
$this-&_init($config);
public function connect($config = array()) {
if(count($config) & 0) {
$this-&_init($config);
if(FALSE === ($this-&conn_id = @ftp_connect($this-&hostname,$this-&port))) {
if($this-&debug === TRUE) {
$this-&_error(&ftp_unable_to_connect&);
return FALSE;
if( ! $this-&_login()) {
if($this-&debug === TRUE) {
$this-&_error(&ftp_unable_to_login&);
return FALSE;
if($this-&passive === TRUE) {
ftp_pasv($this-&conn_id, TRUE);
return TRUE;
* 目录改变
目录标识(ftp)
public function chgdir($path = '', $supress_debug = FALSE) {
if($path == '' OR ! $this-&_isconn()) {
return FALSE;
$result = @ftp_chdir($this-&conn_id, $path);
if($result === FALSE) {
if($this-&debug === TRUE AND $supress_debug == FALSE) {
$this-&_error(&ftp_unable_to_chgdir:dir[&.$path.&]&);
return FALSE;
return TRUE;
* 目录生成
目录标识(ftp)
文件权限列表
public function mkdir($path = '', $permissions = NULL) {
if($path == '' OR ! $this-&_isconn()) {
return FALSE;
$result = @ftp_mkdir($this-&conn_id, $path);
if($result === FALSE) {
if($this-&debug === TRUE) {
$this-&_error(&ftp_unable_to_mkdir:dir[&.$path.&]&);
return FALSE;
if( ! is_null($permissions)) {
$this-&chmod($path,(int)$permissions);
return TRUE;
本地目录标识
远程目录标识(ftp)
上传模式 auto || ascii
上传后的文件权限列表
public function upload($localpath, $remotepath, $mode = 'auto', $permissions = NULL) {
if( ! $this-&_isconn()) {
return FALSE;
if( ! file_exists($localpath)) {
if($this-&debug === TRUE) {
$this-&_error(&ftp_no_source_file:&.$localpath);
return FALSE;
if($mode == 'auto') {
$ext = $this-&_getext($localpath);
$mode = $this-&_settype($ext);
$mode = ($mode == 'ascii') ? FTP_ASCII : FTP_BINARY;
$result = @ftp_put($this-&conn_id, $remotepath, $localpath, $mode);
if($result === FALSE) {
if($this-&debug === TRUE) {
$this-&_error(&ftp_unable_to_upload:localpath[&.$localpath.&]/remotepath[&.$remotepath.&]&);
return FALSE;
if( ! is_null($permissions)) {
$this-&chmod($remotepath,(int)$permissions);
return TRUE;
远程目录标识(ftp)
本地目录标识
下载模式 auto || ascii
public function download($remotepath, $localpath, $mode = 'auto') {
if( ! $this-&_isconn()) {
return FALSE;
if($mode == 'auto') {
$ext = $this-&_getext($remotepath);
$mode = $this-&_settype($ext);
$mode = ($mode == 'ascii') ? FTP_ASCII : FTP_BINARY;
$result = @ftp_get($this-&conn_id, $localpath, $remotepath, $mode);
if($result === FALSE) {
if($this-&debug === TRUE) {
$this-&_error(&ftp_unable_to_download:localpath[&.$localpath.&]-remotepath[&.$remotepath.&]&);
return FALSE;
return TRUE;
* 重命名/移动
远程目录标识(ftp)
新目录标识
boolean 判断是重命名(FALSE)还是移动(TRUE)
public function rename($oldname, $newname, $move = FALSE) {
if( ! $this-&_isconn()) {
return FALSE;
$result = @ftp_rename($this-&conn_id, $oldname, $newname);
if($result === FALSE) {
if($this-&debug === TRUE) {
$msg = ($move == FALSE) ? &ftp_unable_to_rename& : &ftp_unable_to_move&;
$this-&_error($msg);
return FALSE;
return TRUE;
* 删除文件
文件标识(ftp)
public function delete_file($file) {
if( ! $this-&_isconn()) {
return FALSE;
$result = @ftp_delete($this-&conn_id, $file);
if($result === FALSE) {
if($this-&debug === TRUE) {
$this-&_error(&ftp_unable_to_delete_file:file[&.$file.&]&);
return FALSE;
return TRUE;
* 删除文件夹
目录标识(ftp)
public function delete_dir($path) {
if( ! $this-&_isconn()) {
return FALSE;
//对目录宏的'/'字符添加反斜杠'\'
$path = preg_replace(&/(.+?)\/*$/&, &\\1/&, $path);
//获取目录文件列表
$filelist = $this-&filelist($path);
if($filelist !== FALSE AND count($filelist) & 0) {
foreach($filelist as $item) {
//如果我们无法删除,那么就可能是一个文件夹
//所以我们递归调用delete_dir()
if( ! @delete_file($item)) {
$this-&delete_dir($item);
//删除文件夹(空文件夹)
$result = @ftp_rmdir($this-&conn_id, $path);
if($result === FALSE) {
if($this-&debug === TRUE) {
$this-&_error(&ftp_unable_to_delete_dir:dir[&.$path.&]&);
return FALSE;
return TRUE;
* 修改文件权限
目录标识(ftp)
public function chmod($path, $perm) {
if( ! $this-&_isconn()) {
return FALSE;
//只有在PHP5中才定义了修改权限的函数(ftp)
if( ! function_exists('ftp_chmod')) {
if($this-&debug === TRUE) {
$this-&_error(&ftp_unable_to_chmod(function)&);
return FALSE;
$result = @ftp_chmod($this-&conn_id, $perm, $path);
if($result === FALSE) {
if($this-&debug === TRUE) {
$this-&_error(&ftp_unable_to_chmod:path[&.$path.&]-chmod[&.$perm.&]&);
return FALSE;
return TRUE;
* 获取目录文件列表
目录标识(ftp)
public function filelist($path = '.') {
if( ! $this-&_isconn()) {
return FALSE;
return ftp_nlist($this-&conn_id, $path);
public function close() {
if( ! $this-&_isconn()) {
return FALSE;
return @ftp_close($this-&conn_id);
* FTP成员变量初始化
private function _init($config = array()) {
foreach($config as $key =& $val) {
if(isset($this-&$key)) {
$this-&$key = $
//特殊字符过滤
$this-&hostname = preg_replace('|.+?://|','',$this-&hostname);
private function _login() {
return @ftp_login($this-&conn_id, $this-&username, $this-&password);
* 判断con_id
private function _isconn() {
if( ! is_resource($this-&conn_id)) {
if($this-&debug === TRUE) {
$this-&_error(&ftp_no_connection&);
return FALSE;
return TRUE;
* 从文件名中获取后缀扩展
private function _getext($filename) {
if(FALSE === strpos($filename, '.')) {
return 'txt';
$extarr = explode('.', $filename);
return end($extarr);
* 从后缀扩展定义FTP传输模式
ascii 或 binary
private function _settype($ext) {
$text_type = array (
return (in_array($ext, $text_type)) ? 'ascii' : 'binary';
* 错误日志记录
private function _error($msg) {
return @file_put_contents('ftp_err.log', &date[&.date(&Y-m-d H:i:s&).&]-hostname[&.$this-&hostname.&]-username[&.$this-&username.&]-password[&.$this-&password.&]-msg[&.$msg.&]\n&, FILE_APPEND);
/*End of file ftp.php*/
/*Location /Apache Group/htdocs/ftp.php*/
ftp_demo.php
require_once('ftp.php');
$config = array(
'hostname' =& 'localhost',
'username' =& 'root',
'password' =& 'root',
'port' =& 21
$ftp = new Ftp();
$ftp-&connect($config);
$ftp-&upload('ftp_err.log','ftp_upload.log');
$ftp-&download('ftp_upload.log','ftp_download.log');
/*End of file ftp_demo.php*/
/*Location: /htdocs/ftp_demo.php*/
您可能的代码
相关聚客文章
荣誉:1228
相关专栏文章php网站程序通过ftp下载下来怎么用呢?_百度知道用PHP控制FTP文件上传
利用,你总是可以有多种方式来完成某个特定的任务。我们就拿文件上传举个例子。当然了,你可以按照传统的方式来使用HTTP文件上传,把文件直接传输到Web服务器磁盘上。你还可以用更加奇异的方式上传,用两步就完成上传:从你的本地硬盘到Web服务器,然后再到FTP服务器。
PHP在本机同时支持FTP和HTTP上传,所以你可以根据自己应用程序的设计需要进行最佳的选择。使用PHP的FTP函数进行文件传输几乎与使用传统的FTP客户端相同——你会看到连函数的名字都和标准的FTP命令类似。
关于HTTP文件上传的文章已经多得满天飞了,这就是为什么本文有必要把注意力放在基于FTP的文件上传上了(但是在后面给出的例子中,两种方式你都会看到)。要注意的是,本教程假设你已经安装好了PHP/Apache,而且HTTP文件上传和FTP的函数都已经激活了。
本文可以从下载,所有的源代码都以单独的文本方式列出来了。
第一步:确信你拥有连接/上传到FTP服务器的权限
的FTP函数需要客户端-服务器连接,所以你需要在进行文件上传之前登录到目标服务器上。你的第一项任务是确信你已经拥有了完成这项任务的信任书。这一步
可能看起来是理所当然的,但是你会惊奇地发现有多少开发人员忘了这么做,结果后来浪费大量的时间来解决因此而出现的问题。
你可以通过使用命令行的FTP客户端登录到目标服务器上,以检查连接状况并尝试上传一个假文件(列表A):
Connected to .
220 Welcome to leon FTP Server!
User: upload
331 User upload okay, need password.
Password: ***
230 Restricted user logged in.
200 Type okay.
Hash mark printing On?ftp: (2048 bytes/hash mark) .
ftp& put file.bin
200 PORT command successful.
150 Opening BINARY mode data connection.
226 Transfer completed.
ftp: 4289 bytes sent in 0.00Seconds Kbytes/sec.
221 Goodbye.
一旦确认有相关的访问权限,你就可以登录出来。
第二步:创建上传表单
然后,创建一个简单的HTML表单,向用户询问重要的参数——FTP服务器的访问信息、服务器上传的目录,以及完整的目录和上传文件的名字。下面这个例子就是这个表单的样式(列表B):
Please provide the following information:
Destination directory
在这里,元素用来生成文件选择对话框,用户通过它来选择要被上传的文件。
元素然后就把表单数据编码成为多个部分提交,这可以让PHP更容易地识别提交的文件组件。
第三步:创建PHP上传处理程序
一旦表单被提交给了Web服务器,下一步(也是最后一步)是使用PHP的FTP函数按照用户提供的访问信任书把它传输到目标服务器上。下面就是完成上述所有工作的脚本(upload.php),见列表C:
已投稿到:php实现从ftp服务器上下载文件 如何实现自动下载不弹出文件下载框?下载的文件却比原文件小一些?
[问题点数:20分,结帖人yun513]
php实现从ftp服务器上下载文件 如何实现自动下载不弹出文件下载框?下载的文件却比原文件小一些?
[问题点数:20分,结帖人yun513]
不显示删除回复
显示所有回复
显示星级回复
显示得分回复
只显示楼主
本帖子已过去太久远了,不再提供回复功能。}

我要回帖

更多关于 java实现ftp上传下载 的文章

更多推荐

版权声明:文章内容来源于网络,版权归原作者所有,如有侵权请点击这里与我们联系,我们将及时删除。

点击添加站长微信