PHP file path from Command Line different - php

If I run this simple code through the browser, it works normally and creates 'text.txt' in the same location as the script.
<?php
$op = fopen('text.txt', 'w');
fwrite($op, 'test');
fclose($op)
?>
But if I run with command line
php script.php
it creates the file in a completely different directory.
Why does the same script behave differently by running the browser and the command line ?!
To fix this I need to put the full path: fopen('/home/user/site.com/subfolder/text.txt', 'w');
I have a script with many lines and many file paths, it would be too much work to change all. How can I fix this problem without putting the complete path in the file.

Open Terminal and type:
cd /home/user/site.com/subfolder
then run:
php script.php

start_script.sh
#!/bin/bash
cd /to/location/of/script
php script.php

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.

Execute PHP routines on Windows Server [duplicate]

How can I create a scheduled task to run a PHP file?
Yes, I filled out everything in the scheduled task, but it still doesn't work.
Run: "C:\Program Files\Apache Software Foundation\Apache2.2\htdocs\WEB\4w_website\save.php"
Start in: "C:\Program Files\Apache Software Foundation\Apache2.2\htdocs\WEB\4w_website"
It just opens the PHP file in Notepad.
I gave the correct user name and pwd.
Please help me..
The Run command should be
C:\Path\to\php.exe -f "C:\Path\to\file.php"
From the command line help of php.exe:
-f Parse and execute <file>.
I just wrote a .bat file that does the work file.bat
#ECHO OFF
php.exe -f "C:\code\cust.php"
And put it in the Schedule Tasks like this:
Run: C:\code\file.bat
Start in: C:\code\
If you running php scripts, in most of cases script awaiting to be runned in current folder.
That mean you must set up folder each your action, use field "Start In".
Example:
Run: C:\php\php.exe
Arguments: -f C:\web\myscript.php
Do not forget:
Start in: C:\web\
Sounds like you don't have PHP files associated with the EXE.
You can do this My Computer > Tools > Folder Options > File Types. If nothing else, this can also help you verify your settings for them.
Otherwise, you can specify "C:\path\to\php.exe [file]" in the task.
This is what I did:
PHP file
<?php my code goes here ?>
*Note if you are using HTTP API/CURL in CLI use dl("php_curl.dll");
this loads curl into cli
Now I added PHP to windows path variable, this can be done from My computer, properties, advanced settings, environment variables, new
Next I created a .bat file, simply open a notepad & type code below and save as myfile.bat
#ECHO OFF
php -f d:\wamp\www\V3\task.php
This site might help you on .bat file syntax.
Now create a new scheduled task on windows & call the above .bat file as source,
For those people that can't able to make #Tomalak's answer work:
Check if there are spaces () in your directory. If it does, you need to enclose them with ".
"C:\Path\to php file\php.exe" -f "C:\Path\to php file\file.php"
And if it still did not work, check your file.php if there are included files in it.
include("file2.php"); /* OR */
include_once("file2.php");
Remove it. And if the included file in your file.php is really needed, try to move the code/script from that included file to your main file.
I think, you must execute your PHP script via URL.
you can write batch script for execute URL.
Why you don't write backend script in other language such as batch script, vbscript or etc.

Running a bash script using php

I am trying to run a bash script from an html file using php. I've read this question (how to run a .sh file from php?) and followed it exactly but cannot seem to get it to work.
I have two files in my downloads folder on my mac. These are the file names and their content:
hello.sh
#! /bin/bash
echo hello world
test.php
<?php
echo shell_exec('sh /Users/steve/downloads/hello.sh');
?>
I ran this command in command line while in the downloads folder:
php test.php
This resulted in a blank page being opened in chrome while I was expecting to get a page with "hello word" in it.
I am also wondering how to get my php code to open a window in chrome like html files do. For example, if I enter this into command line:
open test.html
a new window is opened in chrome running the content of test.html.
Thanks for the help.
Here is an example of how you can create a .php file named "test.php" that runs a shell script named "hello.txt".
This is the code that goes inside "test.php". You can put this code in any directory and it should work fine.
<?php
echo shell_exec('./hello.sh');
?>
This is the code that goes inside "hello.sh". You can put this code in any directory so long as it is in the same directory as "test.php".
#! /bin/bash
echo hello world
Once you have made both these files, go into their directory using command line and enter the following commands:
chmod +x hello.sh
php test.php
The first command (I think) makes "hello.sh" accessible from "test.php". You only need to do this once. The second command runs test.php and should return "hello world".
This can also be used with programs other than shell scripts. For example you could compile a cpp file into a.out (the default name of a compiled c++ file) and run it from the php file by replacing echo shell_exec('./hello.sh'); with echo shell_exec('./a.out');

Running a .php file from the command line?

I want run a .php file in the command line, but I can’t.
When I get to the file and want to run was opened with IDE:
>cd c:\wamp\www\GXC-CMS-2-master\GXC-CMS-2-master
core\cms\cms.php
Should certain system settings to do?
You should do it like this:
C:\path\to\php.exe file.php

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.

Categories