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

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

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 Logging: Best Practices for PHP Log Analysis PHP Logging: Best Practices for PHP Log Analysis Mar 10, 2025 pm 02:32 PM

PHP logging is essential for monitoring and debugging web applications, as well as capturing critical events, errors, and runtime behavior. It provides valuable insights into system performance, helps identify issues, and supports faster troubleshoot

Working with Flash Session Data in Laravel Working with Flash Session Data in Laravel Mar 12, 2025 pm 05:08 PM

Laravel simplifies handling temporary session data using its intuitive flash methods. This is perfect for displaying brief messages, alerts, or notifications within your application. Data persists only for the subsequent request by default: $request-

cURL in PHP: How to Use the PHP cURL Extension in REST APIs cURL in PHP: How to Use the PHP cURL Extension in REST APIs Mar 14, 2025 am 11:42 AM

The PHP Client URL (cURL) extension is a powerful tool for developers, enabling seamless interaction with remote servers and REST APIs. By leveraging libcurl, a well-respected multi-protocol file transfer library, PHP cURL facilitates efficient execution of various network protocols, including HTTP, HTTPS, and FTP. This extension offers granular control over HTTP requests, supports multiple concurrent operations, and provides built-in security features.

Simplified HTTP Response Mocking in Laravel Tests Simplified HTTP Response Mocking in Laravel Tests Mar 12, 2025 pm 05:09 PM

Laravel provides concise HTTP response simulation syntax, simplifying HTTP interaction testing. This approach significantly reduces code redundancy while making your test simulation more intuitive. The basic implementation provides a variety of response type shortcuts: use Illuminate\Support\Facades\Http; Http::fake([ 'google.com' => 'Hello World', 'github.com' => ['foo' => 'bar'], 'forge.laravel.com' =>

12 Best PHP Chat Scripts on CodeCanyon 12 Best PHP Chat Scripts on CodeCanyon Mar 13, 2025 pm 12:08 PM

Do you want to provide real-time, instant solutions to your customers' most pressing problems? Live chat lets you have real-time conversations with customers and resolve their problems instantly. It allows you to provide faster service to your custom

Explain the concept of late static binding in PHP. Explain the concept of late static binding in PHP. Mar 21, 2025 pm 01:33 PM

Article discusses late static binding (LSB) in PHP, introduced in PHP 5.3, allowing runtime resolution of static method calls for more flexible inheritance.Main issue: LSB vs. traditional polymorphism; LSB's practical applications and potential perfo

Customizing/Extending Frameworks: How to add custom functionality. Customizing/Extending Frameworks: How to add custom functionality. Mar 28, 2025 pm 05:12 PM

The article discusses adding custom functionality to frameworks, focusing on understanding architecture, identifying extension points, and best practices for integration and debugging.

See all articles