用于上传文件的PHP脚本在终端中工作,但不能与incron一起自动调用

use*_*641 6 php ubuntu sharepoint curl incron

我有一个PHP脚本,使用cURL在Sharepoint上上传文档.如果我在终端中运行scipt,则上传过程正常.

因为我想在更改此文件时自动调用脚本,我使用incron来检测相应文件夹中的更改并触发PHP脚本的调用.

我的incron文件如下所示:

/var/www/[further path]/temp IN_MODIFY,IN_CREATE /usr/bin/php /var/www/[further path]/uploadToSharepoint.php
Run Code Online (Sandbox Code Playgroud)

当我查看syslog时,我可以看到incron正确触发了脚本调用.但由于某些原因,该文件未上载到Sharepoint.我还尝试使用全局写权限创建文件,但这并没有解决我的问题.

-rwxrwxrwx 1 www-data www-data 49058 Mär  3 10:28 [file].xlsx
Run Code Online (Sandbox Code Playgroud)

这是我正在调用的脚本:

包括'database.php';

    $username="[username]";
    $password="[password]";

    $localFileURL="/var/www/[further path]/temp/";
    $files = scandir($localFileURL, 1);
    $newest_file = $files[0];
    $pathToUpload=getTeamPath($newest_file);


    uploadDocument($pathToUpload . '/' . $newest_file, $localFileURL . $newest_file,$username, $password);

    function uploadDocument($dest, $localFile,$username, $password){
            $fp = fopen($localFile, 'r');

    // Connecting to website.
            $ch = curl_init();

            curl_setopt($ch, CURLOPT_USERPWD, $username . ':' . $password);
            curl_setopt($ch, CURLOPT_URL, $dest);
            curl_setopt($ch, CURLOPT_UPLOAD, 1);
            curl_setopt($ch, CURLOPT_TIMEOUT, 86400); // 1 Day Timeout
            curl_setopt($ch, CURLOPT_INFILE, $fp);
            curl_setopt($ch, CURLOPT_NOPROGRESS, false);
            curl_setopt($ch, CURLOPT_BUFFERSIZE, 128);
            curl_setopt($ch, CURLOPT_INFILESIZE, filesize($localFile));
            curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_NTLM);
            curl_exec ($ch);

            if (curl_errno($ch)) {

                    $msg = curl_error($ch);
            }
            else {
                    $msg = 'File uploaded successfully.';
            }
            curl_close ($ch);
  }
Run Code Online (Sandbox Code Playgroud)

我真的很感激任何帮助!

编辑: 我现在也用普通的crontab测试它,这也不起作用.它执行脚本并循环遍历它而不打印错误,但不上传文件.是否有可能与身份验证有关?

use*_*641 0

最后发现是代理设置的问题

无论出于何种原因,apache web 服务器以及 incron/cron 都无法识别机器中设置的环境变量(http_proxy、https_proxy...)

因此,直接在我的curl命令中设置代理和代理端口解决了我的问题。

curl_setopt($ch, CURLOPT_PROXY, '[ip.ip.ip...]'); 
curl_setopt($ch, CURLOPT_PROXYPORT, '[port]'); 
Run Code Online (Sandbox Code Playgroud)

有关设置代理代理端口的更多信息可以在提供的链接中找到。

感谢大家的积极贡献,我很高兴终于找到了解决我的问题的方法!