一个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 ?>
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连接 ?>

What’s still popular is the ease of use, flexibility and a strong ecosystem. 1) Ease of use and simple syntax make it the first choice for beginners. 2) Closely integrated with web development, excellent interaction with HTTP requests and database. 3) The huge ecosystem provides a wealth of tools and libraries. 4) Active community and open source nature adapts them to new needs and technology trends.

PHP and Python are both high-level programming languages that are widely used in web development, data processing and automation tasks. 1.PHP is often used to build dynamic websites and content management systems, while Python is often used to build web frameworks and data science. 2.PHP uses echo to output content, Python uses print. 3. Both support object-oriented programming, but the syntax and keywords are different. 4. PHP supports weak type conversion, while Python is more stringent. 5. PHP performance optimization includes using OPcache and asynchronous programming, while Python uses cProfile and asynchronous programming.

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

PHP originated in 1994 and was developed by RasmusLerdorf. It was originally used to track website visitors and gradually evolved into a server-side scripting language and was widely used in web development. Python was developed by Guidovan Rossum in the late 1980s and was first released in 1991. It emphasizes code readability and simplicity, and is suitable for scientific computing, data analysis and other fields.

PHP is suitable for web development and rapid prototyping, and Python is suitable for data science and machine learning. 1.PHP is used for dynamic web development, with simple syntax and suitable for rapid development. 2. Python has concise syntax, is suitable for multiple fields, and has a strong library ecosystem.

PHP remains important in the modernization process because it supports a large number of websites and applications and adapts to development needs through frameworks. 1.PHP7 improves performance and introduces new features. 2. Modern frameworks such as Laravel, Symfony and CodeIgniter simplify development and improve code quality. 3. Performance optimization and best practices further improve application efficiency.

PHPhassignificantlyimpactedwebdevelopmentandextendsbeyondit.1)ItpowersmajorplatformslikeWordPressandexcelsindatabaseinteractions.2)PHP'sadaptabilityallowsittoscaleforlargeapplicationsusingframeworkslikeLaravel.3)Beyondweb,PHPisusedincommand-linescrip

PHP type prompts to improve code quality and readability. 1) Scalar type tips: Since PHP7.0, basic data types are allowed to be specified in function parameters, such as int, float, etc. 2) Return type prompt: Ensure the consistency of the function return value type. 3) Union type prompt: Since PHP8.0, multiple types are allowed to be specified in function parameters or return values. 4) Nullable type prompt: Allows to include null values and handle functions that may return null values.


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

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

Hot Article

Hot Tools

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

Dreamweaver Mac version
Visual web development tools

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

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

WebStorm Mac version
Useful JavaScript development tools