如何将php变量传递给Windows批处理文件

时间:2015-03-11 07:48:34

标签: php windows batch-file ftp

我想通过php执行我的批处理文件,将一些值从php传递到批处理文件。批处理文件的目的是将csvs文件导出到ftp.by命令行它完美但但不能用php.given下面是我的批处理和PHP文件

test.bat的

@echo off
set ftp_domain=%1
echo FTP Domain to Connect to %ftp_domain%
set ftp_username=%2
echo FTP User Name %ftp_username%
set ftp_password=%3
echo FTP Password %ftp_password%
set data_folder=%4
echo Folder to upload Files from %data_folder%
del ftpscript.txt
echo open %ftp_domain%>>ftpscript.txt
echo %ftp_username%>>ftpscript.txt
echo %ftp_password%>>ftpscript.txt
echo !:--- LOGGED INTO FTP --- >>ftpscript.txt
echo pwd >>ftpscript.txt
echo !:--- CHANGE DIRECTORY TO MAGENTO-CONNECTOR FOLDER --->>ftpscript.txt
echo cd Magento-Connector>>ftpscript.txt
echo !:--- CHANGE DIRECTORY TO DATA FOLDER --->>ftpscript.txt
echo cd data>>ftpscript.txt
echo !:--- SET LOCAL DATA DIRECTORY FOR FILE UPLOAD --->>ftpscript.txt
echo lcd %data_folder%>>ftpscript.txt
echo !:--- SET FILE TRANSFER MODE TO BINARY --->>ftpscript.txt
echo binary>>ftpscript.txt
echo !:--- RECURSIVELY UPLOAD FILES --->>ftpscript.txt
echo mput "*.csv">>ftpscript.txt
echo !:--- LIST OF FILES UPLOADED INTO FTP --->>ftpscript.txt
echo dir>>ftpscript.txt
echo !:--- DISCONNECT --->>ftpscript.txt
echo disconnect>>ftpscript.txt
echo !:--- EXIT FTP --->>ftpscript.txt
echo bye>>ftpscript.txt
echo Start Login to FTP Server
ftp -i -s:ftpscript.txt
echo Delete FTP SCRIPT FILE
del ftpscript.txt
echo End of Operation....exiting....

php文件

<?php
$var1 = "domain.com"; //this is domain
$var2 = "username";   //this is username
$var3 = "password";    //this is username
$var4 = "C:\myCSV_FolderPath";
$bat_file = "test.bat".escapeshellarg($var1)." ".escapeshellarg($var2)." ".escapeshellarg($var3)." ".escapeshellarg($var4);
$output = null;
exec($bat_file, $output);
?>

当我运行我的php文件时,它不会给出任何结果,我的csv文件也不会转移到ftp。

1 个答案:

答案 0 :(得分:4)

我认为你只是在第一个变量之前错过了一个空格:

$bat_file = "test.bat ".escapeshellarg($var1)." ".escapeshellarg($var2)." ".escapeshellarg($var3)." ".escapeshellarg($var4);