0

I seem to have the problem that in a php file I wrote, I can not seem to link an external file:

-bash-4.1$ pwd
/home/mywebsite.com/public_html/incl
-bash-4.1$ ls
toinclude.php hereisthecode.php

hereisthecode.php

<?php
$file = "toinclude.php";   // file permission is set to 755
    // alternatives which were tried:
    // $file = "./toinclude.php";
    // $file = "http://mywebsite.com/incl/toinclude.php";
file_exists($file); // returns false
?>
2

You've probably gotten used to using #include statements in C. The equivalent function in PHP is require(). If you have access to pset7 or pset8, just look at the distribution code provided. You'll definitely see require() (or its close relative require_once() near the top of files like index.php.

Also, note that the correct permissions for PHP files are chmod 640, not chmod 755. This won't prevent your code from running, but your permissions are unnecessarily lax and could introduce security vulnerabilities.

If you use require() correctly, as seen in the examples I mentioned, you'll specify a relative or absolute path in the local file system, and you won't need to access a PHP file via an HTTP URL. In fact, if you have set your permissions correctly, nobody should have world-level permissions to read, write, or execute any PHP file from the world outside via the web.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .