Home Backend Development PHP Tutorial FTP file upload class written in php

FTP file upload class written in php

Jul 25, 2016 am 09:04 AM

  1. /**
  2. * desc:FTP类
  3. * link:bbs.it-home.org
  4. * date:2013/02/24
  5. */
  6. class ftp
  7. {
  8. public $off; // Return operation status (success/failure)
  9. public $conn_id; // FTP connection
  10. / **
  11. * Method: FTP connection
  12. * @FTP_HOST -- FTP host
  13. * @FTP_PORT -- Port
  14. * @FTP_USER -- Username
  15. * @FTP_PASS -- Password
  16. */
  17. function __construct($FTP_HOST,$FTP_PORT,$FTP_USER,$FTP_PASS)
  18. {
  19. $this->conn_id = @ftp_connect($FTP_HOST,$FTP_PORT) or die("FTP server connection failed ");
  20. @ftp_login($this->conn_id,$FTP_USER,$FTP_PASS) or die("FTP server login failed");
  21. @ftp_pasv($this->conn_id,1); // Turn on passive simulation
  22. }
  23. /**
  24. * Method: Upload file
  25. * @path-- local path
  26. * @newpath -- upload path
  27. * @type-- create a new directory if it does not exist
  28. */
  29. function up_file($path,$newpath,$type=true)
  30. {
  31. if($type) $this->dir_mkdirs($newpath);
  32. $this- >off = @ftp_put($this->conn_id,$newpath,$path,FTP_BINARY);
  33. if(!$this->off) echo "File upload failed, please check whether the permissions and path are correct!";
  34. }
  35. /**
  36. * Method: Move files
  37. * @path-- original path
  38. * @newpath -- new path
  39. * @type-- create a new directory if it does not exist
  40. */
  41. function move_file($path,$newpath,$type=true)
  42. {
  43. if($type) $this->dir_mkdirs($newpath);
  44. $this- >off = @ftp_rename($this->conn_id,$path,$newpath);
  45. if(!$this->off) echo "File move failed, please check whether the permissions and original path are correct!";
  46. }
  47. /**
  48. * Method: Copy files
  49. * Note: Since FTP does not have a copy command, the alternative operation of this method is: download and then upload to a new path
  50. * @path-- original path
  51. * @newpath -- new path
  52. * @type -- If the target directory does not exist, create it
  53. */
  54. function copy_file($path,$newpath,$type=true)
  55. {
  56. $downpath = "c:/tmp.dat";
  57. $this->off = @ ftp_get($this->conn_id,$downpath,$path,FTP_BINARY);// Download
  58. if(!$this->off) echo "File copy failed, please check whether the permissions and original path are correct! ";
  59. $this->up_file($downpath,$newpath,$type);
  60. }
  61. /**
  62. * Method: delete file
  63. * @path -- path
  64. */
  65. function del_file($path)
  66. {
  67. $this->off = @ ftp_delete($this->conn_id,$path);
  68. if(!$this->off) echo "File deletion failed, please check whether the permissions and path are correct! ";
  69. }
  70. /**
  71. * Method: Generate directory
  72. * @path -- path
  73. */
  74. function dir_mkdirs($path)
  75. {
  76. $path_arr = explode('/',$path); // Get directory array
  77. $file_name = array_pop($path_arr );// Pop up file name
  78. $path_div = count($path_arr);// Get the number of layers
  79. foreach($path_arr as $val)// Create directory
  80. {
  81. if(@ftp_chdir($this->conn_id ,$val) == FALSE)
  82. {
  83. $tmp = @ftp_mkdir($this->conn_id,$val);
  84. if($tmp == FALSE)
  85. {
  86. echo "Directory creation failed, please check permissions and Is the path correct? ";
  87. exit;
  88. }
  89. @ftp_chdir($this->conn_id,$val);
  90. }
  91. }
  92. for($i=1;$i<=$path_div;$i++) // Fallback to root
  93. {
  94. @ftp_cdup($this->conn_id);
  95. }
  96. }
  97. /**
  98. * Method: Close FTP connection
  99. */
  100. function close()
  101. {
  102. @ftp_close($this->conn_id);
  103. }
  104. }
  105. // class class_ftp end
  106. ?>
Copy code

Call example:

  1. /***
  2. * desc: calling example
  3. * link: bbs.it-home.org
  4. * date: 2013/2/24
  5. */
  6. $ftp = new ftp('192.168.0.249',21,'hlj','123456'); // Open FTP connection
  7. $ftp ->up_file('aa.wav','test/13548957217/bb.wav'); //Upload file
  8. //$ftp->move_file('aaa/aaa.php','aaa.php') ;//Move files
  9. //$ftp->copy_file('aaa.php','aaa/aaa.php');// Copy files
  10. //$ftp->del_file('aaa.php') ; // Delete file
  11. $ftp->close(); // Close FTP connection
  12. ?>
Copy code


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)
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Chat Commands and How to Use Them
4 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)

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,

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

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.

How to send a POST request containing JSON data using PHP's cURL library? How to send a POST request containing JSON data using PHP's cURL library? Apr 01, 2025 pm 03:12 PM

Sending JSON data using PHP's cURL library In PHP development, it is often necessary to interact with external APIs. One of the common ways is to use cURL library to send POST�...

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.

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.

What exactly is the non-blocking feature of ReactPHP? How to handle its blocking I/O operations? What exactly is the non-blocking feature of ReactPHP? How to handle its blocking I/O operations? Apr 01, 2025 pm 03:09 PM

An official introduction to the non-blocking feature of ReactPHP in-depth interpretation of ReactPHP's non-blocking feature has aroused many developers' questions: "ReactPHPisnon-blockingbydefault...

See all articles