0

I think this has to be a very simple question but I am not finding any answers for what I am trying to accomplish.

I have a PHP file which will send out emails. This part works. The thing is, I need this PHP file to "run" every 5 minutes. My problem is not with the scheduling, I'm fairly certain I understand how to do that. My issue is with the "running" of the PHP file. My mind is totally void of any information of how to "execute" a PHP file rather than just make it open in a browser and spit its code everywhere. I know that this PHP file works because when I am working on it in Dreamweaver and I click the "Discover" link where it says "Dynamically-related files for this document may have changed and should be re-discovered by the server" it executes this PHP file and I get my emails sent. I've tried going through command line like THIS but all it does is spit out the php code as if I was running it in a browser. Nothing actually sends. Is there some way to change the way the PHP file itself is formatted that would make it do this? What is Dreamweaver doing when I click "Discover" that makes it work? I feel like this should be a simple thing and its insane that I am not understanding it.....

Community
  • 1
  • 1
fender357
  • 37
  • 1
  • 8
  • cron job\windows scheduler, php command line. "php file.php" bingo. http://php.net/manual/en/features.commandline.php –  Jun 08 '12 at 00:06
  • If you're running it on your local machine, you need to be sure you have some kind of mail sending program configured for use with php. I'm not sure what the windows options are. – Sam Dufel Jun 08 '12 at 00:18
  • The issue is not the mail being sent. Like I said, if I hit "Discover" in Dreamweaver the mail is sent no problem. – fender357 Jun 08 '12 at 00:19

2 Answers2

3

Assuming you installed PHP with default settings, the PHP folder should be in your PATH variable. In which case, all you have to do is run the command php filename.php.

If not, you'll have to direct the command to where PHP is installed, maybe something like "C:\Program Files\PHP\php.exe" filename.php - either way, all you're doing is invoking the PHP binary on the file.

Niet the Dark Absol
  • 320,036
  • 81
  • 464
  • 592
1

You have options. If it's a web page that you can access through your browser (and that sends the email as you'd expect), you can just request that page via HTTP every five minutes. Personally, I'd do this with cURL (e.g., curl.exe http://localhost/myfile.php). You can find Windows binaries here. You could also use PowerShell or something.

If this isn't a web page, or if you just don't feel like going that route, you can execute the PHP through PHP's command-line interface (CLI), as you've already figured out. C:\path\to\php.exe myfile.php will execute your code and spit the output to STDOUT. If everything is working with this except for sending email—which I'm guessing you're doing through mail—the issue may be with your PHP configuration. By default, PHP looks for the php.ini file in these locations. You can also explicitly tell the PHP CLI what php.ini file to use with the -c option (e.g., php.exe -c C:\path\to\php.ini myfile.php). Of course, you'll need to have everything configured properly in php.ini to send mail. This can be a bit trickier on Windows than on your average Linux machine with sendmail available.

Community
  • 1
  • 1
Jonathan S.
  • 2,238
  • 16
  • 16
  • Ok, you seem to be closer to an answer that makes sense to me. The PHP file I'm trying to run contains no HTML at all, so when you "open" it in the browser it spits out a bunch of unformatted php code. Would there be a simple way to format the current php file so that when opened in a browser it would execute correctly? – fender357 Jun 08 '12 at 00:26
  • Is it as simple as adding in the HTML tags? – fender357 Jun 08 '12 at 00:27
  • When you open the file in your browser, are you just accessing it over the `file://` protocol, e.g., by right clicking and hitting Open with > Firefox? You'd need to access the PHP file through a web server that's properly configured for PHP. The URL that Dreamweaver is giving you through Discover is presumably what you'd need. – Jonathan S. Jun 08 '12 at 00:30
  • Oh HECK. Just opening it in the browser (using localhost/....) does run it correctly. I knew I was being stupid some how. That still doesn't explain why it doesn't work when I run it from CLI though.... Anyway, thank you georgefox. your wording made it click in my head. – fender357 Jun 08 '12 at 00:40
  • Check the php.ini stuff with the CLI. My guess would be it's using different configuration on your web server vs. through the CLI. – Jonathan S. Jun 08 '12 at 00:42