This article brings you the content is about PHP ftp upload and download the implementation code of the function, there is a certain reference value, there is a need for friends to refer to, I hope you have some help.
/** * Function: FTP operation class (copy, move, delete file/create directory) */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 */const FTP_ host= ' xxx.xx.xx.xxx '; Const ftp_port= ' 21 '; Const ftp_user= ' xx '; Const ftp_pass= ' xxxx '; function __construct () {$this->conn_id = @ftp_connect (Self::ftp_host,self::ftp_port) or Die ("FTP Server Connection failed"); @ftp_login ($this->conn_id,self::ftp_user,self::ftp_pass) or Die ("FTP server login failed"); @ftp_pasv ($this->conn_id,1); Open Passive Impersonation}/** * Method: Upload file * @path--Local Path * @newpath--Upload path * @type--If the target directory does not exist the new */FUNCTI On 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 File * @path--The original path * @newpath--New Path * @type-NEW */function Move_file ($path, $newpath, $type =true) {if ($type) $this->d If the target directory does not exist Ir_mkdirs ($newpath); $this->off = @ftp_rename ($this->conn_id, $path, $newpath); if (! $this->off) echo "File move failed, please check the permissions and the original path is correct!" "; }/** * Method: Copy File * Description: Due to FTP no copy command, this method is: Download and then upload to the new path * @path-the original path * @newpath-New path * @type- The standard directory does not exist then new */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 permissions and The original path is correct! "; $this->up_file ($downpath, $newpath, $type); }/** * Method: Delete file * @path--Path */function Del_file ($path) {$this->off = @ftp_delete ($this- >conn_id, $path); return $this->off; }/** * Method: Generate 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 _p = count ($path _arr); Number of layers foreach ($path _arr as $val)//Create directory {if (@ftp_chdir ($this->conn_id, $val) = = FALSE) {$tmp = @ftp_mkdir ($this->conn_id, $val); if ($tmp = = FALSE) {echo ' Directory creation failed, check the permissions and path are correct! "; Exit } @ftp_chdir ($this->conn_id, $val); }} for ($i =1; $i <= $path _p; $i + +)//fallback to root {@ftp_cdup ($this->conn_id); }/* * File Download */function Download ($select _file) {$tmpfile = Tempnam (GETCWD (). " /"," temp "); Create a unique temporary file if (Ftp_get ($this->conn_id, $tmpfile, $select _file, ftp_binary)) {//download the specified file to a temporary file $th Is->close (); Close connection $file = explode ('/', $select _file); $file = End ($file); Header ("Content-type:application/octet-strEam "); Header ("content-disposition:attachment; Filename= ". $file);//content-disposition:inline; Indicates that the file can be opened online! ReadFile ($tmpfile); Unlink ($tmpfile); Delete temporary file exit; } unlink ($tmpfile); }/** * Method: Close FTP connection */function Close () {@ftp_close ($this->conn_id); }}