部署上传文件服务器上,服务器部署文件上传功能

前提:有可访问的站点

1. 先到域名对应的项目地址根目录,例如:

cd /home/yxy/discuz

2. 编写PHP文件

vim upload.php

内容如下:

$path = "upload/" .date("Y/m/d/");

if(!is_dir($path)){

exec("mkdir -p ".$path);

}

move_uploaded_file($_FILES["file"]["tmp_name"],

$path. $_FILES["file"]["name"]);

echo "http://域名/". $path . $_FILES["file"]["name"];

3. 创建存文件的目标路径

mkdir upload

4. 授权Apache

chown -R apache.apache *

chmod -R 755 *

5. 编写测试页面

vim test.html

内容如下:

enctype="multipart/form-data">

Filename:

6. 授权Apache

chown apache.apache test.html

chmod 755 test.html

7. 修改PHP配置(先备份 cp /etc/php.ini /etc/php.ini20190109bak ),修改最大限制

vim /etc/php.ini

修改内容:

post_max_size = 100M

upload_max_filesize = 100M

max_execution_time = 30

8. 重启PHP

service php-fpm restart

9. 修改NGINX配置(先备份 cp nginx.conf nginx.conf20190109bak),修改最大限制

cd /etc/nginx/

vim nginx.conf

参考如下:

# For more information on configuration, see:

# * Official English Documentation: http://nginx.org/en/docs/

# * Official Russian Documentation: http://nginx.org/ru/docs/

user nginx;

worker_processes auto;

error_log /var/log/nginx/error.log;

pid /var/run/nginx.pid;

# Load dynamic modules. See /usr/share/nginx/README.dynamic.

include /usr/share/nginx/modules/*.conf;

events {

worker_connections 1024;

}

http {

log_format main '$remote_addr - $remote_user [$time_local] "$request" '

'$status $body_bytes_sent "$http_referer" '

'"$http_user_agent" "$http_x_forwarded_for"';

access_log /var/log/nginx/access.log main;

sendfile on;

tcp_nopush on;

tcp_nodelay on;

keepalive_timeout 65;

types_hash_max_size 2048;

client_max_body_size 100m;

fastcgi_buffers 8 128k;

fastcgi_read_timeout 300;

fastcgi_connect_timeout 300;

fastcgi_send_timeout 300;

server_names_hash_bucket_size 128;

client_body_buffer_size 100m;

client_header_buffer_size 100m;

include /etc/nginx/mime.types;

default_type application/octet-stream;

# Load modular configuration files from the /etc/nginx/conf.d directory.

# See http://nginx.org/en/docs/ngx_core_module.html#include

# for more information.

include /etc/nginx/conf.d/*.conf;

}

10. 查看配置文件是否正确,并重启

nginx -t

nginx -s reload

11. 完毕!打开测试地址(站点域名/test.html)即可。

12. 开通接口上传

修改upload.php

如下:

$path = "upload/" .date("Y/m/d/");

if(!is_dir($path)){

exec("mkdir -p ".$path);

}

move_uploaded_file($_FILES["file"]["tmp_name"],

$path. $_FILES["file"]["name"]);

$url = "http://域名/". $path . $_FILES["file"]["name"];

$return = array('file'=>$url);

exit(json_encode($return));

postman使用效果:

e628959c9724

image.png

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值