


FTP operation classes in PHP: copy, move, delete files and create directories
FTP operation classes in PHP: copy, move, delete files and create directories
<?php /** * 作用:FTP操作类( 拷贝、移动、删除文件/创建目录 ) * 时间:2006/5/9 * 作者:欣然随风 * QQ:276624915 */ 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 /************************************** 测试 *********************************** $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连接 ******************************************************************************/ ?>
Detailed description: http://php.662p.com/thread-548-1-1.html
The above has introduced the FTP operation categories in PHP: copying, moving, deleting files and creating directories, including the content. I hope it will be helpful to friends who are interested in PHP tutorials.

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

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

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

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

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

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.

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

Why Snipping Tool Not Working on Windows 11 Understanding the root cause of the problem can help find the right solution. Here are the top reasons why the Snipping Tool might not be working properly: Focus Assistant is On: This prevents the Snipping Tool from opening. Corrupted application: If the snipping tool crashes on launch, it might be corrupted. Outdated graphics drivers: Incompatible drivers may interfere with the snipping tool. Interference from other applications: Other running applications may conflict with the Snipping Tool. Certificate has expired: An error during the upgrade process may cause this issu simple solution. These are suitable for most users and do not require any special technical knowledge. 1. Update Windows and Microsoft Store apps

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
