如何从Windows命令行(bat文件)执行外部PHP文件

时间:2014-08-18 16:48:31

标签: php windows batch-file command-line

我的本​​地网络上有一个.bat文件,用于处理本地共享驱动器上的文件,并将其保存回具有特定名称的本地驱动器。

我的网络服务器上有一个.php文件(在托管公司外部托管),该文件通常通过Web浏览器中的表单上传 - 并将其爆炸并将数据输入MySQL数据库(也在外部Web服务器上。)

我想修改.bat文件以将文件'发送'到外部php脚本并运行外部PHP文件而无需打开浏览器窗口。目的是使整个过程自动化,以便我们的最终用户只需运行.bat文件,外部数据库就会更新。

到目前为止,我所能找到的是如何从PHP触发.bat,这与我正在寻找的相反。我们的网络没有安装Web服务器或PHP,但它们不能。

我发现这是为了执行本地php文件:

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

如果这是一个愚蠢的问题我很抱歉 - 我是一名网络开发人员,虽然我对网络语言的了解很好,但我对DOS等语言的了解与3岁时相同。

这是我目前正在使用的PHP(这将被修改一下,但基本功能将是相同的:

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);

非常感谢任何帮助!