PHP upload files via ftp detailed introduction _php tutorial

Source: Internet
Author: User
Tags ftp connection
This article summarizes several uses the FTP function in PHP to realize through the FTP uploads the file, has the need to study the friend to refer the reference.

The Ftp_get () function downloads a file from the FTP server.

Returns true if successful, or False if unsuccessful.

Grammar
Ftp_get (Ftp_connection,local,remote,mode,resume)


The Ftp_connect () function establishes a new FTP connection.

If successful, returns a connection ID, otherwise false.

Grammar
Ftp_connect (Host,port,timeout)


The Ftp_login () function logs on to the FTP server.

Returns true if successful, failure returns false and a warning is issued.

Grammar
Ftp_login (Ftp_connection,username,password)

Three functions have been introduced, and now begins.

Example 1

The code is as follows Copy Code

$ftp _server = "*.*.*.*";
$ftp _user = "Lu";
$ftp _pass = "Love You";

Set up a connection or die
$conn _id = ftp_connect ($ftp _server) or Die ("couldn-t connect to $ftp _server");

$login _result = Ftp_login ($conn _id, $ftp _user, $ftp _pass);

if ((! $conn _id) | | (! $login _result)) {
echo "FTP connection has failed!";
echo "Attempted to connect to $ftp _server for user $ftp _user_name";
Exit
} else {
echo "Connected to $ftp _server, for user $ftp _user_name";
}

Try to login
$filename =date (' Ymd '). ". XML ";
$source _file= "/usr/local/ivr/sendwireless/xml/data/". $filename; Source Address
echo $source _file;
$destination _file= "/itc/admin/logstat/ftplog/". $filename; Destination Address
$upload = Ftp_put ($conn _id, $destination _file, $source _file, ftp_binary) or Die ("couldn ' t connect to $ftp _server");
Ftp_quit ($conn _id);

if (! $upload) {
echo "FTP upload has failed!";
} else {
echo "uploaded $source _file to $ftp _server as $destination _file";
}
Ftp_close ($conn _id);


Upload to the local to make necessary changes to the file, such as Watermark, and so on, and then through the FTP to the remote server.

Example 2:

The code is as follows Copy Code

Upload image

if ($_files[' pic ' [' name '])
{
$file _path= '/opt/www/img/';
$pic = Upload (' pic ', $filename, ' jpg|jpeg|gif|bmp|png ', $file _path);
if (! $pic)
{
echo "Image upload failed!";
Exit
}
Require_once (Root_path. ' lib/class/ftp.class.php ');
$FTP = new FTP ("127.0.0.1", "gamezeroftp", "123456", "/opt/www");
$localfile = '/opt/www/img/'. $pic;
$remotefile = '/opt/www/gamepics/'. $pic;
$ftpput = $ftp->put ($localfile, $remotefile); FTP upload original image to remote server
if (! $ftpput) {
echo "Upload image to remote server failed!";
}
$ftp->bye (); Close FTP connection
}
Attach the FTP operation class:


Ftpurl= $ftpUrl;
}
if ($ftpUser) {
$this->ftpuser= $ftpUser;
}
if ($ftpPass) {
$this->ftppass= $ftpPass;
}
if ($FTPURL) {
$this->ftpdir= $ftpDir;
}
if ($this->FTPR = Ftp_connect ($this->ftpurl, 21)) {
if (Ftp_login ($this->ftpr, $this->ftpuser, $this->ftppass)) {
if (!empty ($this->ftpdir)) {
Ftp_chdir ($this->ftpr, $this->ftpdir);
}
FTP_PASV ($this->FTPR, true);//r enable passive mode;
$status = 1;
} else {
$status = 3;
}
} else {
$status = 2;
}
}
R switch Directory;
function CD ($dir) {
Return Ftp_chdir ($this->FTPR, $dir);
}
R returns the current road strength;
function pwd () {
Return Ftp_pwd ($this->FTPR);
}
R Create a directory
function mkdir ($directory) {
Return Ftp_mkdir ($this->FTPR, $directory);
}
R Delete Directory
function RmDir ($directory) {
Return Ftp_rmdir ($this->FTPR, $directory);
}
R upload files;
function put ($localFile, $remoteFile = ") {
if ($remoteFile = = ") {
$remoteFile = End (Explode ('/', $localFile));
}
$res = Ftp_nb_put ($this->ftpr, $remoteFile, $localFile, ftp_binary);
while ($res = = Ftp_moredata) {
$res = ftp_nb_continue ($this->ftpr);
}
if ($res = = ftp_finished) {
return true;
} elseif ($res = = ftp_failed) {
return false;
}
}
R download file;
function Get ($remoteFile, $localFile = ") {
if ($localFile = = ") {
$localFile = End (Explode ('/', $remoteFile));
}
if (Ftp_get ($this->ftpr, $localFile, $remoteFile, ftp_binary)) {
$flag = true;
} else {
$flag = false;
}
return $flag;
}
R file size;
function Size ($file) {
Return Ftp_size ($this->FTPR, $file);
}
R file is present;
function Isfile ($file) {
if ($this->size ($file) >= 0) {
return true;
} else {
return false;
}
}
R file time
function FileTime ($file) {
Return Ftp_mdtm ($this->FTPR, $file);
}
R Delete files;
function unlink ($file) {
Return Ftp_delete ($this->FTPR, $file);
}
function nlist ($dir = '/service/resource/') {
Return Ftp_nlist ($this->FTPR, $dir);
}
R close connection;
function bye () {
Return Ftp_close ($this->FTPR);
}
}
?>


http://www.bkjia.com/PHPjc/445297.html www.bkjia.com true http://www.bkjia.com/PHPjc/445297.html techarticle This article summarizes several uses the FTP function in PHP to realize through the FTP uploads the file, has the need to study the friend to refer the reference. The Ftp_get () function downloads a file from the FTP server. ...

  • 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.