美文网首页
服务器部署文件上传功能

服务器部署文件上传功能

作者: 小小卒_oO_ | 来源:发表于2019-01-10 18:57 被阅读0次

    前提:有可访问的站点

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

    cd /home/yxy/discuz

    2. 编写PHP文件

    vim upload.php
    内容如下:

    <?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
    内容如下:

    <html>
    
    <body>
    
    <form action="./upload.php" method="post"
    
    enctype="multipart/form-data">
    
    <label for="file">Filename:</label>
    
    <input type="file" name="file" id="file" />
    
    <br />
    
    <input type="submit" name="submit" value="Submit" />
    
    </form>
    
    </body>
    
    </html>
    

    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

    如下:

    <?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使用效果:


    image.png

    相关文章

      网友评论

          本文标题:服务器部署文件上传功能

          本文链接:https://www.haomeiwen.com/subject/rcfzrqtx.html