Laravel使用SFTP上传文件
首先使用composer安装第三方包
composer require graham-campbell/flysystem
#生产配置文件
php artisan vendor:publish
这个第三方包的github地址为:https://github.com/GrahamCampbell/Laravel-Flysystem
使用Demo
<?php
namespace App\Console\Commands;
use App\Member;
use GrahamCampbell\Flysystem\Facades\Flysystem;
use Http;
use Log;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Storage;
class SynchWechatRegisterUsersToArvato extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'SynchWechatRegisterUsersToArvato {dateday?}';
/**
* The console command description.
*
* @var string
*/
protected $description = '同步注册会员到arvato';
/**
* Create a new command instance.
*
* @return void
*/
public function __construct()
{
parent::__construct();
}
/**
* Execute the console command.
*
* @return mixed
*/
public function handle()
{
ini_set('memory_limit', '1024M');
set_time_limit(3600);
$this->createdir(storage_path('userregister'));
if($dateday = $this->argument('dateday')){
$start = date('Y-m-d 00:00:00',strtotime($dateday));
$end = date('Y-m-d 00:00:00',strtotime("$dateday + 1 day"));
$filename = 'register_users_'.date('Ymd',strtotime($dateday)).'.csv';
$filepath = storage_path('userregister/'.$filename);
}else{
$start = date('Y-m-d 00:00:00',strtotime('-1 day'));
$end = date('Y-m-d 00:00:00');
$filename = 'register_users_'.date('Ymd',strtotime('- 1day')).'.csv';
$filepath = storage_path('userregister/'.$filename);
}
$header = [];
$memberList = [];
file_put_contents($filepath,$header."\n");
file_put_contents($filepath,$memberList,FILE_APPEND);
if(Flysystem::put('/In/register_user/'.$filename,file_get_contents($filepath))){
echo '上传成功';
}else{
Log::info('register_user_updaload'.$filename.'上传失败');
echo '上传失败';
}
}
public function createdir($path){
if(!file_exists($path)){
mkdir($path,0777);
}
}
}
还没有评论,来说两句吧...