DotDragnet
February 08, 2012, 07:26:17 AM *
Welcome, Guest. Please login or register.

Login with username, password and session length
News: Help decide how we fund DDN and where we take the community from here. Post in the thread all about it: http://www.dotdragnet.com/forum/index.php/topic,4368.0.html
 
   Home   Help Search Login Register  
Pages: [1]   Go Down
  Print  
Author Topic: iNotify and automatically running a php file  (Read 1310 times)
Whatever
Hero Member
*****
Posts: 715



View Profile WWW Awards
« on: May 21, 2010, 09:09:40 PM »

Further to my other post, what I've been looking for is a way to run a php script when a file is uploaded to the server. I will not be uploading the file so cannot be sure of when it'll be.

Am I correct that inotify would be able to monitor a given directory for the newly uploaded file and then notify a script that it can run?
Logged

Office Stationery : Paper : Inks & Toners
10% off first 3 orders for registered users. Use the code DDN10
JasonD
Global Moderator
Hero Member
*****
Posts: 529



View Profile Awards
« Reply #1 on: May 21, 2010, 09:58:57 PM »

Possibly. Maybe better to have it hooked into the code that handles the upload. Or if it is not important to run it immediately have a cron job to check every hour or so.
Logged
Whatever
Hero Member
*****
Posts: 715



View Profile WWW Awards
« Reply #2 on: May 22, 2010, 05:38:04 AM »

Thanks Jason. I was mainly looking to see it could be done as this will take a task away from me every week which is good.

The file is actually going to be placed on the server by our wholesaler hence why I wont know when it has been done. It can range from Friday to Sunday.
Logged

Office Stationery : Paper : Inks & Toners
10% off first 3 orders for registered users. Use the code DDN10
Whatever
Hero Member
*****
Posts: 715



View Profile WWW Awards
« Reply #3 on: May 25, 2010, 08:19:54 AM »

I have this now but it doesn't seem to work:

Code:
<?php
if(file_exists("eos.csv"))
{
exec('php eos_update.php');
} else {
echo 
"File not found";
}
?>

eos.csv is in the same folder as this script (which is run by cron) and eos_update.php.

I changed the file to a full if/else statement to tell me if it could see the file and it returned true so there must be something i'm doing wrong elsewhere.

EDIT: This works when run via SSH (not logged in as root) but when run with a cronjob I get the following email:

Quote
public_html/imports/eos/eos_file_checker.php: line 1: ?php: No such file or directory
public_html/imports/eos/eos_file_checker.php: line 2: syntax error near unexpected token `"eos.csv"'
public_html/imports/eos/eos_file_checker.php: line 2: `  if(file_exists("eos.csv"))'



Cheers
« Last Edit: May 25, 2010, 09:57:02 AM by Whatever » Logged

Office Stationery : Paper : Inks & Toners
10% off first 3 orders for registered users. Use the code DDN10
JasonD
Global Moderator
Hero Member
*****
Posts: 529



View Profile Awards
« Reply #4 on: May 25, 2010, 11:59:56 AM »

You need either
Code:
#!/usr/bin/php
(or wherever php is) on the first line, or php -f file.php for the job.
Logged
Whatever
Hero Member
*****
Posts: 715



View Profile WWW Awards
« Reply #5 on: May 25, 2010, 01:50:29 PM »

Thanks again Jason.

Will give that a go.
Logged

Office Stationery : Paper : Inks & Toners
10% off first 3 orders for registered users. Use the code DDN10
Whatever
Hero Member
*****
Posts: 715



View Profile WWW Awards
« Reply #6 on: May 25, 2010, 03:48:11 PM »

I now have this but it doesn't work:

Code:
<?php

#!/usr/local/bin/php

  
if(file_exists("eos.csv"))
    {
      
exec('/php eos_update.php');
    } else {
      echo 
"File not found";
    }
?>

If I run that via ssh then it fails and asks me to remove the leading / of php in the exec function. I then run it again and the whole thing works and does everything it should. Start the cron job back up and it doesn't, giving the weird issues in the post above via email.

Could it be down to permissions and would I be better putting the script to be run by cron in the root directory or elsewhere?
« Last Edit: May 25, 2010, 04:10:39 PM by Whatever » Logged

Office Stationery : Paper : Inks & Toners
10% off first 3 orders for registered users. Use the code DDN10
Whatever
Hero Member
*****
Posts: 715



View Profile WWW Awards
« Reply #7 on: May 25, 2010, 05:20:23 PM »

Doh. Realised I got the path to phh in the wrong place, I didn't realise it came before the php tags so now it tells me it can't find the file but I am closer. Thanks
Logged

Office Stationery : Paper : Inks & Toners
10% off first 3 orders for registered users. Use the code DDN10
Whatever
Hero Member
*****
Posts: 715



View Profile WWW Awards
« Reply #8 on: May 25, 2010, 05:55:50 PM »

All sorted. I ended up with:

Code:
#!/usr/local/bin/php

<?php

  
if(file_exists('public_html/imports/eos/eos.csv'))
    {
      
exec('php public_html/imports/eos/eos_update.php');
    } else {
      echo 
"File not found";
    }
?>

This works a treat.

Thanks for the guidance Jason, much appreciated.
Logged

Office Stationery : Paper : Inks & Toners
10% off first 3 orders for registered users. Use the code DDN10
Pages: [1]   Go Up
  Print  
 
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF | SMF © 2006-2008, Simple Machines Valid XHTML 1.0! Valid CSS!