Setting up processing scripts in sub folders in php - php

I am trying to clean up my website folder structure. I have too many processing scripts in the root directory. The reason is because they won't work if I move them to a sub folder and call them from there.
For eg.
root directory
- images folder
- css folder
- js folder
- includes folder
- admin folder
index.php
ajax.php
Say I move the ajax.php to includes folder, it will look like this.
root directory
- images folder
- css folder
- js folder
- includes folder
- ajax.php
- core folder
- template folder
index.php
Now on every page I have an init.php required. In ajax.php for eg.
<?php require_once 'core/init.php'; ?>
// rest of the code
The issue I am having is that the init.php file won't run as long as ajax.php is in includes folder. I get an error something like this.
Warning: require_once(core/init.php): failed to open stream:
If i move the ajax.php back to the root directory, it'll work fine.
On index.php, this is how I am calling the ajax.php
core/init.php works fine when requiring in the "templates" folder.
Perhaps someone can tell me the solution to this?

Try using PHP's chdir function. This will change the working directory that your PHP environment is working in, and therefore the relative paths that your include functions are looking in. Ideally, you'd want to call chdir with the root of your project, so that when you do require_once('core/init.php'); you don't end up calling from the core directory, but rather your . directory.

Related

How can I make the php files inside an includes/ folder readable by the website but not downloadable by file download managers?

I have a small PHP/MySQL project I would like to upload to our subdomain. The project has an includes/ folder that contains some PHP files that have information about the database name, username, password and login function.
How can I make the files of this directory readable by the website (so when someone comes to the website, they can log in and do other stuff) but not accessible to the public? I can use a file downloader to download the content of the folder which is something I want to block.
Is the solution using a .htaccess file?
EDIT:
Thank you all for the answer. After some reading, I switched my folder structure to be like this:
includes/
- initiate.php
- login.inc.php
- functions.inc.php
public/
- index.php
- login.php
templates/
- header.php
- footer.php
I'm now having issues setting up relative and absolute path constants though
The initiate.php has my constant variables:
define('INITIATE_FOLDER', dirname(__FILE__));
define('ROOT_FOLDER', dirname(INITIATE_FOLDER));
define('TEMPLATES', ROOT_FOLDER . '/templates');
define('INCLUDES', ROOT_FOLDER . '/includes');
define('WWW_ROOT', ROOT_FOLDER . '/public');
When I echo out the constants, I get the followings:
echo INITIATE_FOLDER; C:\wamp64\www\project\includes
echo ROOT_FOLDER; C:\wamp64\www\project
echo INCLUDES; C:\wamp64\www\project/includes
echo TEMPLATES; C:\wamp64\www\project/templates
echo WWW_ROOT; C:\wamp64\www\project/public
Can you please tell me what I'm doing wrong and how to correct it?
If your server setup is correct, no PHP file will get downlaoded, only executed.
Basically, you have PHP extension installed nad if the file starts with <?php then it will be executable.
As others have said, all content between <?php ?> tags will be removed from the page before it's served by your server, so long as your file ends in .php.
If you are trying to keep a non-php file from being served, your best bet is to put your includes folder where it is not publicly available.
Generally, when you FTP into your server, the layout is something like this:
www/
public_html/
... etc, other folders
The files you want to make publicly available should go inside of the public_html/www folder (www is usually just a shortcut/symlink for public_html).
You includes directory should go next to the public_html folder, rather than within it.
www/
public_html/
includes/
... etc, other folders
Then, in the files where you were including those files, include them from the new location.
<?php
require_once "includes/databaseSettings.php";
becomes
<?php
require_once "../includes/databaseSettings.php";
Now your files are outside of the directory being served by your HTTP server, but still available to be included in the rest of your code.
This has usually been my experience, but can vary from vendor to vendor. If, when you FTP into your server, you don't see a www or public_html folder, try navigating up one directory.

How can I build a dynamic absolute path in PHP?

I've a web site made of some folders, one for each section (info, news, blog etc...).
In each of these folders there is an index.php file that should load a layout (common to all). These are stored in a different folder in the root where there is also the main index.php file (the homepage). So i have something like this:
root
-index.php (home)
-/layout
--layout files
-/info
--index.php`
The index file in the /info folder should include the page layout from /layout.
The problem is that the layout files should include other files from other folders.
In layout files I put this:
include 'contents/page-element.php';
But if I try to reach the same page-element.php file from the index.php file in the /info folder, I should do:
include '../contents/page-element.php';
to go to the root and then reach the /layout folder.
I don't want to create a copy of the layout for the folders so I've tried $_SERVER['DOCUMENT_ROOT'], but it does not work in some cases on the localhost and even on the web server.
Can someone help me or let me know how I can build a dynamic absolute path?
By using the built-in constant __DIR__ you can get your absolute path
by print it or save
echo __DIR__;
it will print something like this for you
C:\Users\user\Desktop\test
and there is also another constant that will get the absolute path for your file that executes the command __FILE__
echo __FILE__;
will give you a result like this
C:\Users\user\Desktop\test\index.php

Change working directory

Sorry for asking again, but I really need help. I have header.php in the root/lib which is including header_sub.php in the same directory. Normally files in root can directly include them by this code:
include_once('lib/header.php');
but now i have example.php in a sub-directory /blog, if i use these
include_once(../'lib/header.php'); or
include_once($_SERVER['DOCUMENT_ROOT'].'/lib/header.php'); or
include_once(dirname(__FILE__).'/lib/header.php');
header_sub.php would not be included correctly.
Is there a way to include header.php and header_sub.php without modifying them?
Some body suggested to use these:
$oldcwd = getcwd(); // Save the old working directory
chdir("../"); // Moves up a folder, so from /blog to /
include("header.php"); // Include the file with the working directory as if the header file were being loaded directly, from it's folder
chdir($oldcwd); // Set the working directory back to before
However, even i can see the current url is root directory after chdir(), it still includes this root/blog/lib......
The path to the file you need depends on were you are calling the file.
Examples:
In /root calling file in /root -> include_once('header.php');
In /root calling file in /root/lib -> include_once('lib/header.php');
In /root/lib calling file in /root -> include_once('../header.php');
In /root/blog calling file in /root/lib -> include_once(../lib/'header.php');
In /root/blog/css calling file in /root/lib -> include_once(../../lib/'header.php');
If you do like this all path are relative and you can change the root folder and everything still works.
Another option is you have a file called "common.php" or "include.php" were you define path for some folders. This is useful if your site directory has many sub folders.

require_once files from another directory requiring within

This is a big confusing but I'll try to be as clear as possible.
I am using the Starter Site PHP Template in Web Matrix for an assessmnent, now when I move several files such as the files containing the database details, header, footer ect.. into the admin file I am having an issue with my index.php and page.php.
These files use require_once() to include several files within the admin folder. However in the admin folder the files being required have require_once tags again within that directory.
When I use require_once('admin/database.php'); the database file has require_once(somefile.php) within it. There are numerous files like this being included.
How can I use include these files using require_once in the root directory without getting errors like these.
warrning: require_once(/Includes/simplecms-config.php): failed to open stream: No such file or directory in C:\xampp\htdocs\starter-ste\admin\Includes\connectDB.php on line 2
The includes folder is located within the /admin/ folder.
php require statements will look for files in the current directory and directories in the php_include_path. You can solve your problem by adding your include directories to include path.
You can do this dynamically by calling set_include_path
$directories = array('/library', 'var/', get_include_path());
set_include_path(implode(PATH_SEPARATOR, $directories));
[EDIT]
Your server might not know where the root directory of the website is. So, first, get the document's root directory and append it to the file name like this:
$root = realpath($_SERVER['DOCUMENT_ROOT']);
require_once "$root/your_file_path_here";
Looks like you haven't enclosed the file name within quotes. So that
line should probably be like this:
require_once("/Includes/simplecms-config.php");

PHP cannot find my php files if not under htdocs folder

By someone's advice I've put all my PHP files in a separate folder (inc) on the same level as htdocs. Only index.php is left in htdocs. So, it's like this:
C:\myproject\htdocs
- index.php
C:\myproject\inc
- login.php
- util.php
- register.php
...
Now, when I go to localhost in my browser index.php is processed and shown correctly. But any links to other php files are not found. I tried to prepend links with "inc", but they're still not found. What should I do?
My php.ini file has this line (it's on Windows):
include_path = ".;C:\myproject\inc"
The point of an include directory is to put files you don't want to be accessible by a Webserver. If login.php needs to be accessible via a URL like:
http://yourdomain.com/login.php
then don't put login.php in the include directory. Putting util.php in an include directory makes sense because you never want this:
http://yourdomain.com/util.php
You can't put web-accessible files outside the htdocs folder, you should be using the 'inc' folder for files like 'database_functions.inc' which should not be opened directly in your browser:
http://localhost/index.php // directly accessible - goes in htdocs
http://localhost/login.php // directly accessible - goes in htdocs
http://localhost/register.php // directly accessible - goes in htdocs
http://localhost/util.php // you don't want people loading this file directly - put in 'inc'
http://localhost/database_functions.php // you don't want people loading this file directly - put in 'inc'
I believe you need to escape the backslashes in your php.ini -- so it should be C:\\myproject\\inc. But as others have pointed out, you won't be able to use a browser to access the PHP files in your include directory, because the web server will not allow access to a directory outside the htdocs tree.

Categories