1

I am trying to run this script from the command line:

#!/usr/local/bin/php
<?php
$myfile = fopen("cc.log", "a") or die("Unable to open file!");
$txt = "Success \n";
fwrite($myfile, $txt);
fclose($myfile); 
?>

And it works, but only when I cd into the directory of the script and run:

/usr/local/bin/php test_script.php

This also works:

/usr/local/bin/php /home/<username>/public_html/test_script.php

but not when I leave the directory.

9
  • Are you on Linux? Commented Jul 4, 2017 at 4:54
  • @cosinepenguin I'm accessing a Linux server via PuTTy on Windows Commented Jul 4, 2017 at 4:56
  • Have you made the test_script.php file executable? (chmod 755 test_script.php). Commented Jul 4, 2017 at 4:57
  • Also, do you get any errors when you try running from outside the directory, or does it not return anything? Commented Jul 4, 2017 at 5:00
  • @cosinepenguin I just entered that command with no luck. And no errors, it returns nothing Commented Jul 4, 2017 at 5:04

2 Answers 2

1

Solved by changing line 3 of test_script.php from:

$myfile = fopen("cc.log", "a") or die("Unable to open file!");

to an absolute path:

$myfile = fopen("/home/<username>/public_html/cc.log", "a") or die("Unable to open file!");

The original script created a cc.log file in the directory from where the command was called and appended about 50 "Success" messages xD

Sign up to request clarification or add additional context in comments.

Comments

0

A more formal answer to the original question:

Your file (test_script.php) needs to be made executable to be able to be run from anywhere. This can be done with chmod:

chmod 755 test_script.php

and results in the file being able to be run by invoking.

./test_script.php

And now, drumroll please, you should be able to run the file by invoking it like this:

./path/to/test_script.php

That dot at the front of the path should allow you to run the script as an executable without doing anything else.

Hope it helps/works!

(If it doesn't work comment and I'll boot up my Linux machine and try to replicate it!)

EDIT

Ok, that's weird that it doesn't work! Shoot! Is the PHP CLI (command line interface) installed on your Linux server? If so I would encourage you to try:

php /path/to/your/test_script.php

Hope that second suggestion helps!

EDIT 2

Aha moment number 2!!

I believe your php should be installed here: /usr/bin/php, having it in the local folder will not allow you to properly invoke it from another directory!

To download yourself a new installation of php, run

sudo apt-get install php5 libapache2-mod-php5

You will also have to change your script header to #!/usr/bin/php.

This was a good source for this third edit! I really hope it helps!

6 Comments

Thank you but still no luck. ./test_script.php works but when I navigate up one level in the directory, ./path/test_script.php does not. But it should! I see it used in tutorials etc. but no luck for me! Even absolute path only works from within the folder (in this case, the public_html folder)
My second suggestion is to use the php CLI! That might help, I edited my answer... Here's where I got the idea for using the CLI: stackoverflow.com/questions/34396443/…
In response to your edit: still nothing! And it seems to be installed. I type which php-cli and get /usr/bin/php-cli And php-cli -v returns: PHP 5.4.45 (cli) ...
Aha! And where is your php itself? I think it should also be in /usr/bin/ (/usr/bin/php not /usr/bin/local/php)!
Solved! See my answer
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.