How to access files in public_html through the subdomain folder - php

I have a website called www.meagl.com ...In this, I created a sub-domain called m.meagl.com.
My public_html folder contains two folders called users and groups.
What I wish to do is access these folders through my sub-domain folders in php to get the images that I have stored in them.
To do this, I add "../" to the existing file destinations in the sub-domain files wherever necessary. Example:to access users/sharvai/78.jpg, I wrote ../users/sharvai/78.jpg. But no images are being loaded onto my sub-domain.
Please help!!

UPDATED ANSWER:
I was initially thinking about this as if you were dealing with a .php file dependency. That was the wrong way to think about it. You are talking about images. Images, whether inline img tags or CSS backgrounds are served over http://. Therefore, you need to use a valid URL. Based on the information you have supplied, you want to use URLs like:
http://www.meagle.com/users/75.jpg
Shame on me for not realising this sooner.

Files outside of public_html are protected from being seen on the web. Many people use that feature as a security to their content. If you have a file on there that you want to show contents of though, you have to use the include('file.php'); or include_once('file.php'); or even require_once('file.php') in a public ally visible file. Aka a file you have in public_html has to be the one to call the higher up file. If I am understanding your question right, that is how it is supposed to be done.

Related

Issues with directories and paths in a PHP project

I am having trouble figuring out this path problem. As you can see the highlighted tag is located in a file called admin_navigation.php and the href is going one directory up from includes folder which is admin and to the file posts.php. The absolute path seems correct to me and the to Phpstorm but the PHP server jumps to this location "http://localhost/cms/posts.php?source=add_post" but instead, it is supposed to go to "http://localhost/cms/admin/posts.php?source=add_post". I'd appreciate it if someone can explain this weird behavior to me.
When you link files with include(), you're not 'referencing' them per se, but copying the contents of the target file to the file with include().
Let's say you're including admin_navigation.php on /admin/index.php with include('includes/admin_navigation.php') or similar. This essentially copies the contents of admin_navigation.php into index.php. Now index.php contains the exact same link - add posts.
As such, index.php looks one level up from /admin, and attempts to find posts.php in CMS -- which it can't, as the file doesn't exist there.
Note that this process applies to every file that you include admin_navigation.php in, so if you have multiple references from multiple different folders, then you'll have to have an absolute path in order for all files to reference the target correctly.
This would be done with add posts.
Alternatively, if you're only including admin_navigation.php on files in the admin folder ( categories, index and posts), you can simply omit the parent folder navigation in the relative path: add posts.
As per you folder structure you should have used
./posts.php?source=add_post
this means that you have your posts.php file present in one level up of admin_navigation.php (ie.
http://localhost/cms/admin/posts.php?source=add_post)
If you are giving
../posts.php?source=add_post
it will search for a file presents 2 levels up of admin_navigation.php (ie. http://localhost/cms/posts.php?source=add_post)

Formatting links to easily change a websites location (such as in a subdirectory of another website)

I need to take an existing website and place it as a subdirectory of another existing website. For example, I have www.website1.com and www.website2.com and I need to put website2 in website1 like this: www.website1.com/website2/. It should be noted I'm using IIS to host these websites.
I've actually run into this multiple times and I was wondering if there was a good way to write my links so I could easily drop a website anywhere and not have to reformat every link. I use two main paths - one for my root, and one for an includes folder outside my root. My first thought was to have two path variables in PHP for these paths, but then it couldn't go in my external js and css files. I don't want to turn those into php files if I can help it because of the caching complications that creates.
Anyway, does anyone else have a good solution for this? How have you handled it in the past?
Here's what I ended up doing:
For the items under my root directory, I used pure relative paths. So for example, when I access an image in my image folder, the link depends on how deep the page I'm on in the directory structure is:
<img src='./images/myImg.png'/> <!-- in root dir -->
<img src='../images/myImg.png'/> <!-- 2 levels deep -->
<img src='../../images/myImg.png'/> <!-- 3 levels deep -->
This is not ideal as I have to think about where every link is in relation to the file I'm in and if I change the location of the images folder or the file, I need to change every link. The positive though is as long as I don't start changing my directory structure, I can place this folder at any level and it will work without change. For example, I can do:
www.website2.com
www.website1.com/website2/
www.website1.com/sub-directory/website2/
And it works without any changes to the links.
For the items in my includes folder that is outside the root folder, I had to do it a little differently. For those, I used relative links in PHP, which I learned more about here http://yagudaev.com/posts/resolving-php-relative-path-problem/. He describes a few different options, but in the end, I created a global variable like thus:
$includesPath = $_SERVER['DOCUMENT_ROOT'] . "/../myProductsIncludes/";
This will look for the includes folder in the same folder as the root folder. If I need to adjust the location of the includes folder, I just change the link in this one variable. Then whenever I write a url to a file in the includes folder, I prepend the global var, like this:
include $includesPath . "classes/myClass.php";
This is all rather elementary stuff, and has some drawbacks so I wouldn't use it for every project, but it works great if I have a project that I don't know where it will end up in a directory structure (for example, if I'm contracting for a client). So for example, if I'm giving this to a client, he can drop the contents of my root folder into any subdirectory on his website and place the includes folder in the same folder as the root of his website (or just change that one variable if he wants to store it somewhere else), and it all works without changing a link.
If anyone has a different way of doing this, or any improvements to my way, I'd love to hear.

Formatting PHP include file's resource for use across multiple directory levels

I am attempting to use an include for my footer across all pages on my site. However, as the pages utilising the footer are spread across a variety of different directory levels the image paths in the footer are not correct for the pages in sub directories (assuming my image path is src="images/image1.jpg" and my image folder is in the lowest directory).
I thought about using $_SERVER['DOCUMENT_ROOT'] and building my image filenames from there but it seems a bit long winded. Is this the standard way of overcoming this or am I missing something!?
Thanks,
Rich
I would recommend consolidating your images in a single root level directory. If you want to organize them under that then you can add subfolders. If you really need to keep the image sin the individual folders youre including from then you can always symlink those folders into the top level images folder. This way you can always use absolute URLs to reference images, js, css.
The other way would be to write a helper function you can call that will determine the proper path based on $_SERVER['DOCUMENT_ROOT'] and the path to the file youre including (realpath(dirname(__FILE__)). That way you can just call the function instead of reqriting the code to figure it out in every file.

Is it better to store user uploaded documents in phymyadmin or is it better to store them in folders?

What is better? To be able to store user docs like word docs, pdfs, scanned images, etc in a database or in a folder. Security is of top issue. I don't want somebody to do something like domain.com/user/social/profile/user_images/23432434.png and be able to see it.
Thanks.
Storing files in the database is often regreted afterwards. Store a filename or id only in the database.
If you want to prevent access, store the files outside of the DOCUMENT_ROOT. Make a wrapper script that handles access, and use either readfile() or X-Sendfile: when invoked. (You would basically have done the same for SQL stored resources.)
If security is of top issue - use .htaccess and close public access to content folders.
You could save the assets behind the public folder (the document root folder where the app lies) and get them like http://myapp.com/assets.php?id=13 where assets.php grabs the file and output the content.
Now in assets.php, you can verify the roles, etc etc.
like for example:
root folder
public_html (where your script lies)
assets (where your uploads lies, they'll be inaccessible to the public eye and you can pull them when someone calls assets.php)
Does not really mather what you do if you store it in DB or on HD. The important thing is how you will be serving the files.
For this purpose you can you any kind of hash function.
add "salt" field to each user in DB
then you can do for example md5(md5({filename}) . salt)
and retrieving those files will be prety hard if you combine it with an captcha.
Store your files above the root of the server.
for example:
/var/www/ - your server root. Publicly available
/var/documents - user files here. above the root and not publicly visible.
Server your documents:
in /var/www/serve.php
#add headers
echo file_read_contents("/var/documents/file.doc");
Saving files in sql may cause some problems like as slower speed.
You can use
# no one gets in here!
deny from all
in your .htaccess and use echo file_get_contents($filename); or readfile($filename);

how to organize all js, css, php and html code?

Now I know that you can use OOP and MVC to organize, but that is just for PHP.
Let's say, I add a new window that pops up when a user clicks on a link and it displays a form with JS validations and it is CSS styled.
Here we got 4 codes: JS, CSS, PHP and HTML (with some PHP snippets).
How would you organize all these codes? Because when I got 50 windows codes are spreaded everywhere and for me to change behaviour of delete a window, I have to play detective. I crunch everytime I have to add a new window with JS, CSS and so on.
I have thought about the structure. wouldn't it be better if you got a separate "module" for each one of the window. E.g. a folder for each one of the window. In that map you place one CSS-, one JS-, one PHP-, and one HTML-file? Then you got a very nice structure that aren't messy and you dont mix all windows with each other in one big JS and CSS file.
What do you think? And I would appreciate suggestions of how to organize these 4 kind of codes.
I like Django's way to organize folders. Let's try to imagine and adapt it to your php project:
Root folder is the Project folder, let's say the name of the website. It contains
common shared settings and values (ie. database access values, paths etc.), maybe helper functions (not Object Oriented), call it settings.php and/or utils.php or whatever
a media folder that also has its own structure:
css folder, for general css (ie. reset.css and common.css, for defining a general layout)
image folder, for common shared images
js folder, for common shared javascript code
a template folder containing static common pages not belonging to particular categories of the website
Each Root subfolder is an expression of an application of your project (ie. registration, news, announcements, faq, contacts, forum, ...) and contains:
a model folder in which you put your Models in PHP (MVC pattern)
a controller folder where you implement the Controllers in MVC pattern
a view folder in which you put the Views of your MVC pattern (i.e. quite static php pages responsible just to present the results passed by the Views)
a media folder structured exactly in the same way you structured the one for the root folder. In this folder you put elements only belonging to the particular part of the website you're developing.
For connecting the components, you could directly call / include them based on their paths, or you could implement a php file inside the root directory and each subfolder that is responsible for mapping urls and redirect the requests. Call it index.php or urls.php or connector.php, whatever.
It may seem redundant but it is not, and provides a high quality separation of concerns.
I generally have my PHP pages in one folder, (maybe 10 files if it's a medium size site) then a subfolder in there called media, in which I put a css folder, a js folder an img folder, a swf folder etc.
I have 2 css files, one's a reset, the other has the style for the whole site written in chunks. I use a class on the body tag to target different page layouts.
The js folder has jquery, a file that's run on every page, then specific files for individual pages.
This keeps things pretty straight forward really.
If you are looking for examples on how to design an application, having a look at the many frameworks is a good start. Even just their file structure will give you an idea. Typically, they organize their code into modules where both the PHP code and HTML templates also reside. None in particular are better to look at, but try: CakePHP, Symfony, CodeIgniter, or Agavi.
They will not do a great job on suggesting how to organize JavaScript and CSS files, however. When I make an application, I typically only have a handful of CSS files. I am surprised that you seem to need one per page but if you do: embed them. The advantage of external style sheets is completely lost if there is nothing reusable about their styling. JavaScript files, again, if they are not reusable you should simply embed them. Less HTTP requests per page load makes everyone happy.
When you find yourself scouring for a particular file that stubbornly will not surface itself, grep is an invaluable tool. Here is a random article that illustrates its usage.
If you can/need I'd create top level folders for JS, CSS, PHP, etc that would contain code that could be used across different windows. There's no need to have 50 copies of the same CSS file if they are all the same or even mostly the same code.
Then create a folder for each "window" that has seperate css, js, etc folders. Here you could put the files that are specific to that particular window. This way if you're only changing 1 css rule or js function, that is used by every window you could change it in 1 place.
If you ever need to change the rule for just 1 window, put the rule in the "local css" folder for that window and it will override your default. (That is if your HTML Links it after the "global css")
If you expect the same user to open a few of these different popup windows during a single visit, for their sake you need to consolidate your files so they are cached and the end users doesn't need to load all the CSS/JS again on each popup.
The folder approach is fine for the images and the JS. Unless every popup is drastically different, I would suggest a single CSS file for your own sanity. So your folder structure might look like this:
css/
* layout.css
popups/
* add_new
- add_new.js
- logo.png
- add_new.php
* delete
- delete.js
- other_logo.png
- delete.php
Now, before you deploy you can decide if it makes sense to compile your JS into a single file, or if the separate files would be best. (For instance, if the user opens 30 of the 50 windows every visit, use a single file)
If you are only dealing with the file types mentioned, my best experience is to have folders and files separate everything, and it also keeps it organized with everything Super Easy to Find as your Site Grows & Expands!
OK, so as for folders, you want all you're main HTML, PHP files in the:
/public_html/
(or whatever it's called on you're host. ie. The main area with the cgi-bin inside that is viewable as your main web folder).
eg-:
public_html/index.html
public_html/contact-us-by-email.php
public_html/main.html
public_html/archives.php
NEXT is the CSS and Javascript (JS), these will have their own folders inside your /public_html/ folder. and each file ending in .css or .js go in those particular folders obviously...
eg-:
public_html/css/style.css
public_html/css/dark.css
public_html/css/mobile.css
public_html/js/colorpicker.js
public_html/js/contact-form-tips.js
public_html/js/main.js
And that is That! As easy as it comes to remember, and once you are advanced enough at coding this is the best way to do things. The more advanced you get, the more file types, and also the more 'neat' you are going to want to make it look = organized, you may want to add an /includes/ folder before the /css/ /js/ /templates/ /images/ & so on it goes!
I hope this works out for you, or one of them!
THIS IS MY 1st QUESTION ANSWERED, SO I'M PROUD OF MYSELF & HOPE THE POSTER LIKES MY SOLUTION, GOOD LUCK!

Categories