Home Backend Development PHP Tutorial PHP FTP学习笔记

PHP FTP学习笔记

Jun 20, 2016 pm 12:50 PM

ftp_alloc() 为要上传到 FTP 服务器的文件分配空间。       <br />ftp_cdup() 把当前目录改变为 FTP 服务器上的父目录。       <br />ftp_chdir() 改变 FTP 服务器上的当前目录。      <br />ftp_chmod() 通过 FTP 设置文件上的权限。      <br />ftp_close() 关闭 FTP 连接。       <br />ftp_connect() 打开 FTP 连接。    <br />ftp_delete() 删除 FTP 服务器上的文件。    <br />ftp_exec() 在 FTP 上执行一个程序/命令。      <br />ftp_fget() 从FTP服务器上下载一个文件并保存到本地一个已经打开的文件中<br />ftp_fput() 上传一个已打开的文件,并在 FTP 服务器上把它保存为一个文件<br />ftp_get_option() 返回当前 FTP 连接的各种不同的选项设置。     <br />ftp_get() 从 FTP 服务器下载文件。    <br />ftp_login() 登录 FTP 服务器。    <br />ftp_mdtm() 返回指定文件的最后修改时间。   <br />ftp_mkdir() 在 FTP 服务器创建一个新目录。     <br />ftp_nb_continue() 连续获取/发送文件 (non-blocking)。    <br />ftp_nb_fget() <br />    从FTP服务器上下载文件并保存到本地已经打开的文件中(non-blocking)<br />ftp_nb_fput()    <br />    上传已打开的文件,并在FTP服务器上把它保存为文件(non-blocking)。  <br />ftp_nb_get() 从 FTP 服务器下载文件 (non-blocking)。<br />ftp_nb_put() 把文件上传到服务器 (non-blocking)。 <br />ftp_nlist() 返回指定目录的文件列表。  <br />ftp_pasv() 返回当前 FTP 被动模式是否打开。  <br />ftp_put() 把文件上传到服务器。  <br />ftp_pwd() 返回当前目录名称。 <br />ftp_quit()  ftp_close() 的别名。  <br />ftp_raw() 向 FTP 服务器发送一个 raw 命令。 <br />ftp_rawlist() 返回指定目录中文件的详细列表。  <br />ftp_rename() 重命名 FTP 服务器上的文件或目录。 <br />ftp_rmdir() 删除 FTP 服务器上的目录。  <br />ftp_set_option() 设置各种 FTP 运行时选项。 <br />ftp_site() 向服务器发送 SITE 命令。 <br />ftp_size() 返回指定文件的大小。<br />ftp_ssl_connect() 打开一个安全的 SSL-FTP 连接。 <br />ftp_systype() 返回远程 FTP 服务器的系统类型标识符。<br /><br /><br />class FTPUtil {<br /><br />    public $off;        // 返回操作状态(成功/失败)<br />    public $conn_id;    // FTP连接<br /><br />    /**<br />     * 方法:FTP连接<br />     * @FTP_HOST -- FTP主机<br />     * @FTP_PORT -- 端口<br />     * @FTP_USER -- 用户名<br />     * @FTP_PASS -- 密码<br />     */<br />    function __construct($FTP_HOST, $FTP_PORT, $FTP_USER, $FTP_PASS) {<br />        $this->conn_id = @ftp_connect($FTP_HOST, $FTP_PORT) or die ('FTP服务器连接失败');<br /><br />        @ftp_login($this->conn_id, $FTP_USER, $FTP_PASS) or die('FTP服务器登录失败');<br />        @ftp_pasv($this->conn_id, 1);// 打开被动模拟<br />    }<br /><br />    /**<br />     * 方法:上传文件<br />     * @path    -- 本地路径<br />     * @newPath -- 上传路径<br />     * @type    -- 若目标目录不存在则新建<br />     */<br />    function up_file($path, $newPath, $type = true) {<br />        if ($type) {<br />            $this->dir_mkdirs($newPath);<br />        }<br /><br />        $this->off = @ftp_put($this->conn_id, $newPath, $path, FTP_BINARY);<br />        if (!$this->off) {<br />            echo '文件上传失败,请检查权限及路径是否正确!';<br />        }<br />    }<br /><br />    /**<br />     * 方法:移动文件<br />     * @path    -- 原路径<br />     * @newPath -- 新路径<br />     * @type    -- 若目标目录不存在则新建<br />     */<br />    function move_file($path, $newPath, $type = true) {<br />        if ($type) {<br />            $this->dir_mkdirs($newPath);<br />        }<br /><br />        $this->off = @ftp_rename($this->conn_id, $path, $newPath);<br />        if (!$this->off) {<br />            echo "文件移动失败,请检查权限及原路径是否正确!";<br />        }<br />    }<br /><br />    /**<br />     * 方法:复制文件<br />     * 说明:由于FTP无复制命令,本方法变通操作为:下载后再上传到新的路径<br />     * @path    -- 原路径<br />     * @newPath -- 新路径<br />     * @type    -- 若目标目录不存在则新建<br />     */<br />    function copy_file($path, $newPath, $type = true) {<br />        $downPath = "c:/tmp.dat";<br /><br />        $this->off = @ftp_get($this->conn_id, $downPath, $path, FTP_BINARY);// 下载<br />        if (!$this->off) {<br />            echo "文件复制失败,请检查权限及原路径是否正确!";<br />        }<br />        $this->up_file($downPath, $newPath, $type);<br />    }<br /><br />    /**<br />     * 方法:删除文件<br />     * @path -- 路径<br />     */<br />    function del_file($path) {<br />        $this->off = @ftp_delete($this->conn_id, $path);<br />        if (!$this->off) {<br />            echo "文件删除失败,请检查权限及路径是否正确!";<br />        }<br />    }<br /><br />    /**<br />     * 方法:生成目录<br />     * @path -- 路径<br />     */<br />    function dir_mkdirs($path) {<br />        $path_arr = explode('/', $path);              // 取目录数组<br />        $file_name = array_pop($path_arr);            // 弹出文件名<br />        $path_div = count($path_arr);                // 取层数<br /><br />        foreach ($path_arr as $val) {                  // 创建目录<br />            if (@ftp_chdir($this->conn_id, $val) == FALSE) {<br />                $tmp = @ftp_mkdir($this->conn_id, $val);<br />                if ($tmp == FALSE) {<br />                    echo "目录创建失败,请检查权限及路径是否正确!";<br />                    exit;<br />                }<br />                @ftp_chdir($this->conn_id, $val);<br />            }<br />        }<br /><br />        for ($i = 1; $i <= $path_div; $i++) {                  // 回退到根<br />            @ftp_cdup($this->conn_id);<br />        }<br />    }<br /><br />    /**<br />     * 方法:关闭FTP连接<br />     */<br />    function close() {<br />        @ftp_close($this->conn_id);<br />    }
Copy after login


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)

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 does session hijacking work and how can you mitigate it in PHP? How does session hijacking work and how can you mitigate it in PHP? Apr 06, 2025 am 12:02 AM

Session hijacking can be achieved through the following steps: 1. Obtain the session ID, 2. Use the session ID, 3. Keep the session active. The methods to prevent session hijacking in PHP include: 1. Use the session_regenerate_id() function to regenerate the session ID, 2. Store session data through the database, 3. Ensure that all session data is transmitted through HTTPS.

Describe the SOLID principles and how they apply to PHP development. Describe the SOLID principles and how they apply to PHP development. Apr 03, 2025 am 12:04 AM

The application of SOLID principle in PHP development includes: 1. Single responsibility principle (SRP): Each class is responsible for only one function. 2. Open and close principle (OCP): Changes are achieved through extension rather than modification. 3. Lisch's Substitution Principle (LSP): Subclasses can replace base classes without affecting program accuracy. 4. Interface isolation principle (ISP): Use fine-grained interfaces to avoid dependencies and unused methods. 5. Dependency inversion principle (DIP): High and low-level modules rely on abstraction and are implemented through dependency injection.

How to debug CLI mode in PHPStorm? How to debug CLI mode in PHPStorm? Apr 01, 2025 pm 02:57 PM

How to debug CLI mode in PHPStorm? When developing with PHPStorm, sometimes we need to debug PHP in command line interface (CLI) mode...

How to automatically set permissions of unixsocket after system restart? How to automatically set permissions of unixsocket after system restart? Mar 31, 2025 pm 11:54 PM

How to automatically set the permissions of unixsocket after the system restarts. Every time the system restarts, we need to execute the following command to modify the permissions of unixsocket: sudo...

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.

Framework Security Features: Protecting against vulnerabilities. Framework Security Features: Protecting against vulnerabilities. Mar 28, 2025 pm 05:11 PM

Article discusses essential security features in frameworks to protect against vulnerabilities, including input validation, authentication, and regular updates.

See all articles