PHP MKDIR inside server root but outside domain root folder? - php

I have a domain root which is a folder within the server root. How can I mkdir inside a different folder within the domain root?
ROOT > DOMAIN ROOT FOLDER > PHP MKDIR
ROOT > IMAGE FOLDER > MKDIR HERE
I need the php in the 'domain root folder' to create a directory within the 'image folder'. Any help?

You can usually do something like this:
mkdir('../images/newfolder');
You may have to add more ../../ on to the front of this, depending on where you are running the script from exactly. Each ../ moves you up another level, so it seems like just one would work here given your example, but you have to be sure you are where you think you are.

You could also use absolute path in your php-script.
mkdir('/root/images/new_folder');
Make sure that your web-server has permissions for writing to specific folder.

I usally work from the directory my current script is located in and maneuver my way to the desired location from there. Works fine as long as you don't move you script. In addition, it will be easier to keep track of where everything is relative to the script.
You can get the current path with:
$path = dirname(__FILE__);
Then appending ../ to go deeper etc.

Related

access root file from subdomain in PHP

Is it possible to include file from the ftp's root in a script in a subdomain ?
This doesn't work for me:
include($_SERVER['DOCUMENT_ROOT'].'/joomla.php');
Can someone help me ?
I think first you need to determine where is your subdomain files are placed and echo the $_SERVER['DOCUMENT_ROOT'] command to see the full path then you can combine two paths and make necessary adjustments.
Usually but not always, subdomain files are placed in a folder one level up then the main site's root folder so you may need to replace the folder name according to your needs in the output of server document folder function.
To give an example if your domain files are in a path like
/var/www/vhosts/example.com/httpdocs/
and if your subdomain path is like
/var/www/vhosts/example.com/subdomain/
then you will need to replace httpdocs with subdomain (or the other way around) in the output where you call $_SERVER['DOCUMENT_ROOT']

Accessing PHP script files outside site directory using path relative to root

I'm using apahce server and I have files that I want to keep private. I've read that they should be put in a directory outside of the document root.
I could access them from the root directory using:
<?php include('../includes/somefile.php');?>
To simplify navigation I want to use paths relative to the root. This works fine for directories within the site such as:
define('ROOT_PATH', $_SERVER['DOCUMENT_ROOT']);
include(ROOT_PATH.'/path/file.php');
but I get errors when I try going up the directory tree like this:
include(ROOT_PATH.'../includes/somefile.php');
Am I doing something wrong here?
This is because .. is taking to the previous directory of the DOCUMENT_ROOT which you do not have permission to access.

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

PHP File Directory - $_SERVER['DOCUMENT_ROOT']'s parent folder

I know $_SERVER['DOCUMENT_ROOT'] returns the document root (most likely something like /home/user/public_html), but is there any way to get the parent directory of this folder? (i.e. just /home/user/)
I would try and replace common folder names like public_html or www, but it's not a given that the folder would be named that on all systems I want this code to run on.
FYI: I'm trying to implement this, but i want the user to be able to select their FTP root folder, which is most commonly the parent of the Document Root folder.
Any thoughts?
dirname($_SERVER["DOCUMENT_ROOT"]);

Categories