PHP FTP Learning Notes

Source: Internet
Author: User
Tags file copy ftp connection remote ftp server
Ftp_alloc () Allocates space for the file to upload to the FTP server.
Ftp_cdup () changes the current directory to the parent directory on the FTP server.
Ftp_chdir () changes the current directory on the FTP server.
Ftp_chmod () Sets permissions on the file via FTP.
Ftp_close () closes the FTP connection.
Ftp_connect () Open the FTP connection.
Ftp_delete () Delete the files on the FTP server.
Ftp_exec () executes a program/command on FTP.
Ftp_fget () Download a file from the FTP server and save it to a local file that is already open
Ftp_fput () upload an open file and save it as a file on the FTP server
Ftp_get_option () returns the various options settings for the current FTP connection.
Ftp_get () Download the file from the FTP server.
Ftp_login () Log on to the FTP server.
Ftp_mdtm () returns the last modified time of the specified file.
Ftp_mkdir () Creates a new directory on the FTP server.
Ftp_nb_continue () continuously gets/sends the file (non-blocking).
Ftp_nb_fget ()
Download the file from the FTP server and save it to a file that is already open locally (non-blocking)
Ftp_nb_fput ()
Upload the Open file and save it as a file (non-blocking) on the FTP server.
Ftp_nb_get () Download the file (non-blocking) from the FTP server.
Ftp_nb_put () uploads the file to the server (non-blocking).
Ftp_nlist () Returns a list of files for the specified directory.
FTP_PASV () returns whether the current FTP passive mode is turned on.
Ftp_put () upload the file to the server.
FTP_PWD () returns the current directory name.
Alias of Ftp_quit () Ftp_close ().
Ftp_raw () sends a raw command to the FTP server.
Ftp_rawlist () Returns a detailed list of the files in the specified directory.
Ftp_rename () renames the file or directory on the FTP server.
Ftp_rmdir () deletes the directory on the FTP server.
Ftp_set_option () sets various FTP run-time options.
Ftp_site () sends the site command to the server.
Ftp_size () returns the size of the specified file.
Ftp_ssl_connect () to open a secure ssl-ftp connection.
Ftp_systype () returns the system type identifier of the remote FTP server.


Class Ftputil {

Public $off; Return 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 File
* @path--Local path
* @newPath--Upload path
* @type-New If the target 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 File
* @path-The original path
* @newPath-New Path
* @type-New If the target 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 move failed, please check the permissions and the original path is correct!" ";
}
}

/**
* Method: Copy File
* Description: Due to FTP no copy command, this workaround is: Download and upload to the new path
* @path-The original path
* @newPath-New Path
* @type-New If the target 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 File
* @path--Path
*/
function Del_file ($path) {
$this->off = @ftp_delete ($this->conn_id, $path);
if (! $this->off) {
echo "File deletion failed, please check the permissions and path is correct!" ";
}
}

/**
* Method: Generate Directory
* @path--Path
*/
function Dir_mkdirs ($path) {
$path _arr = explode ('/', $path); Fetch directory Array
$file _name = Array_pop ($path _arr); Popup file name
$path _div = count ($path _arr); Number of layers to be taken

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, please check the permissions and path is correct!" ";
Exit
}
@ftp_chdir ($this->conn_id, $val);
}
}

for ($i = 1; $i <= $path _div; $i + +) {//fallback to root
@ftp_cdup ($this->conn_id);
}
}

/**
* Method: Close FTP connection
*/
function Close () {
@ftp_close ($this->conn_id);
}
  • 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.