Files
baim0-learn/nginx/17.Nginx实现上传的方法.md
2024-03-09 13:38:35 +08:00

6.7 KiB
Raw Permalink Blame History

17.Nginx实现上传的方法

[upload模块实现文件上传]

nginx实现文件上传,需要依赖一个额外的安装模块upload. 跟以往一样,默认情况下,nginx不带这个模块,所以,我们需要做两件事,1:下载该模块 2:重新编译nginx

wget https://github.com/fdintino/nginx-upload-module/archive/2.3.0.tar.gz

解压

tar zxvf 2.3.0.tar.gz

cd  nginx-1.12.2
./configure --add-module=../nginx-upload-module-2.3.0/ --prefix=/usr/local/nginx
make
make install

启动nginx

/usr/local/nginx/sbin/nginx

修改nginx的配置文件,添加如下这一段

    ## -------> 新增内容 <-------------------
    location /upload {
        upload_pass /res_upload;
        upload_store /usr/local/nginx/html;
    }
    location /res_upload {
        default_type text/html;
        return 200 "success";
    }
    ## <------ 新增内容结束 ------------------->

重启nginx,让配置生效

然后在 /usr/local/nginx/html目录下新增一个 file.html(名字别用 upload.html 不然会命中 /upload 这个location的规则,踩坑才跳出来),用于浏览器上传文件使用:

<html>
  <head>
    <title>Test upload</title>
  </head>
  <body>
    <h2>Select files to upload</h2>
    <form name="upload" method="POST" enctype="multipart/form-data" action="/upload">
      <input type="file" name="file"><br>
      <button type="submit"> Upload </button>
    </form>
  </body>
</html>

然后再浏览器打开这个页面,就可以上传文件了

[PHP实现文件上传]

我们之前也尝试过编写PHP,所以这里就不再赘述了
我们准备好一个HTML页面,然后再准备好一个PHP的源代码文件


<!-- 这是提交给nginx -->
<html lang="CN">
<head>
    <meta charset="UTF-8">
    <title>Test upload</title>
</head>
<body>
<h2>Select files to upload</h2>
<form enctype="multipart/form-data" action="/upload" method="post">
    <input type="file" name="file"><br><br>    <input type="file" name="file1"><br><br>    <input type="file" name="file2"><br>

    <input type="submit" name="submit" value="Upload">
    <input type="hidden" name="test" value="value">
</form>
</body>
</html>
<!-- 这是直接提交给php -->
<html lang="CN">
<head>
    <meta charset="UTF-8">
    <title>Test upload</title>
</head>
<body>
<h2>Select files to upload</h2>
<form enctype="multipart/form-data" action="/upload.php" method="post">
    <input type="file" name="file"><br>
    <input type="submit" name="submit" value="Upload">
    <input type="hidden" name="test" value="value">
</form>
</body>
</html>

1:通过配置Ningx 安装 Upload Modules 进行 文件上传 再从PHP 接受 Ningx POST过来的参数。

2.直接通过编写PHP,从HTML 负责文件上传


server {
     listen       80;
   server_name  localhost;
 charset utf-8;

 access_log  /usr/local/nginx/logs/access.log  main;

    client_max_body_size 100m;

   location /upload {

       upload_pass   /upload.php;

       upload_store /usr/share/nginx/html/file 1;

       upload_store_access user:r;

        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 "^submit$|^description$";
    }

如果是当前端口,设置proxy_pass会出现错误


    #location @test {
    #   proxy_pass   htpp://127.0.0.1;
    #}
    location / {
        root           /usr/share/nginx/html/work/public;
        index  index.html index.htm index.php;
    }

    error_page  404              /404.html;

    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /usr/share/nginx/html;
    }

    location ~ \.php$ {
        root           /usr/share/nginx/html/work/public;
        fastcgi_pass   unix:/opt/remi/php70/root/run/lock/php-fcgi.sock;
        fastcgi_index  index.php;
        fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
        include        fastcgi_params;
    }
    location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$
    {
        expires       15d;
    }
    location ~ .*\.(js|css)?$
    {
        expires       1d;
    }
}

<?php
header('Content-Type: text/html;charset=UTF-8');

$type = ['.pdf' => 'application/pdf'];

const FILE_DIR = '/usr/share/nginx/html/work/';

if (count(($file = $_FILES['file'])) > 0) {

    if ($file['error'] == 0) {

        if ($fileType = array_search($file['type'], $type)) {

            //以当前的时间命名目录
            $date_dir = date('Y-m-d', time());

            if (!is_dir(FILE_DIR . $date_dir)) {

                if (!mkdir(FILE_DIR . $date_dir)) {
                    return header('location:503.html');
                }
            }

            $filename = FILE_DIR . $date_dir . '/' . (md5_file($file['tmp_name']) . time() . rand(9999, 99999)) . $fileType;

            //生成新的文件
            if (rename($file['tmp_name'], $filename)) {

                return header('Location: success.html');

            }

        }
    }

    switch ($file['error']) {
        case 1:
            http_response_code(400);
            exit('文件大小超出了服务器的空间大小');
        case 2:
            http_response_code(400);
            exit('要上传的文件大小超出浏览器限制');
        case 3:
            http_response_code(400);
            exit('文件仅部分被上传');
        case 4:
            http_response_code(404);
            exit('没有找到要上传的文件');
        case 5:
            http_response_code(503);
            exit('服务器临时文件夹丢失');
        case 6:
            http_response_code(503);
            exit('文件写入到临时文件夹出错');
    }
}

如果是nginx upload module

if (count(($file = $_POST)) > 0) {

    if ($fileType = array_search($file['file_content_type'], $type)) {

        //以当前的时间命名目录
        $date_dir = date('Y-m-d', time());

        //如果目录没创建,我们就自己创建一个
        if (!is_dir(FILE_DIR . $date_dir)) {

            if (!mkdir(FILE_DIR . $date_dir)) {
                return header('location:503.html');
            }
        }

        $filename = FILE_DIR . $date_dir . '/' . (md5_file($file['file_path']) . time() . rand(9999, 99999)) . $fileType;

        //生成新的文件
        if (rename($file['file_path'], $filename)) {

            return header('Location: success.html');

        }
    }
}