Jump to content

app folder setup


Recommended Posts

Hi All

Currently i am using $_SERVER['DOCUMENT_ROOT'], Its working fine in localhost. But when i move to web its not taking actual files instead its accessing private_html folder. If my folder structure is like below

assets

includes

module1

           module1 sub folder 1

           module1 sub folder 2

           module1 sub folder 3

module2

           module2 sub folder 1

           module2 sub folder 2

           module2 sub folder 3

What would be the best way to access the assets & includes folder files inside sub folders ?

 

Link to comment
Share on other sites

It's not clear exactly what some paths are here. On your computer, does http://localhost/assets work? Does your-website.com/assets work?

And I'm not sure what "private_html" is. Do you mean "public_html"?

Link to comment
Share on other sites

3 hours ago, requinix said:

It's not clear exactly what some paths are here. On your computer, does http://localhost/assets work? Does your-website.com/assets work?

And I'm not sure what "private_html" is. Do you mean "public_html"?

1. Example On my computer <?php include $_SERVER['DOCUMENT_ROOT'].'/assets/filename.php';?> works in localhost

2. When i upload files to webserver in " public_html " it is throwing error warning file doesnot exist in websitename/private_html/assets/filename.php

 

Link to comment
Share on other sites

If you're encountering issues with $_SERVER['DOCUMENT_ROOT'] not pointing to the correct directory when moving your files to a web server, you might need to adjust your approach.

One common method to ensure that your file paths are consistent across different environments is to define a base directory constant in your application.

 

define('BASE_DIR', dirname(__FILE__));

This line of code sets the constant BASE_DIR to the directory of the current script. You can then use this constant to build your file paths reliably:

$assets_path = BASE_DIR . '/assets/';
$includes_path = BASE_DIR . '/includes/';

Alternatively, you can use relative paths to access files within your project. For example:

$assets_path = 'assets/';
$includes_path = 'includes/';

In either case, once you have the base directory or relative paths defined, you can access files inside subfolders using concatenation:

$module1_file = $includes_path . 'module1/module1_subfolder1/file.php';
$module2_file = $includes_path . 'module2/module2_subfolder1/file.php';

i hope 

This approach should help you maintain consistent file paths across different environments.

Best Regard

Danish hafeez | QA Assistant

https://www.ictinnovations.com

https://www.ictbroadcast.com

Link to comment
Share on other sites

On 5/9/2024 at 2:45 PM, Danishhafeez said:

If you're encountering issues with $_SERVER['DOCUMENT_ROOT'] not pointing to the correct directory when moving your files to a web server, you might need to adjust your approach.

One common method to ensure that your file paths are consistent across different environments is to define a base directory constant in your application.

 

define('BASE_DIR', dirname(__FILE__));

This line of code sets the constant BASE_DIR to the directory of the current script. You can then use this constant to build your file paths reliably:

$assets_path = BASE_DIR . '/assets/';
$includes_path = BASE_DIR . '/includes/';

Alternatively, you can use relative paths to access files within your project. For example:

$assets_path = 'assets/';
$includes_path = 'includes/';

In either case, once you have the base directory or relative paths defined, you can access files inside subfolders using concatenation:

$module1_file = $includes_path . 'module1/module1_subfolder1/file.php';
$module2_file = $includes_path . 'module2/module2_subfolder1/file.php';

i hope 

This approach should help you maintain consistent file paths across different environments.

Best Regard

Danish hafeez | QA Assistant

https://www.ictinnovations.com

https://www.ictbroadcast.com

Dear Danish Hafeez

Thank you for your reply, what you have suggested is working fine in normal folder structure. I might be wrong with folder structure, my folder structure looks like this

TESTFOLDER/assets/styles/style.css
TESTFOLDER/includes
TESTFOLDER/dist
TESTFOLDER/fonts
TESTFOLDER/js
TESTFOLDER/module1/models
TESTFOLDER/module1/views
TESTFOLDER/module1/contrls
TESTFOLDER/module1/public

What i am trying to do is to access TESTFOLDER/assets/styles/style.css inside

TESTFOLDER/module1/public/index.php
TESTFOLDER/module1/public/dash.php
TESTFOLDER/module1/public/01.php
TESTFOLDER/module1/public/02.php

is it possible to do it with single file named config.php  and call it on all module folders

Link to comment
Share on other sites

if $_SERVER['DOCUMENT_ROOT'] doesn't contain the correct value to the (public) document root folder and you cannot correct the server configuration so that it does, you can simply set it to the value you want (it's just a variable) in a common configuration .php file that you require (you should use require for things you code must have) at the start of your code.

Link to comment
Share on other sites

12 hours ago, mac_gyver said:

if $_SERVER['DOCUMENT_ROOT'] doesn't contain the correct value to the (public) document root folder and you cannot correct the server configuration so that it does, you can simply set it to the value you want (it's just a variable) in a common configuration .php file that you require (you should use require for things you code must have) at the start of your code.

i have created config.inc.php in root and include that file along with header / css / js include files.

In config.inc.php for local development i use $_SERVER['DOCUMENT_ROOT'] which is returning correct path and on server i use getcwd(); which is returning path to public_html

Link to comment
Share on other sites

Yes, you can achieve this by creating a single config.php file that defines the base path to your assets. Then, you can include this config.php file in all your PHP scripts to access the styles and other assets.

 

Create the config.php file:

Place this file in the TESTFOLDER directory (or any consistent location you prefer). This file will define the base path to your assets.

<?php
// Define the base URL to your assets
define('BASE_URL', '/TESTFOLDER');
?>

Include the config.php file in your scripts:

In each of your PHP scripts (index.php, dash.php, 01.php, 02.php), include the config.php file and use the BASE_URL constant to link to your assets.

For example, in TESTFOLDER/module1/public/index.php:

<?php
// Include the config file
require_once '../../config.php'; // Adjust the path as needed

// Use the BASE_URL constant to link to the CSS file
?>

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <link rel="stylesheet" href="<?php echo BASE_URL; ?>/assets/styles/style.css">
</head>
<body>
    <!-- Your content goes here -->
</body>
</html>

Adjust the path to config.php as necessary:

Since the public folder is two levels deep inside the module1 folder, you need to navigate up two levels to include the config.php file. The ../../config.php path in the example above reflects this. Make sure to adjust this path if your structure changes or if the config.php file is located elsewhere.

By using this approach, you can centralize the configuration of your asset paths, making your code cleaner and easier to maintain.

 

Best Regard

Danish Hafeez | QA Assistant

ICTInnovations

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.