PHP file handling and third level domains - php

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

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.

how to access the file with modified url in php?

I want to access a file with my modified url without going via .htaccess
Let assume my file is in
root/newfolder1/xyz.php
So the url below is working fine
www.abc.com/newfolder1/xyz.php
I want to access the file in newfolder1 but with modified url like below:
www.abc.com/xyz.php // This I want but it is not working.
Any ideas or suggestions would be welcome.
You can either use symlink
Or you can make a php in your main folder
xyz.php
And include the php inside the newfolder1/xyz.php
Using this method make sure to change working dir to newfolder1 before including.

Get path to project directory 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

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.

Absolute URL Path for PHP Website

I am working on a website in a CMS site. I have provided a link in my footer file e.g blogs.php. This page is at the root like "http://www.example.com/new_cms/blogs.php"
When I moves to other files link like "http://www.example.com/new_cms/forums" the footer link is changed to "http://www.example.com/new_cms/forums/blogs.php" but the blogs.php page resides at path "http://www.example.com/new_cms/" . I tried different $_SERVER[] variables but got no luck to get the above path "http://www.example.com/new_cms/" means the server name with the directory where the project is currently running from.
Any one have idea how to get it done will be a great help.
Thanks
This is probably because your link has a relative url in it, like so :
Blogs
So it looks for the blogs.php file in the current directory.
If you want that link to point to the blogs.php file that is in the new_cms folder, you have to use a link like this :
Blogs
You can use absolute URL or try to rewrite your url with a condition.
You can obtain the server name from $_SERVER['SERVER_NAME'] that is the canonical server name specified in the server/virtual host configuration. Besides that $_SERVER['HTTP_HOST'] gives you the host specified in the HTTP request header field Host. In general, these two values are identical. But since both can be manipulated by user input (see Chris Shiflett’s SERVER_NAME Versus HTTP_HOST) you need to be careful when using these.
As for the path, you will probably need to specify the base URL path on our own. Because if you think of URL rewriting techniques, the physical path (file system path) does not need to be the same as the logical path (URL path). So you can not derive the logical base path from the requested URL path, the script file path and the document root.

Categories