PHP file Upload implementation upload to another server

Source: Internet
Author: User
Tags create directory file upload ftp connection ftp file php file php file upload

The Ftp_connect () function establishes a new FTP connection.

If successful, returns a connection identity, or false.

Grammar


Host required. Specify the FTP server to connect to. Can be a domain name or an IP address.
The following should not end with a slash, and the front does not need to start with a ftp://.

Port Optional. Specify the port for the FTP server.

Timeout optional. Specify the timeout time for this FTP server. The default is 90 seconds.


Then a test was done locally on the desktop:

The code is as follows Copy Code
$ftp _server = "192.168.1.100";
$ftp _user_name = "Laohu";
$ftp _user_pass = "123456";
$conn _id = ftp_connect ($ftp _server) or Die ("couldn ' t connect to $ftp _server");
$file = ' test.txt ';
$remote _file = '/test/a.txt ';

Note: the "/test" here refers to the test folder under the FTP root directory, not the system directory

Test

The code is as follows Copy Code

$login _result = Ftp_login ($conn _id, $ftp _user_name, $ftp _user_pass);

if (Ftp_put ($conn _id, $remote _file, $file, ftp_binary)) {
echo "File move succeeded";
} else {
echo "Move Failed";
}
Ftp_close ($conn _id);

After running: File moved successfully.

Want is this effect, and then use the desktop to do the program server, upload attachments full ftp method upload to the notebook, notebook IP is 105, phase

The code should read as follows:

  code is as follows copy code

if (Is_uploaded_file ($_files[' uploadfile '] [' tmp_name '])) {
$ftp _server = "192.168.1.105";
$ftp _user_name = "Lesley";
$ftp _user_pass = "123456";
$conn _id = ftp_connect ($ftp _server) or Die ("couldn ' t connect to $ftp _server");
$file = $_files[' UploadFile '] [' tmp_name '];
$remote _file = '/test/'. $_files[' uploadfile ' [' name '];
$login _result = Ftp_login ($conn _id, $ftp _user_name, $ftp _user_pass);

if (Ftp_put ($conn _id, $remote _file, $file, ftp_binary)) {
echo "File:". $_files[' UploadFile ' [' Name ']. " Upload Success ";
} else {
echo "Upload failed";
}
Ftp_close ($conn _id);
}

The corresponding foreground page code:

The code is as follows Copy Code

<form action= "uploadfile.php" method= "post" enctype= "Multipart/form-data" >
<input type= "File" Name= "UploadFile" id= "UploadFile"/>
<input type= "Submit" name= "submit" value= "Submit"/>
</form>

Did succeed after running.

Below look at an FTP file upload class

The code is as follows Copy Code

<?php
/**
* Desc:ftp Class
* Link:www.111cn.net
* DATE:2013/02/24
*/
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--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); Turn on Passive impersonation
}

/**
* 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 the path is correct!" ";
}

/**
* Method: Moving files
* @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! ";
}

/**
* Methods: Copying files
* Description: Because FTP does not copy the command, this method workaround: 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 replication failed, check that the permissions and the original path are 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 that the permissions and path are correct! ";
}

/**
* Method: Build Directory
* @path--Path
*/
function Dir_mkdirs ($path)
{
$path _arr = explode ('/', $path); Take an array of directories
$file _name = Array_pop ($path _arr);//Pop-up file name
$path _div = 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, please check the permissions and the path is correct! ";
Exit
}
@ftp_chdir ($this->conn_id, $val);
}
}

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

/**
* Method: Turn off FTP connection
*/
function Close ()
{
@ftp_close ($this->conn_id);
}
}
Class Class_ftp End
?>

Calling code

The code is as follows Copy Code

?
/***
* Desc: call Example
* Link:www.111cn.net
* DATE:2013/2/24
*/
$FTP = new ftp (' 192.168.0.249 ', ' Hlj ', ' 123456 '); Open an FTP connection
$ftp->up_file (' aa.wav ', ' test/13548957217/bb.wav '); Uploading files
$ftp->move_file (' aaa/aaa.php ', ' aaa.php ');/move File
$ftp->copy_file (' aaa.php ', ' aaa/aaa.php ');//Copy file
$ftp->del_file (' aaa.php '); deleting files
$ftp->close (); Turn off FTP connections
?>

Note that when using the Ftp_put method, the fourth parameter transfer mode needs to be ftp_binary (binary mode), with FTP_ASCII (text mode)

When the picture can be uploaded but can not be displayed, other files renamed, Chinese garbled solution, upload rights control, and so on, not mentioned here.

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.