Example Analysis of FTP Operation Class in PHP
This article mainly introduces the example analysis of the FTP operation class in PHP, which has a certain reference value, and interested friends can refer to it. I hope you will gain a lot after reading this article.
Click (here) to collapse or open
/ * *
* function: FTP operation class (copy, move, delete files / create directories)
, /
Class class_ftp {
Public $off; / / returns the operation status (success / failure)
Public $conn_id; / / FTP connection
/ * *
* method: FTP connection
* @ FTP_HOST-- FTP host
* @ FTP_PORT-Port
* @ FTP_USER-user name
* @ 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); / / Open passive simulation
}
/ * *
* method: upload files
* @ path-- Local path
* @ newpath-upload path
* @ type-- create a new directory if the destination directory 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 the permissions and path is correct!"
}
/ * *
* method: move files
* @ path-- original path
* @ newpath-- New path
* @ type-- create a new directory if the destination directory 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 movement failed, please check the permissions and the original path is correct!"
}
/ * *
* method: copy files
* Note: since there is no copy command in FTP, the flexible operation of this method is to download and upload to a new path.
* @ path-- original path
* @ newpath-- New path
* @ type-- create a new directory if the destination directory does not exist
, /
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 the permissions and the original path is correct!"
$this- > up_file ($downpath,$newpath,$type)
}
/ * *
* method: delete files
* @ path-- path
, /
Function del_file ($path) {
$this- > off = @ ftp_delete ($this- > conn_id,$path)
If (! $this- > off) echo "failed to delete the file, please check the permissions and path is correct!"
}
/ * *
* method: generate a directory
* @ path-- path
, /
Function dir_mkdirs ($path) {
$path_arr = explode ('/', $path); / / fetch directory array
$file_name = array_pop ($path_arr); / / Pop-up file name
$path_div = count ($path_arr); / / 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 is correct!"
Exit
}
@ ftp_chdir ($this- > conn_id,$val)
}
}
For ($ifoliance 1 / return to root) / / fallback to root
{
@ ftp_cdup ($this- > conn_id)
}
}
/ * *
* method: close the FTP connection
, /
Function close () {
@ ftp_close ($this- > conn_id)
}
} / / class class_ftp end
/ * * Test * *
$ftp = new class_ftp ('192.168.100.143 pwd`); / / Open the FTP connection
/ / $ftp- > up_file ('aa.txt','a/b/c/cc.txt'); / / upload files
/ / $ftp- > move_file ('a _
/ / $ftp- > copy_file ('a pictur cc.txt'); / / copy the file
/ / $ftp- > del_file ('a _
$ftp- > close (); / / close the FTP connection
* * /
? >
Thank you for reading this article carefully. I hope the article "sample Analysis of FTP Operation classes in PHP" shared by the editor will be helpful to you. At the same time, I also hope you will support us and pay attention to the industry information channel. More related knowledge is waiting for you to learn!