laravel 上传文件到 SFTP 服务器

老虎说测试 脚本开发字数 6145阅读20分29秒阅读模式
摘要laravel 上传文件到 SFTP 服务器。

laravel 上传文件到 SFTP 服务器。

composer  JSON文章源自陈学虎-https://chenxuehu.com/article/2019/05/7437.html

"league/flysystem-sftp": "~1.0"

配置文件 filesystems.php,自行替换  ...  内容文章源自陈学虎-https://chenxuehu.com/article/2019/05/7437.html

'default' => env('FILESYSTEM_DRIVER', 'sftp'),
'sftp' => [
            'driver' => 'sftp',
            'host' => '...',
            'username' => '...',
            'password' => '...',

            // Settings for SSH key based authentication
            // 'privateKey' => '/path/to/privateKey',
            // 'password' => 'encryption-password',

            // Optional SFTP Settings
            // 'port' => 22,
            'root' => '...',
            // 'timeout' => 30,
        ],

注册路由文章源自陈学虎-https://chenxuehu.com/article/2019/05/7437.html

Route::get('/',"HomeController@index");
Route::post('/uploadimg', 'HomeController@uploadimg');

前端文章源自陈学虎-https://chenxuehu.com/article/2019/05/7437.html

@extends('layouts.app')

@section('content')
<div class="container">
    <div class="row justify-content-center">
        <div class="col-md-12">
            @if( !$path )
            <div class="card">
             <form class="form-horizontal" method="POST" action="/uploadlocal" enctype="multipart/form-data">
                {{ csrf_field() }}
                <div class="card-header">请上传文件</div>
                <div class="card-body">
                    <div class="col-md-3" style="float:left;margin-bottom:5px;">
                        <input  type="file" class="form-control" style="height:45px;" name="imgfile[]" required>
                    </div>
                    <div class="col-md-3" style="float:left;margin-bottom:5px;">
                        <input  type="file" class="form-control" style="height:45px;" name="imgfile[]" >
                    </div>
                    <div class="col-md-3" style="float:left;margin-bottom:5px;">
                        <input  type="file" class="form-control" style="height:45px;" name="imgfile[]" >  
                    </div>

                    <div class="col-md-3" style="float:left;margin-bottom:5px;">
                        <input  type="file" class="form-control" style="height:45px;" name="imgfile[]" >  
                    </div>

                    <div class="col-md-3" style="float:left;margin-bottom:5px;">
                        <input  type="file" class="form-control" style="height:45px;" name="imgfile[]" >  
                    </div>

                    <div class="col-md-3" style="float:left;margin-bottom:5px;">
                        <input  type="file" class="form-control" style="height:45px;" name="imgfile[]" >  
                    </div>

                   <div class="col-md-3" style="float:left;margin-bottom:5px;">
                        <input  type="file" class="form-control" style="height:45px;" name="imgfile[]" >  
                    </div>

                    <div class="col-md-3" style="float:left;margin-bottom:5px;">
                        <input  type="file" class="form-control" style="height:45px;" name="imgfile[]" >  
                    </div>

                    <div class="col-md-3" style="float:left;margin-bottom:5px;">
                        <input  type="file" class="form-control" style="height:45px;" name="imgfile[]" >  
                    </div>

                    <div class="col-md-3" style="float:left;margin-bottom:5px;">
                        <input  type="file" class="form-control" style="height:45px;" name="imgfile[]" >  
                    </div>

                    <div class="col-md-3" style="float:left;margin-bottom:5px;">
                        <input  type="file" class="form-control" style="height:45px;" name="imgfile[]" >  
                    </div>

                    <div class="col-md-3" style="float:left;margin-bottom:5px;">
                        <input  type="file" class="form-control" style="height:45px;" name="imgfile[]" >  
                    </div>

                    <div class="col-md-3" style="float:left;margin-bottom:5px;">
                        <input  type="file" class="form-control" style="height:45px;" name="imgfile[]" >  
                    </div>

                    <div class="col-md-3" style="float:left;margin-bottom:5px;">
                        <input  type="file" class="form-control" style="height:45px;" name="imgfile[]" >  
                    </div>

                    <div class="col-md-3" style="float:left;margin-bottom:5px;">
                        <input  type="file" class="form-control" style="height:45px;" name="imgfile[]" >  
                    </div>

                    <div class="col-md-3" style="float:left;margin-bottom:5px;">
                        <input  type="file" class="form-control" style="height:45px;" name="imgfile[]" >  
                    </div>

                    <div class="col-md-3" style="float:left;margin-bottom:5px;">
                        <input  type="file" class="form-control" style="height:45px;" name="imgfile[]" >  
                    </div>

                    <div class="col-md-3" style="float:left;margin-bottom:5px;">
                        <input  type="file" class="form-control" style="height:45px;" name="imgfile[]" >  
                    </div>

                    <div class="col-md-3" style="float:left;margin-bottom:5px;">
                        <input  type="file" class="form-control" style="height:45px;" name="imgfile[]" >  
                    </div>

                    <div class="col-md-3" style="float:left;margin-bottom:5px;">
                        <input  type="file" class="form-control" style="height:45px;" name="imgfile[]" >  
                    </div>
                </div>
                <div class="clearfix"></div>
                <div class="card-footer">
                    <button type="submit" class="btn btn-primary">确定上传文件</button>
                </div>
                </form>
            </div>
            @else
            <div class="card card-default">
                <div class="card-header">上传的文件信息</div>
                <div class="card-body">
                    <ul class="list-group">
                        <li class="list-group-item">生成的HTML文件:</li>
                        <li class="list-group-item">
                            {{$htmlurl}}
                        </li>
                        <li class="list-group-item">上传的图片列表,以下地址仅用于本地查看确认</li>
                        @foreach($path as $imgfile)
                        <li class="list-group-item">
                        <div class="media">
                            <div class="media-left">
                                <a href="{{$imgfile}}" target="_blank">
                                    
                                </a>
                                </div>
                                <div class="media-body">
                                    <p>{{$imgfile}}</p>
                                </div>
                            </div>
                        </li>
                        @endforeach
                    </ul>
                <div class="card-footer">
                    <a href="/home" class="btn btn-primary">继续上传文件</a>
                </div>
            </div>
            @endif
        </div>
    </div>
</div>
@endsection

控制器文章源自陈学虎-https://chenxuehu.com/article/2019/05/7437.html

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Storage;

class HomeController extends Controller
{
    /**
     * Create a new controller instance.
     *
     * @return void
     */
    public function __construct()
    {
        $this->middleware('auth');
    }

    /**
     * Show the application dashboard.
     *
     * @return \Illuminate\Contracts\Support\Renderable
     */
    public function index()
    {
        return view('home',['path'=>[]]);
    }

    public function uploadimg(Request $request)
    {
        if ($request->isMethod('POST')) {
            $sftpFileCate = 'imgfile';
            $fileCharaters = $request->file('imgfile');
            $fileArr=[];
            foreach($fileCharaters as $fileCharater)
            {
                if ($fileCharater->isValid()) {
                    $ext = $fileCharater->getClientOriginalExtension();
                    $path = $fileCharater->getRealPath();
                    $filename = date('Y-m-d',time()).'_'.md5(date('Y-m-d-H-i-s').round(100000,999999)).'.'.$ext;
                    $fileArr[]='http://files.com/'.$sftpFileCate.'/'.$filename;
                    $path = Storage::disk('sftp')->put($sftpFileCate.'/'.$filename, file_get_contents($path));
                }
            }
        }

        $str = <<< EOT
 
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <link rel="stylesheet" href="/bootstrap.min.css">
    <link rel="shortcut icon" href="/favicon.ico" />
    <title>在线图片查看</title>
</head>

<body>
    <div class="alert alert-danger" style="margin-top:20px;">
        点击图片查看原图
    </div>
EOT;
        foreach ($fileArr as  $imgUrl) {
            $str.= <<< EOT
    <a href="{$imgUrl}" target="_blank"><img src="{$imgUrl}" class="img-fluid img-thumbnail"></a>
    <div class="alert alert-info" style="margin-top:20px;height: 2px;">&nbsp;</div>
EOT;
        }

$str .= <<< EOT
</body>

</html/>
 
EOT;
        
        $htmlFileName = date('Y-m-d',time()).'_'.md5(time().round(100000,999999));
        Storage::disk('sftp')->put($sftpFileCate.'/'.$htmlFileName.'.html', $str);
        $htmlUrl='http://files.com/'.$sftpFileCate.'/'.$htmlFileName.'.html';
        return view('home',['path'=>$fileArr,'htmlurl'=>$htmlUrl]);
    }
}

如此就完美实现了,界面未美化,以下界面是调试界面,正式界面略有差异。文章源自陈学虎-https://chenxuehu.com/article/2019/05/7437.html

laravel 上传文件到 SFTP 服务器文章源自陈学虎-https://chenxuehu.com/article/2019/05/7437.html 文章源自陈学虎-https://chenxuehu.com/article/2019/05/7437.html

 
  • 版权声明:本文为原创文章,转载请附上原文出处链接及本声明。
  • 转载请注明:laravel 上传文件到 SFTP 服务器 | https://chenxuehu.com/article/2019/05/7437.html
评论  3  访客  3