glob - set relative path root directory - php

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.

Related

How to upload file in any directory with PHP

I have created image uploading codes and they're only allowing me to upload image into only directory that is in the same directory as PHP file.
$profile = 'profiles/'.$_FILES['profile']['name'];
if I change it like this:
$profile = 'php_codes/profiles/'.$_FILES['profile']['name'];
it shows me error. I'm using copy() function to upload it.
Please help me to get to know how to upload it in any directory even into any other partition. Thanks for your help.
You will need to provide the error, but it's very likely that the error you are getting is that the directory that you are attempting to copy the file to does not exist. It could also be permission problems, but your confusion seems to be over building a path.
The problem with your code is that your path is relative to the PHP file's location, rather than being a direct path from your root directory. You should read a little bit about how to navigate file structures, but these are the three key things to remember when working in a *nix file system (such as Linux):
If your file path does not start with a slash, then the path will be relative to the directory that the PHP script is in.
If you start your file path with a slash, the path will be relative to the root directory.
You start a path with one or more ../, to traverse to a parent directory.
So for example, let's say you have these three directories, with your PHP script residing in /php_codes:
/php_codes
/php_codes/code_snippets
/profiles
If you wanted to copy the file to php_codes, your path would be relative to the PHP script:
$profiles = $_FILES['profile']['name'];
If you wanted to copy the file to php_codes/code_snippets, again you could just do it relative to your PHP script:
$profiles = "code_snippets/" . $_FILES['profile']['name'];
However, this is an opportunity to also show how you might do it with an absolute path from the root directory. You could use this (note the slash at the beginning of the path):
$profiles = "/php_scripts/code_snippets/" . $_FILES['profile']['name'];
If you want to copy the file to /profiles, which is outside of the /php_codes directory, there are two ways you can do it.
The first way is with an absolute path from the root directory (path begins with a slash), just like the example above:
$profiles = "/profiles/ " . $_FILES['profile']['name'];
Or, you can make it relative, by using ../ to go up one level to the parent directory:
$profiles = "../profiles/ " . $_FILES['profile']['name'];
A quick note about using ../ to go up one directory: you can repeat that as many times as needed, to continue going up a level. For example, if your PHP script was located inside of /php_codes/code_snippets, but you wanted to copy a file to /profiles, then you would have to go up two levels:
$profiles = "../../profiles/ " . $_FILES['profile']['name'];
you can use
$profiles=__DIR__ . "/profiles/" . $_FILES['profile']['name'];
that will work as DIR will be your script directory and then it will be absolute path
I have finally got it! Just using ../profiles and then directory is making it. You just write dots (2) then times you want to go back. If two times, ../../profiles. Thanks for your help.

Getting path from current working directory not from root

Hello everyone I am trying to get the path from the current working directory but I don't want the root path included with it in PHP or wordpress. For example I have a directory structure like this.
-C
--wamp64
--------www
-----------project
------------------Other directories
Now, what exactly I want is to get the path from project directory something like this.
-project
--Other directories
Currently with __DIR__ or dirname(__FILE__) I am getting.
C:\wamp64\www\project\other_directories
But I want to get.
\project\other_directories
And I wanted to make it dynamic so that it picks up the right path from which ever server I deploy this project.
I searhed alot but couldn't find the solution.
https://www.w3schools.com/php/showphp.asp?filename=demo_global_server
Check above URL and use $_SERVER['PHP_SELF'];
remove the filename from that and you will have the path relative to the document root.

Relative Path Basics

Although I have a fair idea of relative path but I am a little confused while using this particular relative path in a program. Please someone guide me through. I have the following directory structure:
The problem here is that I want to include config.php file in left.php, for that I have given a relative path, include(../../layout/config.php); but somehow this is not being included and the relative path that I have to use is include(../layout/config.php);. Can someone please explain why is that so? Because my knowledge says that I will have to leave includes->admin and then enter layout.
In PHP it's the working direcory that is used as the main path.
All relative paths originate from that folder.
As you wrote in comments you use index.php which is one folder lower than left.
I assume you are in index and include left.php, this does not update the working directory.
Your working directory is still "admin".
You can find out what the working directory is using echo getcwd();
http://php.net/manual/en/function.getcwd.php
As I wrote in comments you can use include($_SERVER['DOCUMENT_ROOT'] . "/Electronix_Store/electronix/layout/config.php"); to use full path instead.
This is easier if the files and folders are in the same position all the time.
My advise is to only use relative paths if you are working with day "user/123/" -> some folder structure in there.

include() and require_once() Issue

I have this situation:
/
index.php
/init (folder)
init.php (of course inside this file i use require_once for db/function/and whatever you want to put inside)
/layout_parts (folder)
header.php (and others common parts are inside this folder)
/my_space (folder inside layout parts)
my_file.php
another_file.php
/your_space (folder inside layout parts)
your_file.php
another_file.php
/third_space (folder inside layout parts)
third_file.php
another_file.php
For the index i have no problem it all works fine, but if i include header.php in one of the subfolder files my require_once(init/init.php); that is on the top of header.php won't work.
(Warning message says no such file in the directory (OF COURSE THE FILE EXIST) and it write down the subfolder directory that is not the path i expected).
I tried echo $SERVER['DOCUMENT_ROOT]; to see the whole path, echo __DIR__; to see wich is the right path to the directory, no way out.
I know is such a dummy question but if some kind heart could help me it would be great. Thanks :)
The best way to handle this would be to use absolute paths instead of relative ones. When you use relative paths, you have to worry each time about how many levels deep you are and then have that many ../ etc. This is messy and difficult to manage, for eg, if you move a file to a different location, you have to update the includes again, based on where you are now. This is why absolute paths are helpful.
So create something like this:
$appRoot = "/path/to/root/folder/";
This is the path where the index.php file is located. It is the "application root", if you will.
Now, in ANY file where you want to include init.php add the following line:
include_once($appRoot."init/init.php");
Ideally the $appRoot should be created in a global config located in root. If you do not have a structure where a univeral config can be added, it can still be messy and you might need to add absolute paths into individual files (which is not a good idea)

how can I get a URL from an absolute folder path?

I have a php function that is moving files for me. It requires absolute paths to place those files (/Applications/MAMP/HTdocs/mysite/myfolder)
how can a turn this folder path string into a url (http://mysite.com/myfolder) so that I can create links to the files on the fly?
I do not know necessarily the names of the folders, as the software could be run in many locations.
Many thanks.
Obviously, you need to know server root for such a calculation.
Luclikly, $_SERVER['DOCUMENT_ROOT'] contains this path.
So, just subtract server root path from the given path:
$path = '/Applications/MAMP/HTdocs/mysite/myfolder';
$approot = substr($path,strlen($_SERVER['DOCUMENT_ROOT']));
check if you have a drive letter in the DOCUMENT_ROOT, and correct the code if necessary
Note that adding http://mysite.com is unnecessary and useless. just /myfolder/ is what you really need.
You can check for this value: $_SERVER["DOCUMENT_ROOT"];
That is the root of your website. If you have the folder and replace the $_SERVER["DOCUMENT_ROOT"] with the $_SERVER["HTTP_HOST"] you will get the URL to the folder/file
If mysite folder is within HTdocs then you can access it using http://yourdomain/mysite, (if HTodcs is your home directory)

Categories