"Symbolic" link to resolve requests to a folder outside public_html? - php

I have a lot of image and pdf files in a folder within the public_html directory, and I have a lot of scripts on public_html dynamically processing, creating and editing these files based on user requests.
Now the problem is that I've realized that having these files stored in folders in public_html isn't safe, maybe someone could just inject some script and delete them all! So what I want to do is move them outside public_html. However - since I have so very many scripts dealing with all these files, all the paths will be messed up. I am using scripts that use relative paths, (/home/public_html/designs/product1/...) as well as scripts that use absolute paths (www.mydomain.com/designs/product1/...).
I don't know much about symbolic links to be honest - I've read up some but I found it confusing since I'm not that good with Linux either.
My question is: is there any way that I can put some kind of symbolic link or connection, so that I can move all my product design files outside public_html, and yet don't have to change all my scripts to point to the new path? So maybe, some kind of code that resolves all public_html/designs/ requests from my scripts to the new path? Maybe symbolic links isn't what I need here, but something else?

create sysmbolic links is easy in linux
first go to public_html directory :
/home/public_html/
then move designs directory to upper directory [ or every where you want ]
mv designs ../
then create sysmbolic link to the real directory
ln -s ../designs designs
every thing will be work

Related

What does the webroot refer to?

So, I've looked around quite a few questions around Stack Overflow and many of them mention the "webroot" - when looking for a specific definition of it, though, I've been unable to find one.
Does the webroot refer to the very base file (as in "App:) in my structural example below. Or does it refer to where the file you are talking about relatively? I.e. if I was talking about balance.php would it refer to view?
app
--Protected
______view
__________balance.php
----------level.php
Generally, the webroot, aka document root, is where "publicly accessible" files start in a website.
E.g. your site has a file, and url pointing at that file:
/home/sites/example.com/html/private/downloads/cutekittens.jpg
http://example.com/private/downloads/cutekittens.jpg
^---where the document root starts
Note where the two start corresponding. Everything "above" that point (/home/site/example.com/html) is OUTSIDE of the site's document root. Anything inside that .../html directory is inside the document root, and therefore accessible by browser from outside the server.
Note that this doesn't take into consideration any server-side aliases, rewriting, routing, etc...
It will be the base directory where web-accessible files go, usually called public_html, httpdocs, htdocs or wwwroot.

move_uploaded_file and relative path to destination in public_html in home directory?

I'm using OpenSuse 12.2 now. To learn and test PHP code I use public_html directory in my home folder: /home/wojtek/public_html/ I access project files in the following way:
http://localhost/~wojtek/projects/foo/bar.php
But I have not clue how to make move_uploaded_file to work. The server root is /srv/www. I set upload_tmp_dir to /srv/www/tmp (I have really no idea where it should go).
When I set destination of move_upladed_file to /srv/www/images, files landed there. But I'd like to make use of public_html folder as I'm keeping all my files there.
For example having:
public_html/projects/foo/
public_html/projects/foo/bar.php
public_html/projects/foo/images/baz.jpg
Can I somehow use the relative destination ('images/') of move_uploaded_file in the bar.php? So that I can access images relatively in IMG tag like from bar.php?
Sorry if it sounds a bit chaotic but I'm new to PHP.
No you wont be able to access the /srv/www/images folder from the /home/wojtek/public_html/projects/foo/bar.php web page.
Id suggest either
1) setting the move_uploaded_file to /home/wojtek/public_html/projects/foo/images
or
2) using a symbolic link to access the images folder from your public_html folder:
cd /home/wojtek/public_html/projects/foo
ln -s /srv/www/images/
and then it will appear that the images are stored in /home/wojtek/public_html/projects/foo/images, but they will actually still be here /srv/www/images
Its possible that you may run into trouble with permissions using this method however

PHP url functions and subdomains

I have a website thenoblesite.com. It has some subdomains i.e.
download.thenoblesite.com
wallpaper.thenoblesite.com
etc.
Pages for subdomains are present in the main htdocs folder i.e.
httpdocs/download <- download.thenoblesite.com
httpdocs/wallpaper <- wallpaper.thenoblesite.com
Problem is that I am using $_SERVER['DOCUMENT_ROOT'] . '/css'; for css folder and other common folders(graphics, includes, script etc). However in the subdomain page download.thenoblesite.com, $_SERVER['DOCUMENT_ROOT'] will refer to download.thenoblesite.com root folder, not the main thenoblesite.com root folder where css,graphics and includes folders are present.
I have to place the same graphics, css and includes folders separately on all subdomains. Every time I update the website I have to copy the common folders to all subdomains folders.
Another related problem is that i have to use absolute linking for the large sized downloads folder for e.g. VLC media player I have to use thenoblesite.com/download/vlc.exe or i also have to duplicate the large size download folder in all subdomain folders. This method unnecessarily increases the website size , creates confusion when I update the site and doesn't look good programming practise. Is there any possible PHP solution so that i can use the same css, images, downloads and includes folder for all subdomains....
I am not sure if this is something you might be interested in, but you could always create a new subdomain and call it something like style.[domain] and create a new variable in your config file and point it to that. this way you have all the images and css files etc stored in one place and if your traffic spikes you can always move that subdomain to a CDN etc so its really customizable.
UPDATE
ok so you can simply use a new variable in your config file like below :
$_config['http'] = 'http://www.yousite.com/';
now you can just use this variable to point to all your downloads etc on the main site rather than each pointing to the subdomain's folder. and if you want to be more flexible you can also add a few more css or js folders like :
$_config['http'] = 'http://www.yousite.com/';
$_config['css'] = $_config['http']."css";
$_config['js'] = $_config['http']."js";
the solution above will also help you if you decided to move the files around or just move a certain folder around etc. this is a good practice if you can adopt it.
You might be able to use an alias in htaccess (or the server config) :
Alias /images /home/username/public_html/images
If that's not possible, you could rewrite all requests to /images via htaccess:
# Untested - should get you on the right track though
RewriteEngine On
RewriteRule ^/images/(.*)$ http://yourdomain.com/images/$1 [R=301,L]

How to correctly link files?

From my previous experience, I've almost always had problems with linking files with my website projects.
For example, linking CSS styles, Javascript files and including files in PHP. The problem is, that on my PC, the directory of my project was /www/project-name/ and when I put the project on a server, the directory would be just /www/. When I uploaded the project to a server, images wouldn't show, styles wouldn't work, database connections wasn't set, functions were not defined etc...
So my question is: What is the best and most efficient way to link/include files?
Something that will work no matter what the directory of the project is, and possibly, if I include project/includes/mysql.class.php in file1.php, and I move that file to a different directory, it would still properly include project/includes/mysql.class.php
You should use relative paths.
Instead of specifying the full path ('/www/project-name/includes/whatever.php'), use a path relative to the current location:
'./includes/whatever.php'
you can define the document root directory of project and then, include all files depending on it
put
define(DOC_ROOT, realpath(direname(__FILE__));
in your front controller, and when you have to include a file
include(DOC_ROOT . "/includes/file.php");
all frameworks uses this method
I'd suggest using a relative path (eg ../style.css or ../../style.css)
The ../ references the parent directory to the current file.
This is what I do, in general.
I use root relative urls inside html (e.g. src="/images/logo.jpg"). This way I can just copy the html from one page and past it in another without having to worry about the link not working becase the other page is inside a folder.
I relative urls in css, because all the resources I use inside the css, like images, I keep in the same folder as the css file (or a sub-directory of it). I mostly do this because it is shorter (url(img/background.jpg); vs. url(/css/img/background.jpg);). Minor added bonus is you could just copy the css folder to create a new theme based on the old one, without having to change all the urls in the css.
In PHP I use include($_SERVER['DOCUMENT_ROOT'] . '/includes/mysql.php');. You can just copy past the code into another file in another folder and it will still work.
The only time I rarely need to hardcode paths is inside htaccess.

Access separate docroot in apache

I have a site on a server running Apache2 that resides at docroot /var/www/html. I want to access some of the files on a separate site at docroot /var/www/vhosts/othersite. Is there a way to access these files from the first site?
Thanks,
Chris Birk
You can include them using the include and require calls, or use symlinks to create a soft link in project 1 from project 2. These obviously depend on what you're actually trying to accomplish.
Edit: Oh, also, you could potentially add the folders you want to PATH.
The right way to do this is with mod_rewrite, and there are several ways of mapping URLs to different paths in the documentation here.
The cheatin' way of doing it would be to create a symbolic link from the directory outside the document root to a directory inside the document root, making sure the user Apache runs has can read that directory, and follow symlinks is turned on.
Yet another way of doing it would be to create a subdomain as a VirtualHost, with a document root of that other directory.

Categories