Laravel使用SFTP上传文件

╰半橙微兮° 2022-05-23 03:04 376阅读 0赞

首先使用composer安装第三方包

  1. composer require graham-campbell/flysystem
  2. #生产配置文件
  3. php artisan vendor:publish

这个第三方包的github地址为:https://github.com/GrahamCampbell/Laravel-Flysystem

使用Demo

  1. <?php
  2. namespace App\Console\Commands;
  3. use App\Member;
  4. use GrahamCampbell\Flysystem\Facades\Flysystem;
  5. use Http;
  6. use Log;
  7. use Illuminate\Console\Command;
  8. use Illuminate\Support\Facades\DB;
  9. use Illuminate\Support\Facades\Storage;
  10. class SynchWechatRegisterUsersToArvato extends Command
  11. {
  12. /**
  13. * The name and signature of the console command.
  14. *
  15. * @var string
  16. */
  17. protected $signature = 'SynchWechatRegisterUsersToArvato {dateday?}';
  18. /**
  19. * The console command description.
  20. *
  21. * @var string
  22. */
  23. protected $description = '同步注册会员到arvato';
  24. /**
  25. * Create a new command instance.
  26. *
  27. * @return void
  28. */
  29. public function __construct()
  30. {
  31. parent::__construct();
  32. }
  33. /**
  34. * Execute the console command.
  35. *
  36. * @return mixed
  37. */
  38. public function handle()
  39. {
  40. ini_set('memory_limit', '1024M');
  41. set_time_limit(3600);
  42. $this->createdir(storage_path('userregister'));
  43. if($dateday = $this->argument('dateday')){
  44. $start = date('Y-m-d 00:00:00',strtotime($dateday));
  45. $end = date('Y-m-d 00:00:00',strtotime("$dateday + 1 day"));
  46. $filename = 'register_users_'.date('Ymd',strtotime($dateday)).'.csv';
  47. $filepath = storage_path('userregister/'.$filename);
  48. }else{
  49. $start = date('Y-m-d 00:00:00',strtotime('-1 day'));
  50. $end = date('Y-m-d 00:00:00');
  51. $filename = 'register_users_'.date('Ymd',strtotime('- 1day')).'.csv';
  52. $filepath = storage_path('userregister/'.$filename);
  53. }
  54. $header = [];
  55. $memberList = [];
  56. file_put_contents($filepath,$header."\n");
  57. file_put_contents($filepath,$memberList,FILE_APPEND);
  58. if(Flysystem::put('/In/register_user/'.$filename,file_get_contents($filepath))){
  59. echo '上传成功';
  60. }else{
  61. Log::info('register_user_updaload'.$filename.'上传失败');
  62. echo '上传失败';
  63. }
  64. }
  65. public function createdir($path){
  66. if(!file_exists($path)){
  67. mkdir($path,0777);
  68. }
  69. }
  70. }

发表评论

表情:
评论列表 (有 0 条评论,376人围观)

还没有评论,来说两句吧...

相关阅读

    相关 SFTP文件与下载

    一、文件上传 说明1:所谓上传window上的文件上传到linux上 说明2:上传的文件会自动放到当前的用户的家目录 1:打开SFTP的窗口 Alt+p ![1297