Priyank Priyank - 4 months ago 7
PHP Question

how to Pass php variables to a windows batch file

I want to execute my batch file through php with passing some values from php to batch file.the purpose of batch file is to export csvs files to ftp.by commandline it works perfectly but not with php.given below is my batch and php files

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 file

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


when I run my php file it won't give any result and my csv files also not transfer to ftp.

Answer

I think you're just missing a space before the first variable:

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