Not able to execute copy command in bat from PHP - 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.

Related

execute php files without typing php in front

Anyone can assist me on how to execute my php files without preceding php front
Examples instead of typing 'php filename.php' in cli or cloud9
I want to execute them by just typing the file name
command: 'filename'
then the script runs
Well i was thinking of using the htaccess file but i'm not sure if that also works on vps or cli. I know of browser only
Edit your PHP script and add this as the new first line, by itself, before your opening <?php tag:
#!/path/to/php
(Replace that with your actual path.)
Alternatively, if you have the env command, you can use that to figure it out for you:
#!/usr/bin/env php
So your script looks like this:
#!/usr/bin/env php
<?php
echo "Hello, world!\n";
Then make the file exectuable:
chmod u+x filename
Now you can just "run" it directly in the shell:
% filename
Hello, world!
Note, you only want to do this if the script is CLI-only. If it's ever run via the web server, that first line will be output to the browser.

Not able to execute batch file in 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.

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

Can I execute the "cd" command to change directories using PHP CLI?

I created a script using PHP CLI that I would like to have cd me into a directory based upon my command line input.
While I can get the PHP execution commands to echo output (e.g. echo ls -al), I cannot get them to run cd.
I have searched a lot online to find the solution, but have come up empty.
You can't use cd as it would run in a subshell, and the changed working directory would be lost when you returned to PHP before issuing the next command.
Use chdir instead.
You need to run chdir from php, running cd from exec, system, shell_exec etc. only change directories in subprocesses called by php, every new system call will start in php current working directory.

Problem executing bash file

HI there!
I've run into some problem while learning to combine .sh files and PHP. I've create a file test.sh and in that file I call a PHP file called test.php.
If I double click on the .sh file then it runs perfectly but when I try to run it from the terminal I get "command not found". I'm in the exact folder as my .sh file but it wont work. Here's my test.sh:
#!/bin/bash
LIB=${0/%cli/}
exec php -q ${LIB}test.php one two three
exit;
When I doubleclick on the test.sh file then it returns the argv array like it suppost to. But why can't I run it from terminal?
use ./filename.sh
no matter if your are in the same folder or not, without giving ./ the system is searching the path variable. (/bin/, /usr/bin and so on)
Is execute bit enabled?
chmod +x test.sh
Your $PATH variable may not include '.' - so the shell may not be able to find the command to run.
As others have said, sh ./file.sh will work...
It is possible that your environment is different when launching from the Terminal and when launching via a double-click. Try executing which php and echo $PATH from the Terminal to see what the deal is.
EDIT 1
As others have noted, if your "command not found" is referring to the shell script and not php, then you probably forgot to include the "./" before the name of the script (i.e. ./test.sh). Also, don't forget to make it executable by invoking chmod a+x test.sh. The reason for this is that PATH does not include the current directory (i.e. "."), because doing so would be a security risk (e.g. folders with a ton of files in them including a fake "ssh" which could then intercept your password or the like).
EDIT 2
Also, I don't know about you, but ${0/%cli/} is evaluating to -bash from within my Terminal. Are you sure that's what you wanted it to expand to? Perhaps you should specify the exact filename.
Another option is to run it with sh (or bash, if sh on your machine isn't bash and the script uses bashims)
sh filename.sh
bash filename.sh
This will work whether or not the file is executable or in $PATH

Categories