laravel 上传文件到 SFTP 服务器

  • A+
所属分类:脚本开发
摘要

laravel 上传文件到 SFTP 服务器。

laravel 上传文件到 SFTP 服务器。

composer  JSON

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

配置文件 filesystems.php,自行替换  ...  内容

'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,
        ],

注册路由

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

前端

@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

控制器

<?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]);
    }
}

如此就完美实现了,界面未美化,以下界面是调试界面,正式界面略有差异。

laravel 上传文件到 SFTP 服务器

  • 我的微信
  • 加我请备注缘由
  • weinxin
  • 我的微信公众号
  • 轻松随便关注
  • weinxin

发表评论

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

关闭震动

目前评论:2   其中:访客  2   博主  0

    • avatar 今日新闻 1

      文章不错支持一下

      • avatar 妙文屋 0

        写的很好,支持一下