Phpftp remote file Upload

Source: Internet
Author: User
Tags ftp connection
& Amp; lt ;? Php *** function: FTP operation class (copy, move, delete file creation directory) * remote file Upload * Time: 2011216 * author: Liu Meng ** classclass_ftp {public $ off; return operation status (failed) public $ conn_id; FTP connection *** method: FTP connection * @ F... /**
* Function: FTP operations (copying, moving, and deleting files/creating directories)
* Remote file Upload
* Duration: 2011216
* Author: Liu Meng
*
*/
Class class_ftp
{
Public $ off; // return the operation status (successful/failed)
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); // enable passive simulation
}

/**
* Method: upload a file.
* @ Path -- local path
* @ Newpath -- Upload path
* @ Type -- New if the target directory does not exist
*/
Function up_file ($ path, $ newpath, $ type = false)
{

If ($ type) $ this-> dir_mkdirs ($ newpath );
$ Thisds-> off = @ ftp_put ($ this-> conn_id, $ newpath, $ path, FTP_BINARY );
If (! $ This-> off) echo "file Upload failed. check whether the permission and path are correct! ";
}

/**
* Method: move a file
* @ Path -- 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 "failed to move the file. check whether the permission and original path are correct! ";
}

/**
* Method: copy an object
* Note: Because FTP does not have a copy command, the work und of this method is to download the file and upload it to a new path.
* @ Path -- 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 "failed to copy the file. check whether the permission and original path are correct! ";
$ This-> up_file ($ downpath, $ newpath, $ type );
}

/**
* Method: delete an object.
* @ Path -- path
*/
Function del_file ($ path)
{
$ This-> off = @ ftp_delete ($ this-> conn_id, $ path );
If (! $ This-> off) echo "an error occurred while deleting the file. check whether the permission and path are correct! ";
}

/**
* Method: generate a directory
* @ Path -- path
*/
Function dir_mkdirs ($ path)
{
$ Path_arr = explode ('/', $ path); // Retrieve the directory array
$ File_name = array_pop ($ path_arr); // the file name is displayed.
$ Path_p = 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 whether the permission and path are correct! ";
Exit;
}
@ Ftp_chdir ($ this-> conn_id, $ val );
}
}
For ($ I = 1; $ I <= $ path_p; $ I ++) // roll back to the root
{
@ Ftp_cdup ($ this-> conn_id );
}
}

/**
* Method: disable the FTP connection.
*/
Function close ()
{
@ Ftp_close ($ this-> conn_id );
}
}
?>
// Usage
$ Ftp = new class_ftp ('2017. 15.0.46 ', 21, 'zengzhi', 'aipu2010 ');
$ Ftp-> up_file ($ _ FILES ['Game _ src'] ['tmp _ name'], '/G /'. $ _ FILES ['Game _ src'] ['name']);
$ Ftp-> close ();
$ Src_img = 'http: // 172.15.0.46/G/'. $ _ FILES ['Game _ src'] ['name'];

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.