FTP operation class (copy, move, delete file creation directory) class class_ftp{ public $off; Return operation status (successful failure) public $conn
- /**
- * Function: FTP operations (copy, move, delete files/create directories)
- * QQ communication group: 136112330
- */
- class class_ftp
- {
- public $off; // Return operation status (success/failure)
- public $conn_id; // FTP connection
- /**
- * Method: FTP connection
- * @FTP_HOST -- FTP host
- * @FTP_PORT -- Port
- * @FTP_USER -- Username
- * @FTP_PASS -- Password
- */
- function __construct($FTP_HOST,$FTP_PORT,$FTP_USER,$FTP_PASS )
- {
- $this->conn_id = @ftp_connect($FTP_HOST,$FTP_PORT) or die("FTP server connection failed");
- @ftp_login($this->conn_id,$FTP_USER,$FTP_PASS) or die ("FTP server login failed");
- @ftp_pasv($this->conn_id,1); // Turn on passive simulation
- }
- /**
- * Method: Upload file
- * @path -- local path
- * @newpath -- upload path
- * @type -- create a new directory if it does not exist
- */
- 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 "File upload failed, please check whether the permissions and path are correct!";
- }
- /**
- * Method: Move files
- * @path -- original path
- * @newpath -- new path
- * @type -- create a new directory if it does not exist
- */
- 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 "File move failed, please check whether the permissions and original path are correct! ";
- }
- /**
- * Method: Copy files
- * Note: Since FTP does not have a copy command, the alternative operation of this method is: download and then upload to a new path
- * @path -- original path
- * @newpath -- new path
- * @type -- If the target directory does not exist, create it
- */
- function copy_file($path,$newpath,$type=true)
- {
- $downpath = "c:/tmp.dat";
- $this->off = @ftp_get($this->conn_id,$downpath,$path,FTP_BINARY);// Download
- if(!$this->off) echo "File copy failed, please check whether the permissions and original path are correct! ";
- $this->up_file($downpath,$newpath,$type);
- }
- /**
- * Method: delete file
- * @path -- path
- */
- function del_file($path)
- {
- $this->off = @ftp_delete ($this->conn_id,$path);
- if(!$this->off) echo "File deletion failed, please check whether the permissions and path are correct! ";
- }
- /**
- * Method: Generate directory
- * @path -- path
- */
- function dir_mkdirs($path)
- {
- $path_arr = explode('/',$path); // Get directory array
- $file_name = array_pop($path_arr) ; // Pop up the file name
- $path_div = count($path_arr); // Get the number of layers
- foreach($path_arr as $val) // Create a directory
- {
- if(@ftp_chdir($this->conn_id,$ val) == FALSE)
- {
- $tmp = @ftp_mkdir($this->conn_id,$val);
- if($tmp == FALSE)
- {
- echo "Directory creation failed, please check the permissions and path correct!";
- exit;
- }
- @ftp_chdir($this->conn_id,$val);
- }
- }
- for($i=1;$i=$path_div;$i++) // Roll back to root
- {
- @ftp_cdup($this->conn_id);
- }
- }
- /**
- *Method: Close FTP connection
- */
- function close()
- {
- @ftp_close($this->conn_id);
- }
- }/ / class class_ftp end
- /************************************** test************* **********************
- $ftp = new class_ftp('192.168.100.143',21,'user','pwd'); // Open FTP Connect
- //$ftp->up_file('aa.txt','a/b/c/cc.txt'); // Upload files
- //$ftp->move_file('a/b/c /cc.txt','a/cc.txt'); // Move files
- //$ftp->copy_file('a/cc.txt','a/b/dd.txt'); // Copy file
- //$ftp->del_file('a/b/dd.txt'); // Delete file
- $ftp->close(); // Close FTP connection
- ******* *************************************************** **********************/
- ?>
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.
- $url = 'http://www.@@@@@@.com/';
- //Initialize curl
- $curl = curl_init($url);
- //curl Timeout 30s
- curl_setopt($curl, CURLOPT_TIMEOUT, '30');
- //user-agent header
- curl_setopt($curl, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:14.0) Gecko/20120722 Firefox/14.0.1");
- //Return to file stream
- curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE);
- //Open header file data stream output
- curl_setopt($curl, CURLOPT_HEADER, 1);
- $string = curl_exec ($curl);
- var_dump($string);
- preg_match_all('/Set-Cookie:stest=(.*)/i', $string, $results);
- var_dump($results);
- ?>
Copy code
|