search
HomeBackend DevelopmentPHP TutorialPHP's FTP operation class (copy, move, delete files, create directories)

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

Detailed explanation of CURL
curl_close — Close a curl session
curl_copy_handle — Copy all contents and parameters of a curl connection resource
curl_errno — Return a numeric number containing the error information of the current session
curl_error — Return a string containing the error information of the current session
curl_exec — Execute a curl session
curl_getinfo — Get information about a curl connection resource handle
curl_init — Initialize a curl session

curl_multi_add_handle — Add a separate curl handle resource to a curl batch session
curl_multi_close — Close a batch handle resource
curl_multi_exec — Parse a curl batch handle
curl_multi_getcontent — Return a text stream of the fetched output
curl_multi_info_read — Get the currently parsed Curl related transmission information
curl_multi_init — Initialize a curl batch handle resource
curl_multi_remove_handle — Remove a handle resource in the curl batch handle resource
curl_multi_select — Get all the sockets associated with the cURL extension, which can then be "selected "
curl_setopt_array — Set session parameters for a curl in the form of an array
curl_setopt — Set session parameters for a curl
curl_version — Get curl-related version information
The role of the curl_init() function is to initialize a curl session. The curl_init() function is unique One parameter is optional and represents a url address.
The curl_exec() function is used to execute a curl session. The only parameter is the handle returned by the curl_init() function.
The curl_close() function is used to close a curl session. The only parameter is the handle returned by the curl_init() function.

  1. $url = 'http://www.@@@@@@.com/';
  2. //Initialize curl
  3. $curl = curl_init($url);
  4. //curl Timeout 30s
  5. curl_setopt($curl, CURLOPT_TIMEOUT, '30');
  6. //user-agent header
  7. curl_setopt($curl, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:14.0) Gecko/20120722 Firefox/14.0.1");
  8. //Return to file stream
  9. curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE);
  10. //Open header file data stream output
  11. curl_setopt($curl, CURLOPT_HEADER, 1);
  12. $string = curl_exec ($curl);
  13. var_dump($string);
  14. preg_match_all('/Set-Cookie:stest=(.*)/i', $string, $results);
  15. var_dump($results);
  16. ?>
Copy code

PHP, FTP


Statement
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
The Continued Use of PHP: Reasons for Its EnduranceThe Continued Use of PHP: Reasons for Its EnduranceApr 19, 2025 am 12:23 AM

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: Exploring Their Similarities and DifferencesPHP and Python: Exploring Their Similarities and DifferencesApr 19, 2025 am 12:21 AM

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 and Python: Different Paradigms ExplainedPHP and Python: Different Paradigms ExplainedApr 18, 2025 am 12:26 AM

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 and Python: A Deep Dive into Their HistoryPHP and Python: A Deep Dive into Their HistoryApr 18, 2025 am 12:25 AM

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.

Choosing Between PHP and Python: A GuideChoosing Between PHP and Python: A GuideApr 18, 2025 am 12:24 AM

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 and Frameworks: Modernizing the LanguagePHP and Frameworks: Modernizing the LanguageApr 18, 2025 am 12:14 AM

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.

PHP's Impact: Web Development and BeyondPHP's Impact: Web Development and BeyondApr 18, 2025 am 12:10 AM

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

How does PHP type hinting work, including scalar types, return types, union types, and nullable types?How does PHP type hinting work, including scalar types, return types, union types, and nullable types?Apr 17, 2025 am 12:25 AM

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.

See all articles

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

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)