Home Backend Development PHP Tutorial 一个php ftp种(复制、移动、删除文件、创建目录等)

一个php ftp种(复制、移动、删除文件、创建目录等)

Jun 13, 2016 pm 12:47 PM
ftp gt path this

一个php ftp类(复制、移动、删除文件、创建目录等)
一个不错的ftp操作类,可以完成复制、移动、删除文件、创建目录等操作,有需要的朋友,可以参考下。

本文原始链接:http://www.jbxue.com/article/7669.html

1、ftp类

<?php
/**
* 作用:FTP类( 拷贝、移动、删除文件/创建目录 )
* site http://www.jbxue.com
*/
class 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


2、调用
<?php
$ftp = new class_ftp('192.168.100.143',21,'user','pwd'); // 打开FTP连接
//$ftp->up_file('aa.txt','a/b/c/cc.txt'); // 上传文件
//$ftp->move_file('a/b/c/cc.txt','a/cc.txt'); // 移动文件
//$ftp->copy_file('a/cc.txt','a/b/dd.txt'); // 复制文件
//$ftp->del_file('a/b/dd.txt'); // 删除文件
$ftp->close(); // 关闭FTP连接
?>
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)

What are the differences between Huawei GT3 Pro and GT4? What are the differences between Huawei GT3 Pro and GT4? Dec 29, 2023 pm 02:27 PM

Many users will choose the Huawei brand when choosing smart watches. Among them, Huawei GT3pro and GT4 are very popular choices. Many users are curious about the difference between Huawei GT3pro and GT4. Let’s introduce the two to you. . What are the differences between Huawei GT3pro and GT4? 1. Appearance GT4: 46mm and 41mm, the material is glass mirror + stainless steel body + high-resolution fiber back shell. GT3pro: 46.6mm and 42.9mm, the material is sapphire glass + titanium body/ceramic body + ceramic back shell 2. Healthy GT4: Using the latest Huawei Truseen5.5+ algorithm, the results will be more accurate. GT3pro: Added ECG electrocardiogram and blood vessel and safety

How to set up nginx reverse proxy ftp server How to set up nginx reverse proxy ftp server May 17, 2023 am 09:31 AM

1. Install nginx 2. Install vsftpd 3. Modify the nginx configuration file nginx.conf 3.1 Add the ftp user userftpuser in the first line; 3.2 Configure the relevant path server{ listen80; #nginx proxy port server_namelocalhost; #ftp server address location/images{root /home/ftpuser; #The absolute path of the folder of the proxy ftp server indexftpuser; #Set the welcome page

Using FTP in Go: A Complete Guide Using FTP in Go: A Complete Guide Jun 17, 2023 pm 06:31 PM

With the rapid development of the Internet, File Transfer Protocol (FTP) has always been an important file transfer method. In Go language, using FTP to transfer files may be a need of many developers. However, maybe many people don't know how to use FTP in Go language. In this article, we will explore how to use FTP in Go language, from connecting to FTP server to file transfer, and how to handle errors and exceptions. Create FTP connection In Go language, we can use the standard "net" package to connect to FTP

Steps to set the PATH environment variable of the Linux system Steps to set the PATH environment variable of the Linux system Feb 18, 2024 pm 05:40 PM

How to set the PATH environment variable in Linux systems In Linux systems, the PATH environment variable is used to specify the path where the system searches for executable files on the command line. Correctly setting the PATH environment variable allows us to execute system commands and custom commands at any location. This article will introduce how to set the PATH environment variable in a Linux system and provide detailed code examples. View the current PATH environment variable. Execute the following command in the terminal to view the current PATH environment variable: echo$P

PHP and FTP: realizing file sharing among multiple departments in website development PHP and FTP: realizing file sharing among multiple departments in website development Jul 28, 2023 pm 01:01 PM

PHP and FTP: Achieve file sharing among multiple departments in website development. With the development of the Internet, more and more companies are beginning to use website platforms for information release and business promotion. However, the problem that arises is how to achieve file sharing and collaboration among multiple departments. In this case, PHP and FTP become one of the most commonly used solutions. This article will introduce how to use PHP and FTP to achieve file sharing among multiple departments in website development. 1. Introduction to FTP FTP (FileTransferPr

What are the ftp commands under linux? What are the ftp commands under linux? Mar 21, 2023 am 09:59 AM

The ftp commands under Linux include: 1. ftp command; 2. close command; 3. disconnect command; 4. open command; 5. user command; 6. account command; 7. bye command; 8. quit command; 9. help command ;10. rhelp command; 11. ascii command; 12. binary/bi command; 13. bell command, etc.

How to compare directories and files on an FTP server via PHP How to compare directories and files on an FTP server via PHP Jul 28, 2023 pm 02:09 PM

How to compare directories and files on an FTP server through PHP In web development, sometimes we need to compare local files with files on the FTP server to ensure consistency between the two. PHP provides some functions and classes to implement this functionality. This article will introduce how to use PHP to compare directories and files on an FTP server, and provide relevant code examples. First, we need to connect to the FTP server. PHP provides the ftp_connect() function to establish an FTP server

What does linux ftp 530 mean? What does linux ftp 530 mean? Mar 14, 2023 am 10:16 AM

linux ftp530 means linux ftp login error 530. The solution is: 1. Check "cat /etc/shells" to see if your user's home directory and login shell are there. If not, add them; 2. Check "/var /log/secure" file and reset the password expiration time.

See all articles