PHP FTP Operation class (copy, move, delete files to create directory)

Source: Internet
Author: User
Tags file copy ftp connection
FTP operation class (copy, move, delete file creation directory) class class_ftp{public $off; return operation status (failed successfully) public $conn
  1. /**
  2. * Role: FTP operation class (copy, move, delete files/create directory)
  3. * QQ Exchange 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-User name
  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-New If the target directory 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 the permissions and path is correct!" ";
  33. }
  34. /**
  35. * Method: Move File
  36. * @path-The original path
  37. * @newpath-New Path
  38. * @type-New If the target directory 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 the permissions and the original path is correct!" ";
  45. }
  46. /**
  47. * Method: Copy File
  48. * Description: Due to FTP no copy command, this workaround is: Download and upload to the new path
  49. * @path-The original path
  50. * @newpath-New Path
  51. * @type-New If the target directory does not exist
  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 the permissions and the original path is 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 the permissions and path is correct!" ";
  68. }
  69. /**
  70. * Method: Generate Directory
  71. * @path--Path
  72. */
  73. function Dir_mkdirs ($path)
  74. {
  75. $path _arr = explode ('/', $path); Fetch directory Array
  76. $file _name = Array_pop ($path _arr); Popup file name
  77. $path _div = count ($path _arr); Number of layers to be taken
  78. foreach ($path _arr as $val)//Create 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 is correct!" ";
  86. Exit
  87. }
  88. @ftp_chdir ($this->conn_id, $val);
  89. }
  90. }
  91. for ($i =1; $i = $path _div; $i + +)//fallback 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 ', +, ' user ', ' pwd '); Open FTP Connection
  106. $ftp->up_file (' aa.txt ', ' a/b/c/cc.txt '); Uploading files
  107. $ftp->move_file (' a/b/c/cc.txt ', ' a/cc.txt '); Moving files
  108. $ftp->copy_file (' a/cc.txt ', ' a/b/dd.txt '); Copying files
  109. $ftp->del_file (' a/b/dd.txt '); deleting files
  110. $ftp->close (); Close FTP connection
  111. ******************************************************************************/
  112. ?>
Copy Code

Curl Detailed
curl_close-closing a Curl session
curl_copy_handle-Copy all the contents and parameters of a Curl connection resource
curl_errno-returns a numeric number that contains the current session error information
curl_error-returns a string containing the current session error message
Curl_exec-performing a Curl session
Curl_getinfo-gets the information for a Curl connection resource handle
curl_init-initialization of 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- Resolves a curl batch handle
curl_multi_getcontent-returns the text stream of the obtained output
curl_multi_info_read-gets the associated transport information for the currently resolved Curl
Curl_multi_ init-Initializes a curl batch handle resource
Curl_multi_remove_handle-removes a handle resource from the Curl batch handle resource
Curl_multi_select-get all the sockets a Ssociated with the curl extension, which can then be "selected"
curl_setopt_array-set session parameters as an array for a CURL
Curl_setop T-set session parameters for a Curl
curl_version-get Curl-related version information
The function of the Curl_init () Initializes a curl session, and the Curl_init () function is the only parameter that is optional, Represents a URL address. The function of the
Curl_exec () function is to perform a curl session, and the only argument is the handle returned by the Curl_init () function. The function of the
Curl_close () function is to close a curl session, and the only argument 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 Head
    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 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
  • Related Article

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.