1

I have a PHP script that synchronize a local directory with a remote one ...

[...]
$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:

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. 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.

Ideas?

UPDATE 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. The execution stops in ftp_connect function when is triggered from batch file in a scheduled task. The windows user that execute this actions is the same in the three cases.

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. Is maybe a permissions issue?

There are another way in Windows to trigger an php script recurrently?

7
  • Is your PHP file in the directory you are changing to? Is the D: drive a local drive or a mapped network drive?
    – Squashman
    Jul 13, 2018 at 13:12
  • I'm not sure why your batch file doesn't just contain the same command you used at the cmd.exe prompt. If you're specifically wanting to stipulate the working directory, you could also consider using the Start command with its /D option. Given the information you've provided, there's nothing wrong with the batch file. It is however undetermined what the working directory was in your 'working' CMD example.
    – Compo
    Jul 13, 2018 at 13:23
  • @Squashman: My php file and my batch file are in the same directori www\project\cron\synchro_hosting.php and www\project\cron\cron.bat. D: drive is a local drive.
    – Aston
    Jul 13, 2018 at 20:21
  • @Compo : OK, this monday I will try your suggestions
    – Aston
    Jul 13, 2018 at 20:51
  • @Compo ... it still doesn't work
    – Aston
    Jul 16, 2018 at 16:08

1 Answer 1

0

As I told to Rockstar, I have found a solution to avoid the problem, not to fix it. I tried to execute a php file directly from scheduled tasks and it worked instead of execute a bat file.

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"

Action Tab from Scheduled Tasks

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.