Browse Source

ftp上传

15954078560 2 years ago
parent
commit
ec0a44641a
2 changed files with 59 additions and 4 deletions
  1. 2 2
      application/api/controller/Index.php
  2. 57 2
      application/api/controller/Notify.php

+ 2 - 2
application/api/controller/Index.php

@@ -1578,8 +1578,8 @@ class Index extends Api
 
         $content = join('|', $content);
 
-        $filename = date('Ymd').'_'.config('cp_id').'_userviewdata_1.dat';
-        $check_filename = date('Ymd').'_'.config('cp_id').'_userviewdata_1.chk';
+        $filename = date('YmdH').'_'.config('cp_id').'_userviewdata_1.dat';
+        $check_filename = date('YmdH').'_'.config('cp_id').'_userviewdata_1.chk';
 
         error_log(print_r($content, 1) . PHP_EOL, 3, './dashuju/' . $filename);
         error_log('', 3, './dashuju/' . $check_filename);

+ 57 - 2
application/api/controller/Notify.php

@@ -1307,13 +1307,68 @@ class Notify extends Api
         }
     }
 
-    //用户观看行为数据文件上传到卓望ftp
+    //用户观看行为数据文件上传到卓望ftp(每小时第10分钟提供)
     public function uploadpayftp() {
+        $host = config('pay_ftp_ip');
+        $port = config('pay_ftp_port');
+        $username = config('pay_ftp_user');
+        $password = config('pay_ftp_pwd');
 
+        $filename = date('YmdH', time() - 3600).'_'.config('cp_id').'_userviewdata_1.dat';
+        $check_filename = date('YmdH', time() - 3600).'_'.config('cp_id').'_userviewdata_1.chk';
+
+        //判断文件是否存在, 不存在则创建
+        if (!is_dir('./dashuju/' . $filename)) {
+            @mkdir('./dashuju/' . $filename, 0755, true);
+        }
+        if (!is_dir('./dashuju/' . $check_filename)) {
+            @mkdir('./dashuju/' . $check_filename, 0755, true);
+        }
+
+        $conn = ftp_connect($host, $port) or die("Could not connect");
+
+        ftp_login($conn,$username,$password);
+        //利用ftp创建目录
+        /*$path = "home/2021-12-08";
+        $this->make_directory($conn,$path);
+        //利用ftp选择进入目录
+        ftp_chdir($conn,$path);*/
+        //开始上传  ftp_put(ftp链接,服务器地址,本地地址,上传模式);
+        if(ftp_put($conn,$filename,'./dashuju/' . $filename,FTP_BINARY)){
+            //上传成功
+        }
+        if(ftp_put($conn,$check_filename,'./dashuju/' . $check_filename,FTP_BINARY)){
+            //上传成功
+        }
+        ftp_close($conn);
+        //注意上传端的ftp权限设置
     }
 
-    //大数据文件上传到大数据ftp
+    //大数据文件上传到大数据ftp(每天一点提供)
     public function uploaddataftp() {
 
     }
+
+    public function make_directory($ftp_stream, $dir){
+        // if directory already exists or can be immediately created return true
+        if ($this->ftp_is_dir($ftp_stream, $dir) || @ftp_mkdir($ftp_stream, $dir)) return true;
+        // otherwise recursively try to make the directory
+        if (!$this->make_directory($ftp_stream, dirname($dir))) return false;
+        // final step to create the directory
+        return ftp_mkdir($ftp_stream, $dir);
+    }
+
+    public function ftp_is_dir($ftp_stream, $dir){
+        // get current directory
+        $original_directory = ftp_pwd($ftp_stream);
+        // test if you can change directory to $dir
+        // suppress errors in case $dir is not a file or not a directory
+        if ( @ftp_chdir( $ftp_stream, $dir ) ) {
+            // If it is a directory, then change the directory back to the original directory
+            ftp_chdir( $ftp_stream, $original_directory );
+            return true;
+        } else {
+            return false;
+        }
+    }
 }