2

I have a php file test.php

#!/bin/php

<?php
  echo "test";
?>

This file owned by root and its permissions are these

-rwxr-x--x 

so 'others' can execute, but cannot read.

When logged in as other user than root and trying to run this file from the command line

php test.php 

Got the following error

Could not open input file: test.php

If given +r permission it works.

I thought that only execute permission was needed.

Does a PHP file need read permission to 'others' to be executed in command line?

Note: using Ubuntu 10.04 LTS and PHP 5.3.2

  • 1
    You execute the file called `php`, not the `test.php`. The `test.php` is read and interpreted by file (program) named `php` (which is actually a symlink). – takeshin Sep 02 '10 at 17:55

4 Answers4

9

Having only execute permission is fine if it is a binary. Scripts must have read permission so that their interpreter can read them.

Ignacio Vazquez-Abrams
  • 45,019
  • 5
  • 78
  • 84
2

if you execute the script directly either using the full path or ./test.php with a shebang line it will need to be executable. If you specify the interpreter (ie. php) it's the interpreter that needs to be able to execute code, so in this case it only needs to be able to read the code.

James L
  • 5,915
  • 1
  • 19
  • 24
1

Yes, the file needs to be readable for the user you try to run it with. On a side note, the executable bit doesn't need to be set if you call it the way you do (php /path/to/script.php).

joschi
  • 20,747
  • 3
  • 46
  • 50
0

Yes, scripts (non-binary files) need r+x permission to execute.

gWaldo
  • 11,887
  • 8
  • 41
  • 68
  • 1
    Only if you are invoking it from shell with, e.g.: `./foo.py`. But not if the interpreter is invoked, e.g.: `python foo.py` – RufusVS Mar 08 '19 at 15:56