running multiple commands in php through system() - php

I'm successfully able to execute with this following command:
system('C:/Program Files/PSPP/bin/psppire.exe ');
i want to do something like opening a file through this exe e.g.
system('C:/Program Files/PSPP/bin/psppire.exe, C:/xampp/htdocs/csv/txtfiles/PSPPfile.txt');
This above command should open txt file in psppire.exe!
Help me out! Thanks.

For Windows OS(according to your example) - separate path for executable file and path for target file with space.The working example is shown below:
system('"C:\Program Files (x86)\Notepad++\notepad++.exe" "C:\arsetup.log"');

It looks like you have a comma in the name of the command you're using. That's a problem in and of itself. I don't think the name of that executable is "pspire.exe,".
Also you may want to quote the different paths because they contain spaces. So it maybe should become
system('"C:\Program Files\PSPP\bin\psppire.exe" "C:\xampp\htdocs\csv\txtfiles\PSPPfile.txt"');
Also you should note that I used backslashes, which is the correct directory separator for Windows. To make this universal you can use the constant DIRECTORY_SEPARATOR

Related

How can I call a .bat file instead of writing the whole path to the .exe?

I have two PHP versions that I want to execute depending on the situation. I have setup PATH so that when I type php there is executed the right version of PHP in the folder C:\xampp\php.
However, I also have an older version of PHP in the folder C:\old-xampp\php. Of course, I can't add also that folder path to PATH as there would be two folders with a file with the name php.exe and used would be always php.exe in first folder in PATH.
At the moment I have to type C:\old-xampp\php\php my-command-here every time I want to execute my old PHP.
Is there a way to create a .bat file in a PATH folder with the name old-php.bat that would act as if I typed C:\old-xampp\php\php?
I am open to another method to do it too.
There can be written into a batch file old-php.bat stored in a folder of which path is listed in string value of environment variable Path the command line:
#C:\old-xampp\php\php.exe %*
%* references all arguments passed to the batch file exactly as passed to the batch file. This is explained by the usage help of command CALL output on running call /? in a command prompt window. Argument 0 is the string used to start the batch file which is not included by %*.
It would be also possible to create in a folder of which path is listed in string value of environment variable Path a hard link or a symbolic link with a name like old-php.exe which links to C:\old-xampp\php\php.exe by using once MKLINK.

How to get/set correct relative paths in PHP with a lot of directories?

I have a problem finding the right path for a file. Here is my example. (I run Windows and WAMP)
My main file is placed here:
/homedir/subdir2/subdir3/index.php
It has to look for the file placed here:
/homedir/subdir1/images/slides/image.png
My code is:
/homedir/subdir2/subdir3/../subdir1/images/slides/image.png
Go up twice to get to /homedir/
../../subdir1/images/slides/image.png
Your code should be:
/homedir/subdir2/subdir3/../../subdir1/images/slides/image.png
Note double ../ here.

What is the best practice to include files in PHP?

I just faced that code
include('../wp-load.php');
Worked just if script called from web-browser (equals to 'from Apache'). If I'm running the same script from cron or from command line I receiving error
Warning: include(): Failed opening '../wp-load.php' for inclusion (include_path='.:/usr/local/php54/pear') in /home/myfolder/public_html/exe/myscript.php on line 6
However it worked if I modify the include in the following way:
include(__DIR__.'/../wp-load.php');
It works from both: command line and from browser.
In the same time I begun to be scared to use includes from the directory where the script placed. So if tools.php placed in the same directory is it safe to use:
include('tools.php');
Or it would be better to add __DIR__?
include(__DIR__.'/tools.php');
At the top of the script, or in the file that you always include at the beginning, define constant or variable that is the path to your directory root.
You should define two paths:
one from your machine, for example:
define('SERVER_PATH', '/var/www/');
this will be used for including php script files in your php script
You will just use it like include(SERVER_PATH.'tools.php') anywhere in your code.
You can also use __DIR__ instead, if you have PHP >= 5.3.0
And second - url path:
define('URL_PATH', 'http://www.xxxxxx.com/');
this will be used for things by your page for http requests like images, javascript files, css files etc.
Cron has its own working directory, but you can change it with cd:
cd /var/www/vhosts/domain.com/httpdocs
You also need to know you can execute multiple commands in cron job like this:
command1 && command2
So command1 can be the cd and command2 your php.
Then you don't have to add __DIR__ and a cron job will work just like normal PHP.
I know this doesn't really answer your question, but it does solve the problem that made you ask your question. Personally I don't think there's a good reason to always use absolute paths in the includes, relative paths will do just fine in most cases.

Using php oncli few questions ~ using XAMPP

Attempting to expand my knowledge by using PHP on the Command Line.
Currently I have a default installation of XAMPP, and have set up my Environment Variable.
I've been able to execute simple scripts like:
<?php echo 'hello world!'; ?>
Questions ~
Where do I store the scripts I am using? Currently I am doing:
C:\Users\Ross>php c:\helloworld.php
it works. Does this mean I need to specify a path every time? Or should I store php files inside my c:>xampp\php directory? I tried this and it doesn't appear to work.
What would be the accepted "best practice".
2nd question
Could someone explain why this doesn't work:
<?php
fwrite(STDOUT, "Enter file name:\n");
$file=fgets(STDIN);
print 'you entered...' . $file;
$fp=fopen($file,'r');
if(!$fp)
{
print 'File could not be opened..';
}
else
{
/* show file pointer */
print($fp);
}
?>
and then I do:
C:\Users\Ross>php c:\file.php
Enter file name:
c:\foo.txt
you entered...c:\foo.txt
Warning: fopen(c:\foo.txt): failed to open stream: Invalid argument in C:\file.php on line 6
File could not be opened..
"foo.txt" is in the same directory and does exist.
thanks for any clarification.
As far as were to store the files is concerned: I normally add the directory where php.exe is to my PATH environment variable, that way I can just call php in whatever directory contains the script I need to run. If you don't add the directory to PATH, then you would need to either run php from its directory and specify the full path to the PHP script, or run it from the directory where the PHP script is and specify the full path to the PHP executable.
Regarding opening the file: the reason this is occurring is because fgets is returning the newline from you pressing enter, too (it would seem). So in reality, it's trying to open a file whose name actually ends with a new line character.
Change the line:
$file=fgets(STDIN);
to:
$file=trim(fgets(STDIN));
and you should be fine.
question #1: all your php files should be inside the www folder of xampp (c:\xampp\www)
question #2: probably because you are not working in the correct folder.
xammp is good but I recommend you to use wamp, it's much easier to understand and use. Just google for it. xampp is more for those who are more techically skilled.

Equivalent of running $_SERVER['DOCUMENT_ROOT'] when running php in terminal

I was wondering if there is some variable that will return what $_SERVER['DOCUMENT_ROOT'] returns when I call PHP like this: ./somescript
If not, how do people get around this case? I'm looking to be able to call the same script (a template compiler) in both the web browser and the terminal.
Thanks!
Matt Mueller
I don't recommend the getcwd() command for this because you cannot be sure of where cwd is pointing unless you've made a chdir before (which mean you already know which directory you're in). Working directory can be define in php config and vary between apache and CLI, and some php frameworks changes it on startup.
Use dirname(__FILE__) it always works wether you're in a apache or cli context (plus it works on windows and unix), and if you need to move inside your project files you can just use relative paths.
I think you should use getcwd function to obtain current directory (or just dirname(__FILE__) if your script is the top one). Then you only need to be sure to run the script from your DOCUMENT_ROOT. Something like this:
cd /var/www/
php ./scripts/top.php
hardcode document root in it.
hardcode is always a solution

Categories