Do i have to make any changes to my php code which i currently run under IIS to be able to run the script from the command line?

Can any one help?

    What script?

    Does your script expect to get values passed into it from a form? If so, how do you intend to get those values passed into it without the form?

      No, no code changes.

      But you do have to run your scripts in the php.exe directory.

      For example:

      c:\php\php.exe script.php

      I loved it when I found this feature. It will allow me to automate a special e-mailing task I i will be putting into production soon. Great stuff.

        No, no code changes.

        how do you know that without seeing the code? as stated, what if the script expects values posted to it? what if the script relies on sessions?

          Well, the PHP scripts I have created don't need to be shown in a browser.

          Mine so far are complete for automation purposes.

          The scrips I have grab e-mail addresses from a database, then sends out the e-mail. Not only that, the PHP script checks to see if the e-mail is even needed. IF not it won't send the e-mail.

          So I have put the script on a Schedule to check to see if e-mail is needed twice a day.

          Thus in my case, there is no input fields etc. I am simply making use of the power of PHP, being able to schedule it, and the PHP E-mail Functions.

          There are 100's of ways to do this, but I found PHP to be the most helpful for my problem.

          So basically, this script is completely independent of any browsers or need for sessions. IN essence, these are simply Batch files.

          Does that make sense?

            yes the code uses sessions and alot of passing data from forms e.g. enter search parameters into a from which are then used to query a database.

            example

            I use sessions to find who is logged on and allows them to query a database by selecting search parameters.

            Would it make any difference if i ran the code under apache or any other web server???

            Sorry i am new to php!!!

            THANKS

              well forms dont exist on the command line, so your script will not work on the command line.

              command line programs generally get there input from either arguments passed to the script when it is called, or by prompting the user.

              Would it make any difference if i ran the code under apache or any other web server???

              in most cases it shouldn't.

                ugg, what is your purpose to run php code from the command line?

                I can't think of any off hand, since most of your interaction will take place when your users submit a form.

                What's that idea of using any of that at the command line? What are you trying to gain?

                I'm just asking for your ideas, as perhaps there is something I could use from it.

                  Hi DoctorWho,

                  I was just exploring possibilities,

                  ugg

                    I have done scripts to process emails, um it is actually pretty easy to set them up, i actually have it done through 2 scripts that work together, one is the main script, and it then forks out the queries to be processed by another script.

                    This is done for often i have lots on information to process at once, and it stop the script from crashing as many servers have a process limit for php scripts.

                    You can have this setup through a cron job, to automate your script at allow everything to be done while you sit back and relax.

                      10 months later

                      I'm having the exact same problem as ugg had when beginning this thread. I normally use PHP using IIS. However, now I need to use PHP to execute a script on the command line seeing as how the script I made doesn't need any user based data as it only opens a browser, runs a script and then closes the browser again.

                      Here's a little description of what I'm trying to do.

                      I have a FTP-server running where a company uploads files to us during the day. Every time a file is uploaded, I want to receive an e-mail stating the filename and filesize and what kind of file it is. I have put a batch-file to execute everytime a file is uploaded which takes the filename and filesize and put them into variables. I have created the entire PHP-script that generates the e-mail and it all works perfectly except for one thing. As of yet I could only figure out to execute the PHP-script through IE (via "start http://www.mywebsite.com/script.php?file=%1-%2" for example) and then close the browser once the script was executed. But since this company sends over 50-100 files a day, IE or the command prompt sometimes have trouble coping with it all, so they start to hang meaning I might see 50 command prompts and 15 IE's open at the same time and that of course slows down the computer.

                      Based on the above - I want to be able to run the script directly via php.exe so I don't need to start an IE-browser.

                      I have tried writing "c:\php\php.exe -f c:\script.php?file=%1-%2", but that didn't work. It said the following: "Could not open input file c:\script.php?file=xxx.txt-1000". Is it because the php-file can't use the variables in order to load or...? What do I do in this case?

                      /// NetRoam

                        The problem is with your variables.

                        Passing variables in via the ? is a HTTP GET standard. When you call the script directly from the commandline you're not using HTTP, and so the variables don't get handed to PHP in th way it expects.

                        What you need to do instead is to pass the variables the same way as you would with a normal command line command, by listing the values after the command line... something like:

                        c:\php\php.exe -f c:\script.php value1 value2

                        Your script then needs to pull those values from the commandline and based on the order in which they appear, assign them to internal PHP variables.

                        The complete manual on the topic is to be found here:
                        http://ie2.php.net/manual/en/features.commandline.php

                          Alright - so far so good. I'll try calling the variables like you instructed me to. Seeing as how I'm pretty much a newbie with this, how do I write my PHP-code so it can split the 2 values up in 2 different variables?

                          Thanks in advance.

                          /// NetRoam

                            Did you look at the manual page, I suggested?

                            $argc will contain a count of the arguments (variables) passed to your script, and they will be stored in the $arg[] array.

                            See example 43-1 in the page I linked above

                              Alright - I think I got it working - but I just encountered another problem.

                              It seems that when I made my PHP script back in the day, I connected to a MySQL database and inserted the filename and filesize in a table there. The script would then each time it is being run check whether or not the same filename and filesize already is in the database. If so, it will NOT e-mail me, but if it isn't in the database it will be inserted and a mail will be forwarded to me.

                              So the problem right now is that the script calls to an undefined function in the PHP-file I have - which I actually linked to in the PHP-file.

                              Is that a big problem to get around or is there another solution which is better?

                              /// NetRoam

                                I'm afraid that I don't fully inderstand your problem.

                                If your function is undefined, you need to define it?

                                  Indeed I should. At the top of my PHP-script I have the following lines:

                                  include("../common.php");
                                  connectDatabase();

                                  At first I just changed the "../common.php" location to the location where it actually is, but as I call the function "connectDatabase();" which IS defined in the common.php file it tells me that it calls to an undefined function. I tried to put the entire function inside the PHP-file and drop the "include" line but that didn't seem to work that well.

                                  I tried copying the common.php to the same directory as the PHP-file and that seemed to kill the error message. I just can't get the script to send me a mail. I wrote the function in the command line and it seems to run for a short second, then goes back to c:\php without error messages. But nothing seems to happen. Dunno what to do next.

                                    I don't understand what you mean when you say you changed the location to "location where it actually is".

                                    Have you tried using the absolute path to where the file is? I haven't used PHP on a Windows environment but in *nix you could specify the location beginning with a "/", eg "/var/www/includes/common.php". IN windows it might be "C:\www\whatever"...?

                                    Includes work exactly the same way, regardless of whether you run in a browser or from the command line. The only difference is that you may run it from a different directory. Using an absolute path should make this issue go away.

                                    If you've referenced the absolute path for common.php, make sure that common.php doesn't also use a relative ("..") path for some file which IT is including.

                                      Well - what I meant was I changed the location to the absolute path of the common.php. Anyway - it didn't work anyhow, so...

                                      As I wrote before - I tried to copy the entire function (which was in common.php) into the PHP-file and it seemed that the error message disappeared - yet I don't get any real output out of it (as in no mail). I'll try pasting the code for you - then maybe it might be a bit easier to figure out the problem.

                                      Example from PHP.net - a bit modified

                                      if ($argc != 2 || in_array($argv[1], array('--help', '-help', '-h', '-?'))) {
                                      ?>
                                      
                                      This is a command line PHP script with one option.
                                      
                                        Usage:
                                        <?php echo $argv[0]; ?> <option>
                                      
                                        <option> can be some word you would like
                                        to print out. With the --help, -help, -h,
                                        or -? options, you can get this help.
                                      
                                      <?php
                                      } else {
                                         $file = $argv[1];
                                      }
                                      

                                      The above outputs the variable I set when I for example use the following execution in the command line:

                                      C:\PHP>php.exe c:\mail.php 2345.txt-123

                                      meaning the variable "2345.txt-123"
                                      This is the variable I want my PHP to split up in 2 so I have 2 variables in my PHP. One with 2345.txt and one with 123. My PHP-file should then go through various scripts to determine if a mail should be sent out or not (meaning it should connect to a database - check to see if the variables already have been inserted in the database - and then send a mail if it isn't. Also - if it isn't in the database, it should be inserted, while sending the mail - that way the same variables can't be inserted nor e-mailed twice. Below I have written the code I use to determine whether the variables should be e-mailed or not.

                                      $pieces = explode("-", $file);
                                      
                                      $mode = $pieces[2];
                                      $filename = $pieces[1];
                                      $filesize = $pieces[0];
                                      
                                      if($filesize != "0")
                                      {
                                        $query = "select * from files where filename = '$filename'";
                                        $result = mysql_query($query) or die("".mysql_errno()." Error: ".mysql_error());
                                      
                                        if (mysql_num_rows($result) > 0)
                                        {
                                          while($row = mysql_fetch_array($result))
                                          {
                                            header("Location: mail2.php"); // This closes the browser without doing anything
                                            exit;
                                          }
                                        }
                                        else
                                        {
                                          $query2 = "insert into files values ('', '$filename', '$filesize')";
                                          $result2 = mysql_query($query2) or die("".mysql_errno()." Error: ".mysql_error());
                                      
                                      $query3 = "select * from files order by fileID desc limit 1";
                                      $result3 = mysql_query($query3) or die("".mysql_errno()." Error: ".mysql_error());
                                      
                                      if (mysql_num_rows($result3) > 0)
                                      {
                                        while($row = mysql_fetch_array($result3))
                                        {
                                          $filename = $row['filename'];
                                          $filesize = $row['filesize'];
                                      
                                      
                                          if($filename != "." && $filename != "..")
                                          {
                                      
                                            $to = " (e-mail addresses) "; // The person the e-mail is sent to
                                            $subject = " (subject line) "; // E-mail subject
                                            $from = "Content-type: text/html\r\n";
                                            $from .= "From: (Company) "; // Name and e-mail of the sender
                                      
                                            // The e-mail message
                                            $message = "<html><script language=\"JavaScript\" src=\"http://www.mywebsite.com/javascript.js\"></script><link rel=\"stylesheet\" href=\"http://www.mywebsite.com/style.css\" type=\"text/css\"><body>\r\n";
                                            $message .= "<span class=\"headline\">Headline</span>\r\n";
                                            $message .= "<br><br><b>There is a new file uploaded for you!</b>\r\n";
                                            $message .= "<br><br>It is called <b>$filename</b> and the filesize is <b>$filesize</b> bytes\r\n";
                                            $message .= "</body></html>\r\n";
                                      
                                            mail($to, $subject, $message, $from);
                                      
                                            header("Location: mail2.php"); // This closes the browser without doing anything
                                            exit;
                                          }
                                          else
                                          {
                                            header("Location: mail2.php"); // This closes the browser without doing anything
                                            exit;
                                          }
                                        }
                                      }
                                        }
                                      }

                                      I can imagine the script should probably be modified somewhat but I don't know exactly in which way it should be modified in order to work

                                      /// NetRoam

                                        Your argument is 2345.txt-123.

                                        When you explode this on the hyphen, you get:

                                        $filesize = "2345.txt";
                                        $filename = "123";
                                        $mode = "";

                                        I assume that's not what you intended... but continuing on...

                                        If there is a row in your table for the filename you issue a HTTP header which will have no impact and will not do anything when run from the command lineinstead of through HTTP.

                                        If the row doesn't exist, you insert it and then do a select, send the mail and try to do a header redirect again (which won't work).

                                        I don't think that the mail is your problem. I think it may be how you assign your variables.

                                        Try adding echos into your script to see how it runs. Use echos to display the values for filesize filename and mode for example, and on every IF statement use echos to show you whether it's going into the if clause or the else clause.

                                        In short, you need to spend some time debugging.