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.

                    5 days later

                    Alright - I've come to terms with most of it and I have decided to completely drop using a database to check if the file exists already, but instead just send me an e-mail whenever a file is uploaded. I've also added to the script, so $mode is identified each time for example either with 1 or 2 depending on the kind of file that has been uploaded.

                    The problem now though is that it won't send the e-mail seeing as how it gives me an error stating the following:

                    PHP Warning: mail(): Failed to connect to mailserver at "localhost" port 25, verify your "SMTP" and "smtp_port" setting in php.ini or use ini_set() in C:\mail_old.php on line 116

                    Line 116 looks like this:

                    mail($to, $subject, $message, $from);

                    How do I set the SMTP and smtp_port setting correctly so it will work? It should be mentioned however that this script will be executed on a server that doesn't have an Apache or IIS server installed seeing as how I have an IIS server installed and configured on another server. Is it possible somehow to "redirect" the script from one server to another so the mail script can be executed properly?

                    Edit:

                    I tried switching "localhost" with our normal e-mail server name called "trisrv01", and then I got the following error:

                    PHP Warning: mail(): SMTP server response: 501 5.5.4 Invalid Address in C:\mail_old.php on line 116

                    As far as I can see, there shouldn't be any problems with the address seeing as how $to refers to an e-mail address and the other variables speak for themselves as well.

                      a month later

                      Any help on this matter is really appreciated. I'm really not sure what to do here!

                      Edit.

                      Never mind - I finally figured it out. Turns out I forgot to remove the ; in the sendmail_from variable in PHP.ini and write an e-mail address there as well. Now it seems that everything's running fine!

                        Write a Reply...