PHP script to download the file works in the terminal, but not as an automatic call with incron

I have a PHP script that uploads a document to Sharepoint using cURL. If I started a session in the terminal, the boot process will work fine.

As I would like to automatically call a script whenever this file has been modified, I use incron to detect the change in the appropriate folder and call the PHP script call.

My incron file looks like this:

/var/www/[further path]/temp IN_MODIFY,IN_CREATE /usr/bin/php /var/www/[further path]/uploadToSharepoint.php 

When I look at syslog, I see that the script call was run correctly by incron. But for some reason, the file does not upload to Sharepoint. I also tried to create a file with global write permissions, but this did not solve my problem.

 -rwxrwxrwx 1 www-data www-data 49058 Mรคr 3 10:28 [file].xlsx 

Here is my script I'm calling:

enable '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); } 

I would really appreciate any help !!!

EDIT: I also tested it with a regular crontab, and that doesn't work either. It runs the script and passes through it without printing an error, but does not load the file. Is it possible that there is something to do with authentication?

+6
source share
4 answers

Finally, it turned out that this is a problem with the proxy server settings .

For some reason, the apache web server, as well as incron / cron, do not recognize the environment variables that were installed on the machine (http_proxy, https_proxy ...)

Therefore, proxy configuration and proksiporta straight curl in my team solved the problem for me.

 curl_setopt($ch, CURLOPT_PROXY, '[ip.ip.ip...]'); curl_setopt($ch, CURLOPT_PROXYPORT, '[port]'); 

More details about setting proxy and proxyport can be found in the links provided.

Thank you all for your active contribution, I am glad that I finally found a solution to my problem!

0
source

If you say that everything works at startup

 > php /var/www/[further path]/uploadToSharepoint.php 

manually from the command line, but not through incron, then most likely the problem is with the user who runs the incrone commands

Option 1) try to determine the user who runs the incrone commands, and then switches to that user and runs the same php /var/www/[further path]/uploadToSharepoint.php

Option 2) try chmod to 0777 of your scan directory and check again with incron

0
source

According to your questions and comments, this does not work anyway.

According to your code, I'm not very sure about the function you used

 $pathToUpload=getTeamPath($newest_file); 

Hope it gives you the correct download path. Second you use CURLAUTH_NTLM . I remember one day I had to perform a similar task that I used CURLAUTH_BASIC , and it worked. So please try.

Also, CURLOPT_USERPWD try to include the domain in it. how

 curl_setopt($ch, CURLOPT_USERPWD, 'domain\\username:password'); 

Alternatively, you can try setting CURLOPT_POSTFIELDS , where the data will be displayed in your file:

 $data = file_get_contents($file); curl_setopt($ch, CURLOPT_POSTFIELDS, $data); 

Also, by default, curl will try to make a receive request. I believe this should be a POST or PUT request. So try specifying a way like

 curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT"); 
0
source

I just read the documentation page scandir function: http://php.net/manual/en/function.scandir.php

It describes the second parameter (sorting_order):

By default, sorted alphabetically in ascending order. If the sorting_order option is set to SCANDIR_SORT_DESCENDING, then the sort order is in alphabetical order in descending order. If it is set to SCANDIR_SORT_NONE, then the result will be unsorted.

When you specify 1 as sorting_order, it is not sorted depending on the time the files were created or modified in this directory, but it bases the sort order on the file names (descending alphabetical order).

You have two lines in your code:

 $files = scandir($localFileURL, 1); $newest_file = $files[0]; 

The naming of the second line variable suggests that $files[0] should be the newest file, but this is probably often not the case.

This is confusing when you have multiple files or directories inside your temporary directory and as such may explain why the first attempt works and the second attempt does not work.

One suggestion is to edit your incron file and use $@ and $# as parameters for your PHP script, for example:

 /var/www/[further path]/temp IN_MODIFY,IN_CREATE /usr/bin/php /var/www/[further path]/uploadToSharepoint.php " $@ /$#" 

Or, perhaps due to security implications, use this form:

 /var/www/[further path]/temp IN_MODIFY,IN_CREATE /usr/bin/php /var/www/[further path]/uploadToSharepoint.php "$#" 

Using the second form, only the file name that has just been changed or has just been created is indicated as a parameter for your PHP script.

Then you can change this line:

 $newest_file = $files[0]; 

For this:

 $newest_file = $argv[1]; 

Please note that for $argv to work you need to enable this parameter: http://php.net/manual/en/ini.core.php#ini.register-argc-argv

0
source

Source: https://habr.com/ru/post/1015433/


All Articles