实现Nginx Upload 模块 功能上传文件。
分析(直接实践是最好的。。。。。):
一、Ningx 上传(
1.安装Nginx 的模块文件(upload):https://www.nginx.com/resources/wiki/modules/upload/,默认Nginx 肯定是没安装这个扩展模块的,你可以准备删除Nginx重新去官网下载一个最新稳定版本,并且进行编译吧。。。。。。
# Upload form should be submitted to this location
location /upload { # Pass altered request body to this location
upload_pass /upload.php; # Store files to this directory
# The directory is hashed, subdirectories 0 1 2 3 4 5 6 7 8 9 should exist 这里是扔到10个文件夹里去
upload_store /usr/share/nginx/html/file 1; # Allow uploaded files to be read only by user
upload_store_access user:r;
就是在这里,他会自动给你命名。
# Set specified fields in request body
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; # Inform backend about hash and size of a file
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$";
}
这里是个大坑,因为如果默认就是当前的服务器的80端口,配置这个是会出错的,我就直接没用代理,直接
upload_pass /upload.php;
#如果是当前端口,设置proxy_pass会出现错误
# Pass altered request body to a backend
#location @test {
# proxy_pass htpp://127.0.0.1;
#}
2.Nginx的某个路由(看我下面的配置文件)检测到上传请求后,会分别将各个你定义的form file name,上传到不同的文件夹,一共是10个(创建10个文件夹,命名0 1 2 3 ...),文件位置自定义(但一定要包含那10个文件夹,这个切记),一定要检测你创建的文件夹Nginx是否具有写入权限,这个可以自己看log(这个相当重要),如果你配置完成后想玩点新花样,可以自己玩玩,还可以限制上传速度之类的,而且可以做转发,如果你配置的代理服务器本身就做了URL反向代理,那肯定可以转发上传文件到多个Nginx服务器(上传文件提交信息,比如文件位置在哪儿之类的。)去滴。。。
二、直接PHP上传
PHP上传文件,本身就要配置Nginx 模块,所以很多人会搞混,其实两者是有差异的,
正常的上传流程:html 提交上传文件,nginx 收到后 扔到tmp目录,PHP收到后,把TMP的上传文件扔到自己想放的文件夹。
-------------------------------------------------------------------
两者都需要编写HTML,直接提交给Nginx ,在Upload Modules 配置好了,是可以直接接受多个文件上传的。
<!-- 这是提交给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>
<input type="file" name="file1"><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;
# Upload form should be submitted to this location
location /upload {
# Pass altered request body to this location
upload_pass /upload.php;
# Store files to this directory
# The directory is hashed, subdirectories 0 1 2 3 4 5 6 7 8 9 should exist
upload_store /usr/share/nginx/html/file 1;
# Allow uploaded files to be read only by user
upload_store_access user:r;
# Set specified fields in request body
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;
# Inform backend about hash and size of a file
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会出现错误
# Pass altered request body to a backend
#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;
# redirect server error pages to the static page /50x.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;
}
}
自己感受~~~~~
然后贴上Demo PHP 代码:
<?php
header('Content-Type: text/html;charset=UTF-8');
//文件后缀=>文件类型
$type = ['.pdf' => 'application/pdf']; const FILE_DIR = '/usr/share/nginx/html/work/'; //如果非nginx upload module 上传文件
if (count(($file = $_FILES['file'])) > 0) { if ($file['error'] == 0) { //判断文件类型是否存在,文件后缀是我们自己的key去定义
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');
}
} //文件的MD5+当前unix时间戳+一个5位随机数,如果此处需求频繁也可以用微秒时间戳 $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) { //判断文件类型是否存在,文件后缀是我们自己的key去定义
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');
}
} //文件的MD5+当前unix时间戳+一个5位随机数,如果此处需求频繁也可以用微秒时间戳 $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'); }
}
} http_response_code(400);
exit('错误操作方式!');
实现Nginx Upload 模块 功能上传文件。的更多相关文章
- 转:使用 Nginx Upload Module 实现上传文件功能
普通网站在实现文件上传功能的时候,一般是使用Python,Java等后端程序实现,比较麻烦.Nginx有一个Upload模块,可以非常简单的实现文件上传功能.此模块的原理是先把用户上传的文件保存到临时 ...
- 在Update Panel 控件里面添加 File Upload 控件 上传文件
Detail Information:http://www.codeproject.com/Articles/482800/FileplusUploadplusinplusUpdateplusPane ...
- Ant Design Upload 组件上传文件到云服务器 - 七牛云、腾讯云和阿里云的分别实现
在前端项目中经常遇到上传文件的需求,ant design 作为 react 的前端框架,提供的 upload 组件为上传文件提供了很大的方便,官方提供的各种形式的上传基本上可以覆盖大多数的场景,但是对 ...
- 关于js异步上传文件
好久没登录博客园了,今天来一发分享. 最近项目里有个需求,上传文件(好吧,这种需求很常见,这也不是第一次遇到了).当时第一想法就是直接用form表单提交(原谅我以前就是这么干的),不过表单里不仅有文件 ...
- Laravel5.1 搭建博客 --上传文件及文件管理
教程源自:Laravel学院 这一节 咱来说说上传文件的功能实现,我们会把上传的文件保存到项目本地,不仅上传 还有删除和预览功能. 1 配置 我们先从配置开始做起,先修改我们自己创建的 blog.ph ...
- koa 实现上传文件
项目目录: 1.上传单个文件 思路: (1)获取上传文件,使用 const file = ctx.request.files.file (2)我们使用 fs.createReadStream 来读取文 ...
- uploadify 上传文件插件
今天在项目中要用到文件上传功能时,想借助Jquery方式来实现,于是想到用uploadify插件来实现.不经意间在网上看到了一遍关于这个插件的用法,写的很好.在这里就分享给大家,希望对大家有帮助.以下 ...
- Android上传图片到PHP服务器并且支持浏览器上传文件(word、图片、音乐等)
暑假已经过了一半了,这才完成计划当中的第二个任务.虽然进度是慢了点.但也算是暑假的收获吧.下面我就把我学习当中的收获记录在此. 还是跟以往一样,先上图片. 操作的步骤:打开程序---->选择上传 ...
- [moka同学代码]PHP初级知识:上传文件源码
1.目录结构
随机推荐
- Ali_Cloud++:阿里云服务器防火墙相关命令
systemctl start firewalld ##启动Firewall systemctl stop firewalld ##关闭Firewall systemctl restart firew ...
- iOS App的启动过程
一.mach-O Executable 可执行文件 Dylib 动态库 Bundle 无法被连接的动态库,只能通过 dlopen() 加载 Image 指的是 Executable,Dylib 或者 ...
- iOS 项目发布
一.Apple开发者账号 1.1 开发者账号类型 个人级 公司级 企业级 公司和企业的可多人协作. 在苹果的开发者平台登录后,可在 People 界面邀请其他人员协作开发,邀请的人需要注册一个 app ...
- 字符串中的count()方法
描述 Python count() 方法用于统计字符串里某个字符出现的次数.可选参数为在字符串搜索的开始与结束位置. 语法 count()方法语法: str.count(sub, start= 0,e ...
- Asp.Net.Core WebApi 版本控制
前言 在后端Api的开发过程中,无法避免的会遇到接口迭代的过程,如何保证新老接口的共存和接口的向前的兼容呢,这时候就需要对Api进行版本的控制,那如何优雅的控制Api的版本呢? 开始 Microsof ...
- 如何优雅的将文件转换为字符串(环绕执行模式&行为参数化&函数式接口|Lambda表达式)
首先我们讲几个概念: 环绕执行模式: 简单的讲,就是对于OI,JDBC等类似资源,在用完之后需要关闭的,资源处理时常见的一个模式是打开一个资源,做一些处理,然后关闭资源,这个设置和清理阶段类似,并且会 ...
- 为应用程序池“XXX”提供服务的进程在与 Windows Process Activation Service 通信时出现严重错误。该进程 ID 为“XXXX”。数据字段包含错误号。 改进查找流程
原文链接:https://www.cnblogs.com/qidian10/p/6028784.html 为防止原作者删除,留作解决方法备份 ---------------------------- ...
- MariaDB使用数据库查询《三》
MariaDB使用数据库查询 案例5:使用数据库查询 5.1 问题 本例要求配 ...
- ASP.NET Core中的Controller
ASP.NET CORE出现之前我们实现的Controller,MVC都继承自Controller基类,WebApi的话继承自ApiController.现在ASP.NET CORE把MVC跟WebA ...
- php设计模式总结
#1 使用设计模式(如建造者.外观.原型和模板模式)更快速.更有效地创建对象 #2 使用数据访问对象和代理设计模式分离体系结构 #3 使用委托.工厂和单元素设计模式改善代码流和控制 #4 在不修改对象 ...