php exec vbs脚本只能在循环中第一次使用
我正在脚本来将Excel文件转换成CSV文件。 Excel文件在下载文件夹中,并以“START”开始。 只有第一个文件被转换,然后PHP仍在运行,但没有任何反应。 从浏览器运行它,我可以看到这个圆圈永远在运行,就像加载网站一样。 这是代码:
$path = "C:\Users\\tom\Downloads\\"; foreach(glob($path."*.xls") as $excel_file) { $substr = substr($excel_file, 24, 5); if($substr = "START") { $csv_file = str_replace(".xls", ".csv", $excel_file); exec("$path"."csv.vbs $excel_file $csv_file"); } }
我使用的脚本是csv.vbs
脚本,代码是:
if WScript.Arguments.Count < 2 Then WScript.Echo "Error! Please specify the source path and the destination. Usage: XlsToCsv SourcePath.xls Destination.csv" Wscript.Quit End If Dim oExcel Set oExcel = CreateObject("Excel.Application") Dim oBook Set oBook = oExcel.Workbooks.Open(Wscript.Arguments.Item(0)) oBook.SaveAs WScript.Arguments.Item(1), 6 oBook.Close False oExcel.Quit WScript.Echo "Done"
使用shell_exec
是一样的。 这里有什么问题?
根据php.net,$ substr不等于“START”
substr ( string $string , int $start [, int $length ] )
START是5个字符
$substr = substr($excel_file, 0, 5);
并有一个错误的比较尝试
if($substr = "START")
正确的比较是通过使用==或===进行严格的比较
if($substr == "START")
删除了csv.vbs
的最后一行
WScript.Echo "Done"
它工作正常