nginx-upload-module 断点上传 实战

配置好nginx-upload-module如何测试呢?好像这方面的资料很少。用实例说明把一个文件分3段上传

test1.php

<?php
$url = 'http://dev.upload.com/upload.php';
$filename = "/tmp/upload.jpg";
// 唯一ID
$sessionid =  '1111215056';//uniqid();
$length = filesize($filename);
// 分成3段上传
$chunkSize = intval($length/3);
$fp = fopen($filename, 'r');
// 要上传的文件片段
$posts = fread($fp, $chunkSize);
fclose($fp);
$headers = array(
    "Content-Type: application/octet-stream",
    "Content-Disposition: attachment; name=\"file1\"; filename=\"" .basename($filename). "\"",
    "Content-Length: $chunkSize",
    "X-Content-Range: bytes 0-".($chunkSize-1)."/$length",
    "Session-ID: " . $sessionid
);
// 登录信息
$cookie = "User=loginUser;";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS,$posts);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_COOKIE, $cookie);
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_exec($ch);

test2.php

<?php
$url = 'http://dev.upload.com/upload.php';
$filename = "/tmp/upload.jpg";
// 唯一ID
$sessionid =  '1111215056';//uniqid();
$length = filesize($filename);
// 分成3段上传
$chunkSize = intval($length/3);
$fp = fopen($filename, 'r');
// 要上传的文件片段
fseek($fp, $chunkSize);
$posts = fread($fp, $chunkSize);
fclose($fp);
$headers = array(
    "Content-Type: application/octet-stream",
    "Content-Disposition: attachment; name=\"file1\"; filename=\"" .basename($filename). "\"",
    "Content-Length: $chunkSize",
    "X-Content-Range: bytes $chunkSize-".(2*$chunkSize-1)."/$length",
    "Session-ID: " . $sessionid
);
// 登录信息
$cookie = "User=loginUser;";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS,$posts);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_COOKIE, $cookie);
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_exec($ch);

test3.php

<?php
$url = 'http://dev.upload.com/upload.php';
$filename = "/tmp/upload.jpg";
// 唯一ID
$sessionid =  '1111215056';//uniqid();
$length = filesize($filename);
// 分成3段上传
$chunkSize = intval($length/3);
$fp = fopen($filename, 'r');
// 要上传的文件片段
fseek($fp, 2*$chunkSize);
$posts = fread($fp, $length-2*$chunkSize);
fclose($fp);
$headers = array(
    "Content-Type: application/octet-stream",
    "Content-Disposition: attachment; name=\"file1\"; filename=\"" .basename($filename). "\"",
    "Content-Length: ".($length-2*$chunkSize),
    "X-Content-Range: bytes ".(2*$chunkSize) . '-' .($length-1)."/$length",
    "Session-ID: " . $sessionid
);
// 登录信息
$cookie = "User=loginUser;";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS,$posts);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_COOKIE, $cookie);
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_exec($ch);

执行结果

[minggui@bogon app_upload]$ php test1.php 
HTTP/1.1 201 Created
Server: nginx
Date: Wed, 26 Aug 2015 03:41:37 GMT
Content-Length: 14
Connection: keep-alive
Range: 0-43008/129027

0-43008/129027

[minggui@bogon app_upload]$ php test3.php 
HTTP/1.1 201 Created
Server: nginx
Date: Wed, 26 Aug 2015 03:41:41 GMT
Content-Length: 34
Connection: keep-alive
Range: 0-43008/129027,86018-129026/129027

0-43008/129027,86018-129026/129027 

[minggui@bogon app_upload]$ php test2.php 
HTTP/1.1 200 OK
Server: nginx
Date: Wed, 26 Aug 2015 03:41:52 GMT
Content-Type: text/html
Transfer-Encoding: chunked
Connection: keep-alive
Vary: Accept-Encoding
Vary: Accept-Encoding
X-Powered-By: PHP/5.4.25

array(5) {
  ["file1_name"]=>
  string(10) "upload.jpg"
  ["file1_content_type"]=>
  string(24) "application/octet-stream"
  ["file1_path"]=>
  string(33) "/data/tmp/upload_tmp/6/1111215056"
  ["file1_md5"]=>
  string(0) ""
  ["file1_size"]=>
  string(5) "86018"
}

 

更多资料 https://github.com/keminar/nginx-upload-module

上一篇: nginx-upload-module 编译问题   下一篇: archlinux回退软件版本和忽略升级downgrade

提交疑问

回顶部