nginx upload模块使用

安装

#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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值