Locating Path in HTML - php

Main Folder1
- Submain1 folder
- filehere1.html
Main Folder2
- image.jpg
I currently working on filehere.html file, what I want to do is to locate image .jpg. How to locate this via path? Any idea?

To go folders back, you have to use ../ in the link. Spaces aren't recognised either, so you use %20 for them. Therefore, the path should be: ../Main%20Folder2/image.jpg

./ Is referring to current working directory
../ will refer to parent directory
In your case files are outside of current working directory
../ Main Folder2/image.jpg. Will give u the expected result

Related

glob - set relative path root directory

I'm having some trouble with glob in php and although I did some research, I just couldn't seem to find an answer to my problem.
First of all to my directory structure:
/ (root)
/images (folder)
- gallery/ (sub folder containing image files)
/pages (folder)
- gallery.php
I want to access the subfolder 'gallery' in the folder 'images'. So: In my gallery.php file I got the following line:
$images = glob("/images/gallery/*.*");
And this doesn't work.
What does work is, if I change the path to "../"
$images = glob("../images/gallery/*.*");
or if I change the code to:
define('BASE', $_SERVER['DOCUMENT_ROOT']);
$images = glob(BASE."/images/gallery/*.*");
Unfortunatly the sourcecode then shows some info I'm not sure I want to actually make public
e.g.
/home/scecjwkh/htdocs/images/gallery/3.JPG
I hope the information I provided is enough to actually understand my problem. Not sure why I have so much trouble with a relative path o.O
Thank you in advance,
Stuben
What you usually do is use __DIR__ to build relative paths from the current script's folder. You can also use __DIR__ to know what part to cut from the final paths, because you should know where in the project your current file is and thus know where the root is relative to it.
$images = glob(__DIR__."/../images/gallery/*.*"); gets the image list without caring about current working dir, after which you can use realpath(__DIR__.'/..') to figure out how much to snip from each file path.

PHP use full path from root to link to file instead of going from parent

I need to be able to access a file in a folder located in the root directory on each of my pages.
Is there a piece of code I can use to type out the full directory going from the root folder to the specific folder I want and be able to copy onto each page without having to change the code?
For more clarification, it's hard to word, but I want to be able to link top my CSS file in the directory /css on each page without manually having to put ../../ etc.
Or does anyone else know the best way to link one style sheet to each page with ease?
If the root of your website has the path / (in other words, the address you type for the root of your website is something like http://mydomain.com/) then you can simply refer to your CSS files using /my.css or /css/my.css in all pages. The leading / tells the browser to look for these files relative to the root of your website.
You should be able to use a relative path of /folder path that will be accessible from all your pages.
You should also be able to do it the same was Cristian is recommending with:
$folder = $_SERVER['DOCUMENT_ROOT']."/folder path";

how does the php copy function destination string ( ./ )work?

Okay so I'm learning about uploads and need to copy the tmp file to a specified directory so I use the copy command.
I use this inside my index.php of the main directory (learningupload/ folder)
copy($_FILES['photo']['tmp_name']['file'], './pics/'.$photoname.'.jpg');
Now I'm doing this on Xampp local host, and my uploads go to the tmp folder which is of course different from where I want the upload to go
so there's
C:\xampp\tmp
C:\xampp\htdocs\learningupload\pics\
My question is this: What is this ./ inside the destination string I need to have? Doesn't it usually mean you're moving UP a directory? Why not have it '/pics' no dot? I tried that and said it couldn't open the stream. Does the dot here mean it's referring to whatever directory index.php is in? Or am I thinking ../ moves up a directory? And single dot is different?
Thanks.
./ means "current working directory". Which directory is current working one you can see with echo getcwd();
The parent directory (the upper one) is ../
You can change your working directory using chdir() or you can just specify the full path instead.

Open file from a different folder in PHP

Let’s say I have two folders in my PHP server root directory: x and y. There are two more directories inside the x folder: a and b. In the /x/a directory I have the index.php file and in the /y folder a stuff.php file.
From my /x/a/index.php file, I want to include the /y/stuff.php file. How do I do that?
My server doesn’t allow including files from other domains, so adding the full URL doesn’t work!
Also, I’d like to know how to start a path from the root in PHP. I use ./blabla from the index of my root and ../blabla from directories in the root, but, unfortunately, .../blabla doesn’t work from 2nd grade directories.
To access the root begin your path with "/".
If you want to go up one directory from where you are currently, e.g. from /x/a/ to /x/ you could use "../".
If you want to go back up two directories (e.g. from /x/a/ to /) you could use "../../" (rather than ".../" which you mentioned).
try:
include "../../y/stuff.php";

What does "URL or relative path from IM to your main site" mean?

I wanted to use a script using instant messaging and found this.
The instruction said that to Make sure that in blab_im/config.php you've set properly $site_to_bim and $bim_to_site:
$site_to_bim='localhost/ThesisDB/blab_im/'; // URL or relative path from your main site to BLAB!IM, default:> $site_to_bim='./blab_im/'; [must end > with a trailing slash]
$bim_to_site='../'; // URL or relative path from BLAB!IM to your > main site, default: $bim_to_site='../'; [must end with a > trailing slash]
i was able to understand a little bit the first part(Correct me if I am wrong)
the second part I do not understand which is bim_to_site.
Thanks/Salamat in advance!
The second variable is asking for the path to take from the directory that the im client script is in to the www root.
So, for instance if the im client is here:
www.example.com/files/imclient
that would give you:
$site_to_bim = '/files/imclient/;
$bim_to_site = '../../';
because you go up 2 levels from the im directory to the main site directory.
Update
When you are specifying a relative path, you can start a few different ways:
with a slash: /files/. That will tell the parser (or browser) to try to find the target beginning from the main directory, then into the files directory.
with simply the directory name: files/. That means begin looking where you currently are and look for the files directory. So if the script resides in the imclient directory from the above example, this would mean "look for the files directory inside the imclient directory.
with a dot slash: ./files/. This means the same as the above. Start in the current working directory.
with two dots and a slash: ../files. This means start in the current working directory and move up out of it to the parent directory. So in the example, if the script was in imclient, using ../ would say "look for things in the files directory and ../../ would say "look for things in the main directory (the parent of files).
Does that flesh it out a bit?

Categories