php 从服务器到服务器的http传输文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2578128/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 06:59:14  来源:igfitidea点击:

http transfer file from server to server

phphtmlfile-upload

提问by qualle

with html forms we can upload a file from a client to a server with enctype="multipart/form-data", input type="file"and so on.

使用 html 表单,我们可以使用enctype="multipart/form-data"、输入type="file"等将文件从客户端上传到服务器。

Is there a way to have a file already ON the server and transfer it to another server the same way?

有没有办法让文件已经在服务器上并以相同的方式将其传输到另一台服务器?

Thanks for hints.

谢谢你的提示。

// WoW! This is the fastest question answering page i have ever seen!!

// 哇!这是我见过最快的问答页面!!

回答by Pascal MARTIN

When the browser is uploading a file to the server, it sends an HTTP POST request, that contains the file's content.

当浏览器将文件上传到服务器时,它会发送一个包含文件内容的 HTTP POST 请求。

You'll have to replicate that.

你必须复制它。


With PHP, the simplest (or, at least, most used)solution is probably to work with curl.


对于 PHP,最简单(或至少最常用)的解决方案可能是使用curl.

If you take a look at the list of options you can set with curl_setopt, you'll see this one : CURLOPT_POSTFIELDS(quoting):

如果您查看可以使用 设置的选项列表curl_setopt,您会看到这个:(引用)CURLOPT_POSTFIELDS

The full data to post in a HTTP "POST" operation.
To post a file, prepend a filename with @ and use the full path.
This can either be passed as a urlencoded string like 'para1=val1&para2=val2&...' or as an array with the field name as key and field data as value.
If value is an array, the Content-Type header will be set to multipart/form-data.

要在 HTTP“POST”操作中发布的完整数据。
要发布文件,请在文件名前加上 @ 并使用完整路径
这既可以作为 urlencoded 字符串(如 'para1=val1¶2=val2&...')传递,也可以作为以字段名称作为键和字段数据作为值的数组传递。
如果 value 是一个数组,则 Content-Type 标头将设置为 multipart/form-data。


Not tested,but I suppose that something like this should do the trick -- or at least
help you get started :


未经测试,但我认为这样的事情应该可以解决问题 - 或者至少可以
帮助您入门:

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http://www.example.com/your-destination-script.php");
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, array(
        'file' => '@/..../file.jpg',
         // you'll have to change the name, here, I suppose
         // some other fields ?
));
$result = curl_exec($ch);
curl_close($ch);

Basically, you :

基本上,您:

  • are using curl
  • have to set the destination URL
  • indicate you want curl_execto return the result, and not output it
  • are using POST, and not GET
  • are posting some data, including a file -- note the @before the file's path.
  • 正在使用卷曲
  • 必须设置目标网址
  • 表示要curl_exec返回结果,而不是输出
  • 正在使用POST,而不是GET
  • 正在发布一些数据,包括文件 - 请注意@文件路径之前的内容。

回答by Raven

you can do it in the same way. Just this time your server who received the file first is the client and the second server is your server. Try using these:

你可以用同样的方式做到这一点。只是这次你的第一个收到文件的服务器是客户端,第二个服务器是你的服务器。尝试使用这些:

For the webpage on the second server:

对于第二台服务器上的网页:

  <form>
         <input type="text" name="var1" />
         <input type="text" name="var2" />
         <input type="file" name="inputname" />
         <input type="submit" />
  </form>

And as a script to send the file on the first server:

并作为在第一台服务器上发送文件的脚本:

<?php
function PostToHost($host, $port, $path, $postdata, $filedata) {
     $data = "";
     $boundary = "---------------------".substr(md5(rand(0,32000)),0,10);
     $fp = fsockopen($host, $port);

     fputs($fp, "POST $path HTTP/1.0\n");
     fputs($fp, "Host: $host\n");
     fputs($fp, "Content-type: multipart/form-data; boundary=".$boundary."\n");

     // Ab dieser Stelle sammeln wir erstmal alle Daten in einem String
     // Sammeln der POST Daten
     foreach($postdata as $key => $val){
         $data .= "--$boundary\n";
         $data .= "Content-Disposition: form-data; name=\"".$key."\"\n\n".$val."\n";
     }
     $data .= "--$boundary\n";

     // Sammeln der FILE Daten
     $data .= "Content-Disposition: form-data; name=\"{$filedata[0]}\"; filename=\"{$filedata[1]}\"\n";
     $data .= "Content-Type: image/jpeg\n";
     $data .= "Content-Transfer-Encoding: binary\n\n";
     $data .= $filedata[2]."\n";
     $data .= "--$boundary--\n";

     // Senden aller Informationen
     fputs($fp, "Content-length: ".strlen($data)."\n\n");
     fputs($fp, $data);

     // Auslesen der Antwort
     while(!feof($fp)) {
         $res .= fread($fp, 1);
     }
     fclose($fp);

     return $res;
}

$postdata = array('var1'=>'test', 'var2'=>'test');
$data = file_get_contents('Signatur.jpg');
$filedata = array('inputname', 'filename.jpg', $data);

echo PostToHost ("localhost", 80, "/test3.php", $postdata, $filedata);
?>

Both scripts are take from here: http://www.coder-wiki.de/HowTos/PHP-POST-Request-Datei

两个脚本都取自这里:http: //www.coder-wiki.de/HowTos/PHP-POST-Request-Datei

回答by George John

This is a simple PHP script I frequently use for while moving big files from server to servers.

这是一个简单的 PHP 脚本,我经常在将大文件从服务器移动到服务器时使用它。

set_time_limit(0); //Unlimited max execution time

$path = 'newfile.zip';
$url = 'http://example.com/oldfile.zip';
$newfname = $path;
echo 'Starting Download!<br>';
$file = fopen ($url, "rb");
if($file) {
    $newf = fopen ($newfname, "wb");
    if($newf)
        while(!feof($file)) {
            fwrite($newf, fread($file, 1024 * 8 ), 1024 * 8 );
            echo '1 MB File Chunk Written!<br>';
        }
}
if($file) {
    fclose($file);
}
if($newf) {
    fclose($newf);
}
echo 'Finished!';

回答by Arunprabakaran M

The easiest way to transfer file one server into another server.

将文件从一台服务器传输到另一台服务器的最简单方法。

First, need to enable ssh2 php extensionand use the following code

首先,需要启用ssh2 php扩展并使用以下代码

$strServer = '*.*.*.*';
$strServerPort = '*';
$strServerUsername = '*****';
$strServerPassword = '****';

**//connect to server**
$resConnection = ssh2_connect($strServer, $strServerPort);
         if(ssh2_auth_password($resConnection, $strServerUsername, $strServerPassword))
         {
            //Initialize SFTP subsystem            
         $resSFTP = ssh2_sftp($resConnection);             
         $resFile = fopen("ssh2.sftp://{$resSFTP}/FilePath/".$filename, 'w');
         $srcFile = fopen($path.$filename, 'r');
         $writtenBytes = stream_copy_to_stream($srcFile, $resFile);
         fclose($resFile);
         fclose($srcFile);
         }

回答by Yasith Praharshana

It is Very easy.This is very easy & simple code

这是非常容易的。这是非常容易和简单的代码

<html>
<body>

<?php
/*the following 2 lines are not mandatory but we keep it to 
avoid risk of exceeding default execution time and mamory*/
ini_set('max_execution_time', 0);
ini_set('memory_limit', '2048M');

/*url of zipped file at old server*/
$file = 'http://i.ytimg.com/vi/Xp0DOC6nW4E/maxresdefault.jpg';

/*what should it name at new server*/
$dest = 'files.jpg';

/*get file contents and create same file here at new server*/
$data = file_get_contents($file);
$handle = fopen($dest,"wb");
fwrite($handle, $data);
fclose($handle);
echo 'Copied Successfully.';
?>

</body>
</html>




Type your Url in this variable - $file ="http://file-url"

在此变量中输入您的网址 - $file ="http://file-url"

Type your file save as in hosting sever- $dest = 'files.jpg';

在托管服务器中键入您的文件另存为 $dest = 'files.jpg';

回答by Vidar Vestnes

Ex. if you have a file called mypicture.gif on server A and want to send it to server B, you can use CURL.

前任。如果您在服务器 A 上有一个名为 mypicture.gif 的文件并希望将其发送到服务器 B,则可以使用 CURL。

  1. Server A reads the content as a String.
  2. Post the string with CURL to Server B
  3. Server B fetches the String and stores it as a file called mypictyre-copy.gif
  1. 服务器 A 将内容读取为字符串。
  2. 将带有 CURL 的字符串发布到服务器 B
  3. 服务器 B 获取字符串并将其存储为名为 mypictyre-copy.gif 的文件

See http://php.net/manual/en/book.curl.php

http://php.net/manual/en/book.curl.php

Some sample PHP code:

一些示例 PHP 代码:

<?php
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_HEADER, 0);
    curl_setopt($ch, CURLOPT_VERBOSE, 0);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible;)");
    curl_setopt($ch, CURLOPT_URL, _VIRUS_SCAN_URL);
    curl_setopt($ch, CURLOPT_POST, true);
    // same as <input type="file" name="file_box">
    $post = array(
        "file_box"=>"@/path/to/myfile.jpg",
    );
    curl_setopt($ch, CURLOPT_POSTFIELDS, $post); 
    $response = curl_exec($ch);
?>