Saving to "/" on Apache server w/CodeIgniter - php

This seems like a basic question, but it's stumping me. I have CodeIgniter installed, and I've got a model that manipulates and saves XMLs. My problem is when trying to save it to "/" I get a "Permission denied" PHP error. I need to save them to a separate directory relatively, but I'm not sure where exactly "/" is located on the server. Is it in the "/www" of Apache, or root of the whole server? Once I know this I should be able to navigate to the correct directory

/ is the root directory. The starting point of your directory structure. This is where the Linux system begins. Every other file and directory on your system is under the root directory. Usually the root directory contains only sub-directories, so it's a bad idea to store single files directly under root.
Try specifying the complete path in your application.
Example: /home/user/public_html/yourApplicationFolder/
Or specify a relative path:
Example: ../somePath/.
This article could be helpful.

To get the filesystem path to the document root just use $_SERVER["DOCUMENT_ROOT"];

Related

Reverting to a different file when PHP fails to find a specific file (include function)

Sorry for you advanced guys, I'm actually teaching myself some PHP so this may seem like a beginner's question.
I'm using a testing server and then uploading to a remote server. The index.php file is located in "C:\XAMPP\htdocs\php_site" on my local pc and in "home/www/myname.atwebpages.com/" on the remote server. Now the code I'm trying to run is just a simple:
define ('ROOT', $_SERVER['DOCUMENT_ROOT']);
include ROOT."menu/menu.php";
This code works fine for the remote server. However, when attempted on my local machine, it spits out this error:
Warning: include(C:/XAMPP/htdocs/menu/menu.php): failed to open stream: No such file or directory in C:\XAMPP\htdocs\php_site\index.php on line 21
Clearly, it's not looking in the php_site folder. Instead, it's tying to find a menu folder in the htdocs directory, but it's not there. The menu folder is located inside the site folder, php_site. If I chance around the code to work on the local machine, it no longer works on the remote server. I'm a little confused as to how to get around this problem.
I think $_SERVER['DOCUMENT_ROOT'] is defined by apache, so you'd need to change the config there. Or, define the ROOT constant relative to where you actually put your files, so if you do something like:
define ('ROOT', dirname(__FILE__));
Put that in a constants file in the same folder as your index.php.
Your document root on the remote and local machines is different. On your local machine your document root is the htdocs directory, and the php_site folder is merely a sub-folder, and thus the path is wrong.
I suggest either making the ROOT directory be a relative directory to the index page, or have a constants file in the root directory of the PHP site that defines the root directory as the directory it is in (which would be in the php_site directory on your local machine, the same directory as your index page). define ('ROOT', dirname(__FILE)); would work in this situation.
Another idea is to use a try-catch to catch the failure of the include statement, and attempt to try another directory, perhaps using define ('ROOT', $_SERVER['DOCUMENT_ROOT']); first, and if it fails, attempt to use define ('ROOT', $_SERVER['DOCUMENT_ROOT'] . 'php_site/'); instead.

Find filepath to public_html directory or it's equivalent using PHP

I'm creating a .php file that will be uploaded to the root directory of a server. I need that .php file to then figure out the path to the public_html folder or it's equivalent.
I need to do this because I want my .php file to be able to be uploaded to the root and used on any hosting account. Because many hosting companies use different file paths to the public_html folder or even call it something different, I'm trying to figure out how to detect it.
Preferable there is a server variable or easy test to do this. If not, the public_html folder will always contain a particular file so maybe I could search for this particular file and get the path that way. I'm just worried about a filename search being heavy on memory.
The .php file that is being executed is located inside the ROOT directory and needs to locate the public_html folder.
Like this: /home/user/file.php
needs to detect
/home/user/public_html/ or /home/user/var/www/ or /home/user/website.com/html/ etc.
The challenge with this is that a server can have very many public_html's so outside of the context of a request there is no real way to find out what that is.
One thing that you might be able to do to get this information from a php script (if you know the url to get to the host) is to create a php file called docroot.php that looks like this.
<?php
if ($_SERVER["REMOTE_ADDR"] == '127.0.0.1'){
echo $_SERVER["DOCUMENT_ROOT"];
}
Then within your file.php your would do something like
$docRoot = trim(file_get_contents("http://www.mydomain.com/docroot.php"));
This makes the assumption that the server can resolve to itself via the local interface by name.
I found this website which provided me with the only good solution I have found after scouring the web...
$root = preg_replace("!${_SERVER['SCRIPT_NAME']}$!", "", $_SERVER['SCRIPT_FILENAME']);
The way this works is by getting the full path of the file and then removing the relative path of the file from the full path.

Is there a difference between /../ and ../ in php?

I was curious if ../ and /../ is the same in PHP so I tried these:
require_once('/../frame/header.php');
require_once('../frame/header.php');
Both of them worked. Was I just lucky or they realy do the same exact thing? Why?
In your context if they both worked, that implies that your scripts are at the root of the filesystem you have access to, or one level in. They are not the same however! /../ refers to the filesystem root (and .. one directory up, which just gets eaten and is still the root), while ../ refers to one directory higher than the current one. Any path beginning with / is an absolute path from the filesystem root.
From anywhere other than the filesystem root, these would not function equivalently.
Suppose your working directory is /var/www/scripts.
require_once('../include.php');
Will include a file at /var/www/include.php.
But from that same location, if you did
require_once('/../include.php');
...php will attempt to load the file /include.php at the filesystem root, and it probably won't exist.
Now, a lot of web hosts will supply you with a filesystem whose root / is also the web server's document root, the web server document root is only one directory level in from the root like /www. In that case, /../ may work fine, but beware if you ever attempt to move it to another server with a different filesystem configuration.
So if the script's working directory was /www, just by luck, these two would function the same way:
require_once('../include.php');
require_once('/../include.php');
Both would include the file /include.php at the filesystem root.
Note, this is not PHP-specific, but a property of filesystems which use the .. in general...
Preceding any link with a / mean that you are coming from the root of the context you are in. If you are already in the root directory, or a chroot jail, then both statements are the same.

how can I get a URL from an absolute folder path?

I have a php function that is moving files for me. It requires absolute paths to place those files (/Applications/MAMP/HTdocs/mysite/myfolder)
how can a turn this folder path string into a url (http://mysite.com/myfolder) so that I can create links to the files on the fly?
I do not know necessarily the names of the folders, as the software could be run in many locations.
Many thanks.
Obviously, you need to know server root for such a calculation.
Luclikly, $_SERVER['DOCUMENT_ROOT'] contains this path.
So, just subtract server root path from the given path:
$path = '/Applications/MAMP/HTdocs/mysite/myfolder';
$approot = substr($path,strlen($_SERVER['DOCUMENT_ROOT']));
check if you have a drive letter in the DOCUMENT_ROOT, and correct the code if necessary
Note that adding http://mysite.com is unnecessary and useless. just /myfolder/ is what you really need.
You can check for this value: $_SERVER["DOCUMENT_ROOT"];
That is the root of your website. If you have the folder and replace the $_SERVER["DOCUMENT_ROOT"] with the $_SERVER["HTTP_HOST"] you will get the URL to the folder/file
If mysite folder is within HTdocs then you can access it using http://yourdomain/mysite, (if HTodcs is your home directory)

Trouble finding server root directory

i am working on a php project. I have uploaded the files in the server. When i login to the ftp account, it takes me to the main directory of files. I cannot implement the file download script. because i cannot obtain the root folder. when i use echo "$_SERVER["DOCUMENT_ROOT"]", it shows - "(/var/chroot/home/content/30/6323230/html/)". but this does not work for me. if i use file location like - "$_SERVER["DOCUMENT_ROOT"]/songs/", it simply gives error.
Its kinda unknown to me. because previously in another server outputs like - "/home/tskbdcom/public_html". when i log in to this ftp i get the root directory. then i go to "public_html" or "www" directory.
How can I solve this problem?
Try using
$_SERVER["DOCUMENT_ROOT"] . "songs/"
no leading slash before directory name because $_SERVER["DOCUMENT_ROOT"] has one slash at the and of path.
This is not answer to your question this is just reason why you got error using
"$_SERVER["DOCUMENT_ROOT"]/songs/"
Also you have double-quotes inside of double-quotes.

Categories