Get path to project directory PHP - php

I need get full path to my web-project directory, as I am using single entry point so I doing this:
$_SERVER['SCRIPT_FILENAME']
then remove index.php and then I get something like this: /var/www/myproject, and then I can use it for require some stuff files like configuration etc. But when I call this method (e.g. for include some stylesheets) in my php files for rendering I getting 404 error for this stylesheet and what I getting in browser console:
http://localhost/var/www/myproject/bower_components/bootstrap/dist/css/bootstrap.min.css Failed to load resource: the server responded with a status of 404 (Not Found)
Obviosly that path should looks like:
http://localhost/myproject/bower_components/bootstrap/dist/css/bootstrap.min.css
So the question is how I can properly get path to my project for proper include files in PHP and load assets in HTML ? And the approach what I am using now it ok? Or I need to do somehow better? Thanks!

The problem is there are TWO type of adresses:
1) LOCAL, which are accessible throw your scripts from INSIDE your server.
You use them if need to include file inside your script and such, but not from outside by browser.
2) WEB addresses, which are accessible by browser from OUTSIDE.
You use them if need to give some file to browser, like style-sheet or JavaScript script.
Try $_SERVER['SERVER_NAME'] . $_SERVER["DOCUMENT_ROOT"] for this task.
Detailed description for server environment variables

Related

Host, PHP, htaccess

I need some help.
I was reading the security recommendations of my hosting service and they say that ideally just put the
index file and files like css, js and img inside my root folder, and that all other files should be placed
off, that is, a level above.
I tried doing this in my tests, and I had some problems. The structure of the hosting folders is:
/
/htdocs
Inside /htdocs I put the index.php file and when accessing it through the url exemple.com/index.php works normally.
But putting other test files out of htdocs is what starts the problem. For example, if I have a file called contact.php
and I try to access it through the url exemple.com/contact.php I get the 404 error message.
So the question I have to ask is:
Is it possible to access url files that are outside of htdocs, or better to put all the files that will be accessed by the url inside
of htdocs and leave only configuration files outside this folder, like class, functions, database connection, etc?
And if it is possible to access the files by url, how would I rewrite these urls in htaccess?
and that all other files should be placed off
Yes, this is good practice. However, you're misunderstanding the implementation.
You can not directly access files outside the document root. But you can indirectly access them. I.e., the web server can't see them, but your programming code can.
Ideally, your site would use the front controller pattern. Here, your index.php file would serve every page of your app by intercepting every request and then routing it to the correct end point. I.e., you would never directly request /contact.php, you'd instead request /contact, which would get funneled to /index.php, which would load the required resources from outside the doc root.

PHP file handling and third level domains

I've a subdomain like software.domain.com that is a redirect to domain.com/software/ through cPanel redirect.
I'm creating an upload system that send all the files in a sibling folder, like domain.com/files/, no problem uploading with some tricks but when it's time to delete that file, that specific script cannot find it...
Returning the $_POST variable send to the script, the path is right, as /files/path/to/file, it refers to the public_html root as it has to be!
The script is invoked using an ajax call, it could be it? Maybe some path translation...
Thanks a lot!
If you are saying your AJAX can't find it from the subdomain, or anywhere else, you need to use the full domain path, WITHOUT the subdomain.
var url = "http://domain.com/files/path/to/file";
If you're saying your PHP can't find it, you can do something like this to ge the absolute path. From a file in the root of your SUBDOMAIN...
$url = realpath(dirname(dirname(__FILE__)))."/files/path/to/file.php";

How come my code works separately but not when I merge it with my controller?

I wrote this code in a test.php file.
<?php
include ($_SERVER["DOCUMENT_ROOT"].'/MySite/protected/MyYouTube/google/src/Google/autoload.php');
require_once ($_SERVER["DOCUMENT_ROOT"].'/MySite/protected/MyYouTube/google/src/Google/Client.php');
require_once ($_SERVER["DOCUMENT_ROOT"].'/MySite/protected/MyYouTube/google/src/Google/Service/YouTube.php');
?>
If I go to this file like this: localhost/MySite/protected/MyYouTube/test.php it works, but if I copy the same code into my controller that is located under the same folder as test.php, I get this:
include(Google_Service.php): failed to open stream: No such file or
directory
There are no conflicts with the imports. In fact, the controller and view can be empty and still I get the same thing.
Apparently that happens when autoload.php is not actually loaded.
How is it possible that when the code is integrated into the website it throws this error?
This is what the path to my site looks like:
localhost/MySite/index.php/user/view It seems that the way I visit the file matters.
I tried several things. I tried importing the test.php into my view or my controller, and still I get the same error. I tried using Yii::app()->basePath and it gives the same problem.
The paths are correct, I have checked several times. How can I fix this?
The include path is actual server path, not site path, if you use / at the beginning, you are telling it to look in the server root.
If you know the absolute path in the server you can use it like /var/www/MySite or c:\mysiteif you don't know that then you use relative paths.
$_SERVER["DOCUMENT_ROOT"] works well with PHP 5.3, not sure if below check your version. Try removing the '/' before the MySite part, as the variable already does it for you so it might be printing like localhost//MySite, although I'm not sure if that should or shouldn't work. ]
Also the autoload php should be loaded with a require_once function, not with the include. Good luck!

Creating absolute path in PHP

I've created a PHP file url.php and wrote this codes -
<?php
define("BASE_URL", "/geo15/" );
define("ROOT_PATH", $_SERVER['DOCUMENT_ROOT'] . "geo15/" );
?>
I've included the url.php file in several pages but when I browse those files, I get this error message:
Warning: require_once(inc/url.php): failed to open stream: No such
file or directory in C:\wamp\www\geo15\inc\header.php on line 1
But when I paste the path C:\wamp\www\geo15\inc\header.php in my browser's URL bar, it opens.
N.B. I'm using WAMP server for the development.
The DOCUMENT_ROOT would output something like "c:/wamp/www" so you'll be missing one / there. Your current code would actually output something like "c:/wamp/wwwgeo15/".
Also, make sure that 'geo15/' is in the actual www directory rather than an alias. If it is an alias then you'll definitely get a broken link. In addition, you might find the following useful:
dirname($_SERVER['SCRIPT_FILENAME'])
Would output something like 'x:/path/to/the/current/executing/php/file'.
$_SERVER['SCRIPT_FILENAME'] would output something like 'x:/path/to/the/current/executing/php/file/theCurrentRunningFile.php'.
This is useful for locating pages that you need to include, includeonce, or require.
dirname($_SERVER['PHP_SELF'])
Would output something like '/myAliasDirectory/and/path/to/current/php/file'.
$_SERVER['PHP_SELF'] would output something like '/myAliasDirectory/and/path/to/current/php/file/theCurrentRunningFile.php'.
This is useful for creating dynamic hyperlinks.
Alternatively, if you have a lot of sub-directories, you might want to consider using "../" to point out a directory above the current directory. So, if your script is "/my/path/to/current/runningscript.php" and the file you want to include is in "/include/this/file.php", then the right way to include it is "include('../../../../../include/this/file.php');".
And.. beginning with a "/" will also bring you to the root directory or actually, it can either point to the root directory of the current alias OR the root directory of the hard drive, while beginning with no slash will start on the current directory of the running script. If you're working in an alias "/myAliasDirectory/" starting your include with / like in "include('/file.php')" would work if "file.php" is in either "x:/file.php" or "/myAliasDirectory/file.php". So yeah, it gets pretty confusing if you do that so I'll only half recommend it.
I use this code (in Unix):
include dirname(__FILE__).'/paht/relative/to/the/current/file/file_name.php';
It works in all situations.
in Windows, you may use:
include dirname(__FILE__).'\paht\relative\to\the\current\file\file_name.php';

Magento - include/require php file within phtml template file

In one of my magento template phtml files I am trying to include a seperate php file.
When I include it I get nothing outputted and when i use require instead I get the following error
Fatal error: require(): Failed opening required 'http://www.site.co.uk/dir/test.php'
(include_path='/home/usr/public_html/app/code/local:/home/usr/public_html/app/code/community:/home/usr/public_html/app/code/core:/home/usr/public_html/lib:.:/usr/lib/php:/usr/local/lib/php') in /home/usr/public_html/app/design/frontend/theme/edits/template/review/product/view/list.phtml on line 30
The first line of the error shows the correct url path and when i go to it directly it works - it just doesn't like being included/required from the phtml template page.
I've tried the following in the phtml file (using magento's BaseURL, absolute path and the relative path):
<?php
$root = Mage::getBaseUrl(Mage_Core_Model_Store::URL_TYPE_WEB);
require/include ($root.'dir/test.php');
?>
<?php
require/include ('http://www.site.co.uk/dir/test.php');
?>
<?php
require/include ('../../../../../../../../../dir/test.php');
?>
Instead of Mage::getBaseUrl use $root = Mage::getBaseDir();
require('http://....') is going to (if it's enabled) do a full-blown HTTP request to the specified URL. Since it's a URL, that webserver is going to EXECUTE the php script and send you its output.
If you're trying to actually load the CODE of that test.php, e.g. the raw un-executed PHP, then you cannot use an http request. The remote server has NO idea that the http request is actually from PHP and is coming from in include(). It'll just blindly run that script and send over the output.
You'd need to (say) rename the remote file to test.txt, so that the webserver sends it out as-is, and then that text will be executed as PHP code on YOUR server.
And as far as the other paths go, if your $root is something like:
/home/sites/example.com/html
then the require is going to be looking for
/home/sites/example.com/html/dir/test.php
Is there a dir subdir inside whatever your site's $root is?

Categories