Trouble finding server root directory - php

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.

Related

Saving to "/" on Apache server w/CodeIgniter

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"];

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.

PHP, MoDX: Upload file from custom PHP Snippet

I have a snippet that I want to use to upload a file.
The script seems to be running fine until it gets to the point where PHP transfers the file from temp docs to my own folder.
My folder is called 'uploads' and is the root.
On ModX the PHP files directory is
/core/cache/includes/elements/modsnippet
I cannot seem to figure out a way to direct the script to send the file back from the modsnippet directory to the public_html/uploads directory.
So far I have tried:
move_uploaded_file($_FILES['instlogo']['tmp_name'], dirname(__FILE__) . "/../../../../uploads".$new_file_name)
;
as well as absolute paths, eg:
http://mysite.com/uploads
To no avail.
Would anyone know a way of doing this correctly? Thanks!
(ps: permissions on that folder are 777)
can you try something like:
$my_uploads = $modx->getOption('base_path').'uploads/';
move_uploaded_file($_FILES['instlogo']['tmp_name'], $my_uploads.$new_file_name)
the base_path option will give you the full file system path [from the server root]
if not & you are getting a filesystem error, the server logs [aapche] should be telling you why. Do you have access to them?

site root relative path confusion in php

I'm trying to upload images to server, create a thumbnail from the uploaded image, and save the relative paths to mysql database . . . . The image path is
/mysite/images/
from my site root (which is wamp/www/mysite/images in my local testing environment)
Now, I'm performing a directory check with
if(!is_dir($path) && !is_writable($path)){
throw new Exception ("$path is not valid .. ");
}
where path is /mysite/images/ , thinking that initial '/' will denote the root.
But, the script was throwing an exception saying invalid path name, and my
move_uploaded_file($tmp_name, $path.$filename)
too was not working.
PS: the path mysite/images/ also throws an error and the file is not moved
Now, I have worked around by appending document root manually to path name for both directory checks and move file , which is now looking like
if(!is_dir($_SERVER['DOCUMENT_ROOT'].$path) && !is_writable($_SERVER['DOCUMENT_ROOT'].$path)){
throw new Exception ("$path is not valid .. ");
}
move_uploaded_file($tmp_name, $_SERVER['DOCUMENT_ROOT'].$path.$filename)
I also changed the path to
mysite/images/
removing the initial slash intended for site root. This is working perfectly.
But, I cannot understand why directory check and move uploaded file snippets are not taking site root relative links.
Any ideas is appreciated.
The problem is with the path representation.
In linux /mysite will try to access the directory in the root level of file system. Windows this might not create an error. for is_file() kind of API's it should the absolute path and not relative ones.
Your website path is not wamp/www/mysite/images.
You are using a web server, you cant access the drive like: c:/wamp ....
Once online, how will you access your files? Linux does not have c:/ ...
Your website path is: http://localhost/mysite/
Your image path is: http://localhost/mysite/images or ./images if your code is correctly written.
so, do this:
define('ROOT', $_SERVER['DOCUMENT_ROOT']);
include_once(ROOT."/mycfgfile.php");
or
include_once(ROOT."/includes/mycfgfile.php");

Categories