1

I have a .bat file on our local network that processes a file on a local shared drive and saves it back to said local drive with a specific name.

I have a .php file on our web server (hosted externally with a hosting company) that takes that file- usually uploaded via a form in a web browser- and explodes it and and enters the data into a MySQL database (also on the external web server).

I want to modify the .bat file to 'send' the file to the external php script and run the external PHP file without opening a browser window. The purpose is to automate this entire process so our end users can just run the .bat file and the external database will be updated.

So far, all I can find is how to trigger .bat from PHP, which is the opposite of what I'm looking for. Our network does not have a web server OR PHP installed and they cannot be.

I found this for executing a local php file:

    C:\PHP5\php.exe -f "C:\PHP Scripts\script.php" -- -arg1 -arg2 -arg3

I apologize if this is a stupid question- I'm a web developer and while my knowledge of web languages is great, my knowledge of languages like DOS is on par with a 3 year old.

Here's the php that I am currently working with (this will be modified a bit but the basic functionality will be the same:

include "dbcommon.php";
//connect to the database
$conn = mysql_connect($dbhost,$username,$password) or die ("Could not connect to the employee database");
@mysql_select_db($dbname,$conn);
$uploaded_file = "employees.csv";
if ( move_uploaded_file ($_FILES['infilename'] ['tmp_name'], $uploaded_file) )
{ //open uploaded file
$result = file_get_contents($uploaded_file);
if ($result === false)
{
    $error = "Could not read $uploaded_file.<br>";
}
else
{
    $error ="";
    $query = "DELETE FROM employees";
    $delresult = mysql_query($query,$conn);
    if (!$delresult)
        $error .= '<br>Error deleting existing employee records.';

    $lines = explode("\r\n", $result);
    foreach ($lines as $line)
    {
        $field = explode(",", $line);
        $employee_id = str_replace('"','', $field[0]);
        $last_name = addslashes(str_replace('"','', $field[1]));
        $first_name = addslashes(str_replace('"','', $field[2]));
        $supervisor_id = str_replace('"','', $field[3]);

// If all required fields have a value, insert the row
        if ($employee_id != "" && $last_name != "" && $first_name != "")
        {
            $query="INSERT INTO employees (employee_id, last_name, first_name, supervisor_id) VALUES ('$employee_id', '$last_name', '$first_name', '$supervisor_id')";
            $result = mysql_query($query,$conn);
            if (!$result)
                $error .= '<br>Error inserting "'.$employee_id.'","'.$last_name.','.$first_name.'","'.$supervisor_id.'"';
        }
// If any required field has a value set, report the error
        elseif ($employee_id != "" || $last_name != "" || $first_name != "")  
        {
            $error .= '<br>Error inserting "'.$employee_id.'","'.$last_name.','.$first_name.'","'.$supervisor_id.'"';
        }   
    }
    if ($error == "")
        $error = "Successfully uploaded and inserted the employee records.";

}

} else { 

 $error = "";
 switch ($_FILES['infilename'] ['error'])
 { 
     case 1:
          $error .= '<p> The file is bigger than this PHP installation allows</p>';
          break;
     case 2:
          $error .= '<p> The file is bigger than this form allows</p>';
          break;
     case 3:
          $error .= '<p> Only part of the file was uploaded</p>';
          break;
     case 4:
          $error .= '<p> No file was uploaded</p>';
          break;
 default:
    $error .= '<p>'.$_FILES['infilename'] ['error'].'</p>';
    break;
 }


}
echo $error; 
//close the database connection
mysql_close($conn);

Any help is greatly appreciated!

JasonMArcher
  • 14,195
  • 22
  • 56
  • 52
Dan Kaufman
  • 115
  • 1
  • 13
  • if you don't have PHP installed locally, exactly how are you planning on executing your script locally? Plus, your script is vulnerable to [sql injection attacks](http://bobby-tables.com) – Marc B Aug 18 '14 at 17:51

1 Answers1

0

I think you might be looking for one of the following:

CURL

Run cURL commands from Windows console

http://howes-it-going.com/curl_Windows_Example.html

How to post PUT request under the Windows using curl?

send/post xml file using curl command line

SOAP / REST

Representational state transfer (REST) and Simple Object Access Protocol (SOAP)

SOAP Third-Party:

WinBatch - commercial, excellent support

AutoHotKey - free, not-so-hot support

biterScripting - unknown

In particular, I think you might find something in cURL for DOS, or via CygWin for Windows (which provides a better cURL implementation).

Community
  • 1
  • 1
cssyphus
  • 37,875
  • 18
  • 96
  • 111
  • Oh man- I think this cURL might just be what we need! thank you so much for this!I don't have enough reputation points to vote this up but I +1'd you in my heart. – Dan Kaufman Aug 18 '14 at 17:52
  • You are most welcome. Glad to help as so many others have helped me. – cssyphus Aug 18 '14 at 17:53