Not able to execute batch file in php - php

I m trying to execute batch file in php file. I m using Apache server. Itried following ways but its not working
PHP Code
echo shell_exec('download.bat');
echo exec('download.bat');
system ("cmd /c download.bat");//Also tried for exec and shell_exec
Batch file contains downloading code using ftp client
Batch file
"c:\program files\coreftp\coreftp.exe" -s -O -site mysite -d /Export/*.* -p C:\wamp\www\file\txt
If I run it in cmd or run directly then its works fine when I run its in php its just write or echo batch file's code
download.bat file is in same folder.
I also tried to call simple bat file
start "link" "https://www.google.co.in/?gfe_rd=cr&ei=NzuIVI-FG6aG8Qef44CAAw"
Its also not calling to this bat file

Are you sure that your 'download.bat' file is in the same PATH as your PHP script?
Try to use absolute path like this
exec('C:\\MY\\PATH\\TO\\download.bat');

Just use exec('download.bat'); if the file is in the same directory, however you need to ensure that Apache has correct permissions to execute the batch file i.e. it should be running on an Administrator account. If you're on Win7 or above, look at how to run programs in elevated mode.

Related

Not able to execute copy command in bat from PHP

I am trying a really simple thing in PHP. I want to execute an external bat file from PHP script, which is invoked from web server (Apache with WAMP). Following is the content of bat file named savereport.bat
echo %PATH% > C:\project\mypath.txt
cp D:\books\Ant\1.pdf D:\books\Ant\2.pdf
The bat file when executed manually works fine, it creates mypath.txt in C:\project directory and 1.pdf is copied to 2.pdf correctly.
However, when I run the same bat file from PHP with system(), only the first statement completes, which means it does create C:\project\mypath.txt, but strangely it does not copy 1.pdf to 2.pdf.
This is the PHP code I am using
<?php
system('C:\wamp\www\savereport.bat');
?>
I have also tried exec(), passthru, same result in all the case. Please help.
cp is not a windows command, it's probably a Powershell alias, replace the cp with copy.

Can't properly execute php file within bash file if in differenct directory

So I have a a php script I'd like to run from a bash file script.sh. This is the code:
#!/bin/bash
/usr/bin/php /var/www/html/folder/file.php
The php file sends push notifications to an iOS app. The only way it works for me is if my current directory is /var/www/html/folder/ and the script is also in this
directory. So if I cd .. and try to do
x#x:./folder/script.sh
I get an error from the php file saying "unable to connect to ssl://gateway.sandbox.push.apple.com:2195(Unknown error)". I've also changed the permissions for both the php and bash file to rwx for all. Any ideas why it fails if everything isn't in the same directory?(Using Ubuntu)
As Etan Reisner pointed out, my php file included credentials/keys referenced locally. Referencing them with absolute paths fixed the problem.

php script to run bash script which runs scrapy crawler

I have a form in html page that has action to run php file. The php file has to run a bash function command which in turn runs a scrapy spider. Since I have the scrapy spider located outside var/www/, I have added a function in .bashrc to run the bash command ($ startscript) from anywhere. When I run it on the terminal from var/www folder or anywhere it works as expected but when I do it in php file it does not work. I am not sure if its because of php file permission, scrapy proprieties or something else.
Any suggestions?
.php file:
<?php
$output = shell_exec('startscript');
echo $output;
?>
.bashrc file:
function startscript
{
cd /home/pi/IndeedCoUkCrawl
./BotScrapy.sh
}
So what you can try:
put the absolute path of the startscript in shell_exec and see if it works.
try running with error_reporting(E_ALL) and see if this give any error/notice
check if your webserver has permissions to run the file. Try putting the file into the same group as the webserver runs in.

Getting exec() PHP to work like it does on the command line

The following command, which generates a .pdf file from a .tex file, works from the command line but not when I run it with PHP. The file has the appropriate permissions and I'm able to run other commands with exec() so not sure what is going on.
$file_path='uploads/some-path';
$full_path='uploads/some-path/file.pdf';
$cmd ="pdflatex -output-directory ".$file_path.' '.$full_path;
exec($cmd);
The flag -output-directory place the file in the file_path rather than the root directory.
Is pdflatex in the search path? Perhaps try specifying the full path to the executable and see if that makes a difference.

How to run a php script through batch file for windows 7?

I have 2 php files. One which stores various xml files in a database and other php files takes those xml files as input, parse them and produces a another table having various information about the xml files.
I want this work to be scheduled for everynight.
I want to do this via batch files but I have no idea how to use .bat files, and how to incoorporate the php script in it.
Need help/guidance.
Thanks.
Yogesh
I assume you are doing this on a windows machine as you mention a .bat file
PHP can be executed from a command line e.g. a dos shell
http://www.php.net/manual/en/features.commandline.introduction.php
Once you get it working manually just put what you are typing in a text file. (.bat)
You can execute the .bat file (a program file to windows) when you want using a scheduler, e.g. http://windows.microsoft.com/en-GB/windows7/schedule-a-task
create start.sh add this code:
#!/bin/bash
php /path/to/folder/script.php
chmod 644 script.php
OR
create start.sh add this code:
#!/bin/bash
/path/to/folder/script.php
chmod 755 script.php
the first line of your script.php has to be:
#!/usr/bin/php

Categories