简体   繁体   English

PHP ftp_connect不响应计划任务中的批处理文件

[英]PHP ftp_connect doesn't responds from batch file in scheduled task

I have a PHP script that synchronize a local directory with a remote one ... 我有一个PHP脚本 ,可以将本地目录与远程目录同步...

[...]
$ftp = new Ftp('11.222.11.222', 'the_user', 'the_pass');
$ftp->syncFolder('userfiles/project', '/web/userfiles/project', true);
[...]

And a Class 和一班

class Ftp
{
    public $conn;
    private $url;
    private $user;
    private $password;
    private $loged;


    /**
     * Ftp constructor.
     * @param $url
     * @param $user
     * @param $password
     * @throws Exception
     */
    public function __construct($url, $user, $password)
    {

        $this->conn = ftp_connect($url);
        $this->user = $user;
        $this->password = $password;


        if ($this->ftp_login($this->user, $this->password)) {

            $this->loged = true;
        } else {

            $this->loged = false;
        }

        $this->ftp_pasv(true);
    }

    /**
     * @param $func
     * @param $a
     * @return mixed
     * @throws Exception
     */
    public function __call($func, $a)
    {
        if (strstr($func, 'ftp_') !== false && function_exists($func)) {
            array_unshift($a, $this->conn);
            return call_user_func_array($func, $a);
        } else {
            if (method_exists($this, $func)) {
                return call_user_func_array($func, $a);
            } else {
                throw new Exception('Undefine Class Function');
            }
        }
    }

    /**
     * @param $local
     * @param $server
     * @param $verbose
     * @return bool
     * @throws Exception
     */
    function syncFolder($local, $server, $verbose)
    {
        if (!$this->loged) {
            return false;
        }

        $this->ftp_chdir($server);
        $server_list = $this->ftp_nlist('.');
        $local_list = scandir(BASE_PATH . $local);

        foreach ($server_list as $item) {
            if ($item == '.' || $item == '..') {
                continue;
            }

            $folder = $this->ftp_nlist($item);

            if ($folder[0] == '.') {
                if (!in_array($item, $local_list)) {
                    $this->emptyDir($server . '/' . $item, $verbose);
                    $this->ftp_chdir($server);
                } else {
                    $this->syncFolder($local . '/' . $item, $server . '/' . $item, $verbose);
                    $this->ftp_chdir($server);
                }
            } else {
                if (!in_array($item, $local_list)) {
                    if ($this->ftp_delete($item)) {

                    } else {

                    }
                } else {
                    $stats = stat(BASE_PATH . $local . '/' . $item);

                    if ($this->ftp_mdtm($item) < $stats['mtime']) {
                        if ($this->ftp_put($item, BASE_PATH . $local . '/' . $item, FTP_BINARY)) {

                        } else {

                        }
                    } else {

                    }
                }
            }

        }

        $this->putMissingFilesFromDir($local, $server, $verbose);
        $this->ftp_chdir($server);
    }


    /**
     * @param $dir
     * @param $verbose
     * @return bool
     * @throws Exception
     */
    function emptyDir($dir, $verbose)
    {
        if (!$this->loged) {
            return false;
        }

        $this->ftp_chdir($dir);
        $server_list = $this->ftp_nlist('.');

        foreach ($server_list as $item) {
            if ($item == '.' || $item == '..') {
                continue;
            }
            $folder = $this->ftp_nlist($item);
            if ($folder[0] == '.') {
                $this->emptyDir($dir . '/' . $item, $verbose);
                $this->ftp_chdir($dir);
            } else {
                if ($this->ftp_delete($item)) {

                } else {

                }
            }
        }

        if ($this->ftp_rmdir($dir)) {

        } else {

        }
    }


    /**
     * @param $local
     * @param $server
     * @param $verbose
     * @return bool
     * @throws Exception
     */
    function putMissingFilesFromDir($local, $server, $verbose)
    {
        if (!$this->loged) {
            return false;
        }

        $this->ftp_chdir($server);
        $server_list = $this->ftp_nlist('.');
        $local_list = scandir(BASE_PATH . $local);

        foreach ($local_list as $item) {
            if ($item == '.' || $item == '..') {
                continue;
            }
            if (is_dir(BASE_PATH . $local . '/' . $item)) {
                if (!in_array($item, $server_list)) {
                    $this->ftp_mkdir($item);
                }
                $this->putMissingFilesFromDir($local . '/' . $item, $server . '/' . $item, $verbose);
                $this->ftp_chdir($server);

            } else {
                if (!in_array($item, $server_list)) {
                    if ($this->ftp_put($item, BASE_PATH . $local . '/' . $item, FTP_BINARY)) {

                    } else {

                    }
                }
            }

        }
    }

}

This script works fine from browser and from windows' CMD using this command: 使用以下命令,此脚本可从浏览器和Windows的CMD正常运行

C:\php5.6\php.exe -f "D:\www\project\cron\synchro_hosting.php"

But when I try to run this script from a batch file in a scheduled task the ftp_connect function doesn't do anything . 但是,当我尝试计划任务中的批处理文件运行此脚本时 ftp_connect函数不会执行任何操作 It remains waiting for something. 它仍然在等待某些东西。

cd /D "D:\www\project\cron\"
"C:\php5.6\php.exe" -f synchro_hosting.php

I'm using a log file to write events (now removed for summarize the code) and the code stop in ftp_connect. 我正在使用一个日志文件来写事件(现在已删除,以总结代码),并且代码停止在ftp_connect中。

Ideas? 有想法吗?

UPDATE 1: 更新1:

I tried to run the same command from CMD, batch file triggered from CMD and a batch file triggered from scheduled task, and works fine with two first options. 我尝试从CMD运行相同的命令,从CMD触发的批处理文件和从计划任务触发的批处理文件,并且在使用两个第一个选项时工作良好。 The execution stops in ftp_connect function when is triggered from batch file in a scheduled task. 从计划任务中的批处理文件触发时,执行将在ftp_connect函数中停止。 The windows user that execute this actions is the same in the three cases. 在这三种情况下,执行此操作的Windows用户是相同的。

C:\php5.6\php.exe -f "D:\www\project\cron\synchro_hosting.php"

I don't know where is the problem with ftp_connect from scheduled task. 我不知道计划任务中ftp_connect的问题在哪里。 Is maybe a permissions issue? 可能是权限问题?

There are another way in Windows to trigger an php script recurrently? Windows中还有另一种方式可以反复触发php脚本吗?

As I told to Rockstar, I have found a solution to avoid the problem, not to fix it. 正如我对Rockstar所说,我已经找到了避免该问题的解决方案,而不是加以解决。 I tried to execute a php file directly from scheduled tasks and it worked instead of execute a bat file. 我试图直接从计划任务中执行一个php文件,它起作用了而不是执行了bat文件。

The action tab from scheduled tasks should look like this: 计划任务的“操作”选项卡应如下所示:

Action : Start a program Program or Script : C:\\php5.6\\php.exe Add arguments : "D:\\www\\project\\cron\\synchro_hosting.php" 操作 :启动程序程序或脚本 :C:\\ php5.6 \\ php.exe 添加参数 :“ D:\\ www \\ project \\ cron \\ synchro_hosting.php”

Action Tab from Scheduled Tasks 计划任务中的“操作”选项卡

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM