文件上载脚本作为代理

时间:2012-10-02 14:46:56

标签: php file-upload proxy

我真正想做的是:

  • 用户将从html页面上传到脚本
  • 该脚本将登录并返回链接

嗯,所有这些都可以通过curl和通过多部分帖子接受文件来轻松完成。

但问题是,在完成所有上传后,它将开始通过卷曲将文件从服务器上传到另一个:(因此这需要很长的过程。 我想知道它是否可能像用户上传文件一样,它还会继续像下载文件一样发送数据

我不确定这是否甚至可以用PHP。如果没有,可以通过任何其他方式实现这一目标

1 个答案:

答案 0 :(得分:4)

您可以使用cURL执行此操作:

// Open a stream so that we stream the image download
$localFile = $_FILES[$fileKey]['tmp_name'];

$stream = fopen($localFile, 'r');

// Create a curl handle to upload to the file server     
$ch = curl_init($fileServer);
// Send a PUT request
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'PUT');
// Let curl know that we are sending an entity body
curl_setopt($ch, CURLOPT_UPLOAD, true);
// Let curl know that we are using a chunked transfer encoding
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Transfer-Encoding: chunked'));
// Use a callback to provide curl with data to transmit from the stream
curl_setopt($ch, CURLOPT_READFUNCTION, function($ch, $fd, $length) use ($stream) {
    return fread($stream, $length) ? '';
});

curl_exec($ch);