安装
#wget -O nginx-upload-module.tar.gz https://github.com/vkholodkov/nginx-upload-module/archive/2.2.0.tar.gz #tar zxvf nginx-upload-module.tar.gz 重新编译nginx --add-module=指定模块目录
nginx配置
location /upload { upload_pass /test.php; upload_store /tmp/nginx_upload; upload_store_access user:rw; upload_set_form_field $upload_field_name.name "$upload_file_name"; upload_set_form_field $upload_field_name.content_type "$upload_content_type"; upload_set_form_field $upload_field_name.path "$upload_tmp_path"; upload_aggregate_form_field "$upload_field_name.md5" "$upload_file_md5"; upload_aggregate_form_field "$upload_field_name.size" "$upload_file_size"; upload_pass_form_field "^.*$"; upload_cleanup 400 404 499 500-505; } location ~ \.php$ { try_files $uri =404; include fastcgi.conf; fastcgi_pass 127.0.0.1:9000; fastcgi_param env development; }
上传页面
<html> <body> <h2>Select files to upload</h2> <form name="upload" method="POST" enctype="multipart/form-data" action="http://114.215.142.108:8080/upload"> <input type="file" name="file1"><br> <input type="file" name="file2"><br> <input type="file" name="file3"><br> <input type="submit" name="submit" value="Upload"> <input type="hidden" name="test" value="value"> </form> </body> </html>
后端处理test.php
<?php for($i=1; $i<=3; $i++) { if (isset($_POST["file{$i}_path"]) && $_POST["file{$i}_name"] && $_POST["file{$i}_md5"]) { $src = $_POST["file{$i}_path"]; $md5 = $_POST["file{$i}_md5"]; $dir = '/tmp/'.substr($md5, 0, 2); $dest = $dir.'/'.$_POST["file{$i}_name"]; if (!is_dir($dir)) { mkdir($dir, 0777); } rename($src, $dest); } }
流程:
nginx接收到请求,保存到配置文件指定位置,然后使用php做后续处理。
配置说明
http://wiki.nginx.org/HttpUploadModule