简体   繁体   English

如何在php中将文件从一台服务器写入另一台服务器?

[英]How can I write a file from one server to another server in php?

Is it possible to write a file from on server to another server? 是否可以将服务器上的文件写入另一台服务器?

I have 2 domains both are on diffrent web servers 我有2个域都在不同的Web服务器上

http://example1.com is on godady and http://example2.com is on a free webhost eu5.org , http://example1.comgodady上http://example2.com在免费的webhost eu5.org上

I want to write htaccess file of http://example1.com from http://example2.com . 我想从http://example2.comhttp://example1.com的 htaccess文件。

my code in example2.com 我在example2.com中的代码

$code =$_POST["cd"];

if (empty($code)) {
    echo "Could not insert data!";
} else {
    $file = fopen("http://server2.com/.htaccess", "w");

    echo fwrite($file, $code);
    fclose($file);
}

It does'nt rewrite the .htaccess file and no php error. 它没有重写.htaccess文件,也没有php错误。 file permision of example1/.htaccess is 777. example1 / .htaccess的文件permision是777。

Any idea? 任何想法?

The short answer is: 简短的回答是:

"Yes. It possible to write a file from on server to another server" “是的。可以将服务器上的文件写入另一台服务器”

To write any file to any server across the network, you need to: 要将任何文件写入网络中的任何服务器,您需要:

1) decide on a protocol (for example, ftp ), then 1)然后确定协议(例如, ftp

2) instantiate a "listener" on the host to accept client requests for that protocol (for example, installing WinSCP on your Windows host) 2)在主机上实例化一个“监听器”以接受客户端对该协议的请求(例如,在Windows主机上安装WinSCP

Since you're writing in PHP - and since PHP is already using a protocol (HTTP) and already has a "listener" (your web server) -then why not just write a PHP app to accept files for upload, and send files for download? 因为您正在使用PHP编写 - 并且因为PHP已经使用协议(HTTP)并且已经有一个“监听器”(您的Web服务器) - 所以为什么不只是编写一个PHP应用程序来接受上传文件,并发送文件下载?

Here's a simple example: 这是一个简单的例子:

Whatever you choose to do, make sure it's secure . 无论您选择做什么,请确保它是安全的 In particular, you should not be able to "re-write .htaccess", and you should not be able to read or write to any arbitrary directory. 特别是,您应该“重写.htaccess”,并且您应该能够读取或写入任何任意目录。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM