user2550641 user2550641 - 1 year ago 64
PHP Question

PHP script to upload file works in terminal, but not as automatical call with incron

I have a PHP script that uploads a document on a Sharepoint using cURL.
If I run the scipt in the terminal, the upload process works fine.

As I would like to automatically call the script whenever this file is changed, I use incron to detect a change in the respective folder and trigger the call of the PHP script.

My incron file looks like the following:

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

When I have a look at the syslog, I can see that the script call has been triggered correctly by incron. But for some reasons, the file is not uploaded to the Sharepoint.
I also tried to create the file with global write permissions, but this didn't solve my problem.

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

Here is my script I'm calling:

include 'database.php';


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

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_exec ($ch);

if (curl_errno($ch)) {

$msg = curl_error($ch);
else {
$msg = 'File uploaded successfully.';
curl_close ($ch);

I would really appreciate any help!!!

I have also teste it with a normal crontab now and this doesn't work either. It does execute the script and loops through it without printing an error, but doesn't upload the file. Is it possible that is has somethin to do with the authentication?

Answer Source

Finally it turned out, that it has been an issue with the proxy settings.

For whatever reason the apache webserver as well as the incron/cron don't recognize the environment variables that have been set in the machine (http_proxy, https_proxy...)

Therefore, setting the proxy and the proxyport directly in my curl command solved the problem for me.

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

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

Thank you all for your active contributon, I'm glad that i finally found a solution for my problem!

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download