Table of Contents
PHP操作FTP类 (上传、下载、移动、创建等),phpftp
您可能感兴趣的文章:
Home php教程 php手册 PHP操作FTP类 (上传、下载、移动、创建等),phpftp

PHP操作FTP类 (上传、下载、移动、创建等),phpftp

Jun 13, 2016 am 08:42 AM
ftp php

PHP操作FTP类 (上传、下载、移动、创建等),phpftp

本文针对PHP操作FTP类进行详细介绍,php实现FTP上传、FTP下载、FTP移动、FTP创建等,供大家参考,具体内容如下

1.使用PHP操作FTP-用法

<&#63;php 
  
// 联接FTP服务器 
$conn = ftp_connect(ftp.server.com); 
  
// 使用username和password登录 
ftp_login($conn, “john”, “doe”); 
  
// 获取远端系统类型 
ftp_systype($conn); 
  
// 列示文件 
$filelist = ftp_nlist($conn, “.”); 
  
// 下载文件 
ftp_get($conn, “data.zip”, “data.zip”, FTP_BINARY); 
  
// 关闭联接 
ftp_quit($conn); 
  
//初结化一个FTP联接,PHP提供了ftp_connect()这个函数,它使用主机名称和端口作为参数。在上面的例子里,主机名字为 “ftp.server.com”;如果端口没指定,PHP将会使用“21”作为缺省端口来建立联接。 
  
//联接成功后ftp_connect()传回一个handle句柄;这个handle将被以后使用的FTP函数使用。 
$conn = ftp_connect(ftp.server.com); 
  
//一旦建立联接,使用ftp_login()发送一个用户名称和用户密码。你可以看到,这个函数ftp_login()使用了 ftp_connect()函数传来的handle,以确定用户名和密码能被提交到正确的服务器。 
ftp_login($conn, “john”, “doe”); 
  
// close connection 
ftp_quit($conn); 
  
//登录了FTP服务器,PHP提供了一些函数,它们能获取一些关于系统和文件以及目录的信息。 
ftp_pwd() 
  
//获取当前所在的目录 
$here = ftp_pwd($conn); 
  
//获取服务器端系统信息ftp_systype() 
$server_os = ftp_systype($conn); 
  
//被动模式(PASV)的开关,打开或关闭PASV(1表示开) 
ftp_pasv($conn, 1); 
  
//进入目录中用ftp_chdir()函数,它接受一个目录名作为参数。 
ftp_chdir($conn, “public_html”); 
  
//回到所在的目录父目录用ftp_cdup()实现 
ftp_cdup($conn); 
  
//建立或移动一个目录,这要使用ftp_mkdir()和ftp_rmdir()函数;注意:ftp_mkdir()建立成功的话,就会返回新建立的目录名。 
ftp_mkdir($conn, “test”); 
  
ftp_rmdir($conn, “test”); 
  
//上传文件,ftp_put()函数能很好的胜任,它需要你指定一个本地文件名,上传后的文件名以及传输的类型。比方说:如果你想上传 “abc.txt”这个文件,上传后命名为“xyz.txt”,命令应该是这样: 
ftp_put($conn, “xyz.txt”, “abc.txt”, FTP_ASCII); 
  
//下载文件:PHP所提供的函数是ftp_get(),它也需要一个服务器上文件名,下载后的文件名,以及传输类型作为参数,例如:服务器端文件为his.zip,你想下载至本地机,并命名为hers.zip,命令如下: 
ftp_get($conn, “hers.zip”, “his.zip”, FTP_BINARY); 
  
//PHP提供两种方法:一种是简单列示文件名和目录,另一种就是详细的列示文件的大小,权限,创立时间等信息。 
  
//第一种使用ftp_nlist()函数,第二种用ftp_rawlist().两种函数都需要一个目录名做为参数,都返回目录列做为一个数组,数组的每一个元素相当于列表的一行。 
$filelist = ftp_nlist($conn, “.”); 
  
//函数ftp_size(),它返回你所指定的文件的大小,使用BITES作为单位。要指出的是,如果它返回的是 “-1”的话,意味着这是一个目录 
$filelist = ftp_size($conn, “data.zip”); 
  
&#63;> 
Copy after login

2. FTP上传类 (ftp.php)

<&#63;php 
/******************************************** 
* MODULE:FTP类 
*******************************************/ 
class ftp 
{ 
  public $off;             // 返回操作状态(成功/失败) 
  public $conn_id;           // FTP连接 
  
  /** 
  * 方法:FTP连接 
  * @FTP_HOST -- FTP主机 
  * @FTP_PORT -- 端口 
  * @FTP_USER -- 用户名 
  * @FTP_PASS -- 密码 
  */ 
  function __construct($FTP_HOST,$FTP_PORT,$FTP_USER,$FTP_PASS) 
  { 
    $this->conn_id = @ftp_connect($FTP_HOST,$FTP_PORT) or die("FTP服务器连接失败"); 
    @ftp_login($this->conn_id,$FTP_USER,$FTP_PASS) or die("FTP服务器登陆失败"); 
    @ftp_pasv($this->conn_id,1); // 打开被动模拟 
  } 
  
  /** 
  * 方法:上传文件 
  * @path  -- 本地路径 
  * @newpath -- 上传路径 
  * @type  -- 若目标目录不存在则新建 
  */ 
  function up_file($path,$newpath,$type=true) 
  { 
    if($type) $this->dir_mkdirs($newpath); 
    $this->off = @ftp_put($this->conn_id,$newpath,$path,FTP_BINARY); 
    if(!$this->off) echo "文件上传失败,请检查权限及路径是否正确!"; 
  } 
  
  /** 
  * 方法:移动文件 
  * @path  -- 原路径 
  * @newpath -- 新路径 
  * @type  -- 若目标目录不存在则新建 
  */ 
  function move_file($path,$newpath,$type=true) 
  { 
    if($type) $this->dir_mkdirs($newpath); 
    $this->off = @ftp_rename($this->conn_id,$path,$newpath); 
    if(!$this->off) echo "文件移动失败,请检查权限及原路径是否正确!"; 
  } 
  
  /** 
  * 方法:复制文件 
  * 说明:由于FTP无复制命令,本方法变通操作为:下载后再上传到新的路径 
  * @path  -- 原路径 
  * @newpath -- 新路径 
  * @type  -- 若目标目录不存在则新建 
  */ 
  function copy_file($path,$newpath,$type=true) 
  { 
    $downpath = "c:/tmp.dat"; 
    $this->off = @ftp_get($this->conn_id,$downpath,$path,FTP_BINARY);// 下载 
    if(!$this->off) echo "文件复制失败,请检查权限及原路径是否正确!"; 
    $this->up_file($downpath,$newpath,$type); 
  } 
  
  /** 
  * 方法:删除文件 
  * @path -- 路径 
  */ 
  function del_file($path) 
  { 
    $this->off = @ftp_delete($this->conn_id,$path); 
    if(!$this->off) echo "文件删除失败,请检查权限及路径是否正确!"; 
  } 
  
  /** 
  * 方法:生成目录 
  * @path -- 路径 
  */ 
  function dir_mkdirs($path) 
  { 
    $path_arr = explode('/',$path);       // 取目录数组 
    $file_name = array_pop($path_arr);      // 弹出文件名 
    $path_div = count($path_arr);        // 取层数 
  
    foreach($path_arr as $val)          // 创建目录 
    { 
      if(@ftp_chdir($this->conn_id,$val) == FALSE) 
      { 
        $tmp = @ftp_mkdir($this->conn_id,$val); 
        if($tmp == FALSE) 
        { 
          echo "目录创建失败,请检查权限及路径是否正确!"; 
          exit; 
        } 
        @ftp_chdir($this->conn_id,$val); 
      } 
    } 
      
    for($i=1;$i<=$path_div;$i++)         // 回退到根 
    { 
      @ftp_cdup($this->conn_id); 
    } 
  } 
  
  /** 
  * 方法:关闭FTP连接 
  */ 
  function close() 
  { 
    @ftp_close($this->conn_id); 
  } 
} 
// class class_ftp end 
Copy after login

/************************************** 测试 *********************************** 
$ftp = new ftp('222.13.67.42',21,'hlj','123456');     // 打开FTP连接 
$ftp->up_file('aa.wav','test/13548957217/bb.wav');     // 上传文件 
//$ftp->move_file('aaa/aaa.php','aaa.php');        // 移动文件 
//$ftp->copy_file('aaa.php','aaa/aaa.php');        // 复制文件 
//$ftp->del_file('aaa.php');                // 删除文件 
$ftp->close();                       // 关闭FTP连接 
//******************************************************************************/ 

Copy after login

3. PHP用FTP函数创建目录

<&#63;php 
// create directory through FTP connection 
function FtpMkdir($path, $newDir) { 
   
    $server='ftp.yourserver.com'; // ftp server 
    $connection = ftp_connect($server); // connection 
   
  
    // login to ftp server 
    $user = "me"; 
    $pass = "password"; 
    $result = ftp_login($connection, $user, $pass); 
  
  // check if connection was made 
   if ((!$connection) || (!$result)) { 
    return false; 
    exit(); 
    } else { 
     ftp_chdir($connection, $path); // go to destination dir 
    if(ftp_mkdir($connection,$newDir)) { // create directory 
      return $newDir; 
    } else { 
      return false;     
    } 
  ftp_close($conn_id); // close connection 
  } 
  
} 
&#63;> 
Copy after login

以上就是本文的全部内容,希望对大家学习php程序设计有所帮助。

您可能感兴趣的文章:

  • 用PHP实现Ftp用户的在线管理的代码
  • bplaced 德国可绑米2G支持FTP免费PHP空间
  • php实现从ftp服务器上下载文件树到本地电脑的程序
  • php下连接ftp实现文件的上传、下载、删除文件实例代码
  • php ftp文件上传函数(基础版)
  • 无需重新编译php加入ftp扩展的解决方法
  • 深入PHP FTP类的详解
  • win2008 r2 服务器环境配置(FTP/ASP/ASP.Net/PHP)
  • PHP FTP操作类代码( 上传、拷贝、移动、删除文件/创建目录)
  • PHP实现ftp上传文件示例
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian Dec 24, 2024 pm 04:42 PM

PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

7 PHP Functions I Regret I Didn't Know Before 7 PHP Functions I Regret I Didn't Know Before Nov 13, 2024 am 09:42 AM

If you are an experienced PHP developer, you might have the feeling that you’ve been there and done that already.You have developed a significant number of applications, debugged millions of lines of code, and tweaked a bunch of scripts to achieve op

How To Set Up Visual Studio Code (VS Code) for PHP Development How To Set Up Visual Studio Code (VS Code) for PHP Development Dec 20, 2024 am 11:31 AM

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Apr 05, 2025 am 12:04 AM

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

How do you parse and process HTML/XML in PHP? How do you parse and process HTML/XML in PHP? Feb 07, 2025 am 11:57 AM

This tutorial demonstrates how to efficiently process XML documents using PHP. XML (eXtensible Markup Language) is a versatile text-based markup language designed for both human readability and machine parsing. It's commonly used for data storage an

PHP Program to Count Vowels in a String PHP Program to Count Vowels in a String Feb 07, 2025 pm 12:12 PM

A string is a sequence of characters, including letters, numbers, and symbols. This tutorial will learn how to calculate the number of vowels in a given string in PHP using different methods. The vowels in English are a, e, i, o, u, and they can be uppercase or lowercase. What is a vowel? Vowels are alphabetic characters that represent a specific pronunciation. There are five vowels in English, including uppercase and lowercase: a, e, i, o, u Example 1 Input: String = "Tutorialspoint" Output: 6 explain The vowels in the string "Tutorialspoint" are u, o, i, a, o, i. There are 6 yuan in total

Explain late static binding in PHP (static::). Explain late static binding in PHP (static::). Apr 03, 2025 am 12:04 AM

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

What are PHP magic methods (__construct, __destruct, __call, __get, __set, etc.) and provide use cases? What are PHP magic methods (__construct, __destruct, __call, __get, __set, etc.) and provide use cases? Apr 03, 2025 am 12:03 AM

What are the magic methods of PHP? PHP's magic methods include: 1.\_\_construct, used to initialize objects; 2.\_\_destruct, used to clean up resources; 3.\_\_call, handle non-existent method calls; 4.\_\_get, implement dynamic attribute access; 5.\_\_set, implement dynamic attribute settings. These methods are automatically called in certain situations, improving code flexibility and efficiency.

See all articles