Home  >  Article  >  Backend Development  >  How to upload files to remote server in php

How to upload files to remote server in php

高洛峰
高洛峰Original
2016-11-28 16:50:323263browse

Sometimes in order to achieve load or other reasons, we need to place the uploaded files on other servers. Since I am not familiar with the tools on the server, I only start with the PHP program and use PHP's ftp or curl. To upload files to a remote server

Of course, both methods need to upload to the current web server first, and then transfer to other servers

The first method is to upload to the remote server via ftp, which requires a remote The server's ftp IP address, ftp user and ftp password are in the directory location allowed by permissions
The ftp class provided by www.yawill.com is used. Please see the attachment for the specific usage of this class
[php]
require " ./ftp.class.php";
$a = "";//Local path
$b = "";//Remote file name
$ftp = new ClsFTP("username","password",'202.202 .202.202');//Modify the settings by yourself
$ftp->cd('web');//Change to the directory. If you need to upload to the root directory, there is no need to change it
$ftp->put($b ,$a."/".$b);//put file
$ftp->close();
?>
[/php]


2. Another method is to use curl to submit
This is not much different from ordinary PHP uploads in processing uploads, but it needs to be prevented from malicious uploads by others. The source program is the work of other netizens. Unfortunately, the URL can no longer be opened
[php]
echo "< ;pre>";
print_r($_POST);
print_r($_FILES);
echo "";
//Here is the upload processing of the remote server
if(move_uploaded_file($_FILES['file1' ]['tmp_name'][1],'./test.txt')){
  echo 'ok';
}
?>
[/php]


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn