Create php directory at root using php - php

I have this folder tree:
assets/
scripts/
sites/
create.php
index.html
sites/
The create file (create.php) has the following in it:
<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
$data = json_decode(file_get_contents("php://input"));
$fileName = $data->fileName;
$url = './sites/'.$fileName.'/';
mkdir('../../sites/'.$fileName);
I am trying to make a folder in the sites folder at the root of the website.
When I run the code above, I get this error:
Warning: mkdir(): No such file or directory in /Users/mikes/websites/simple/assets/scripts/sites/create.php on line 12
How the heck do I make a folder inside the sites folder at the base of the site?
UPDATE
Through some research, I got this.
mkdir($_SERVER['DOCUMENT_ROOT'] . '/simple/sites/'.$fileName);
I do not like that I have to include the '/simple/' in the url. It's not going to be very friendly when I upload this to my server. How would I automatically detect that?

mkdir(__DIR__.'/../../../sites/'.$fileName);
__DIR__ is a magic constant, that return the absolute path of the directory of the included file. You can read more about here.

Your're only going back 2 levels with ../../. you need to go back 3. ../../../
You may also want to make use of realpath('../../../sites/'.$fileName); to get an absolute URL if that helps.
Finally, you may want to check permissions of the directories that you want to mkdir in. most likely something like www-data group (for apache) and 0775 as the permission on the directory.

Related

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.

No such file or directory (path error)

Well, the structure of site is simple:
site.com
'config' folder
config.php
cesar.php
'login' folder
index.php
index.php
Config.php:
include_once '../config/cesar.php';
At site.com/index.php:
Warning: include_once(../config/cesar.php): failed to open stream: No such file or directory
At site.com/login/index.php everything is OK.
If I will remove one dot (./config/cesar.php), main index will become OK and login page will get the error.
How to make both codes work?
The .. in your path are used to go up a directory because of that the file won't exist where you are looking for it.
If you update it to be include_once 'config/cesar.php'; it should work since that will allow it to go down into the config directory rather than try to find a directory with the name of config 1 level above where index.php is located.
./ works since . is the notation for the current directory.
To answer your question, it wouldn't be possible to have the code work by using a relative path since both the files are in different locations on the server in relation to the one you want to include. If you want to have something that does then you will need to use an absolute path rather than the relative path. This would be something like /path/to/webdirectory/site.com/path/to/file/config.php (i.e. /home/charles/websites/site.com/config/config.php) in *nix and C:\path\to\webdirectory\site.com\path\to\file\config.php on windows.
In PHP you should be able to get the absolute path in a dynamic way by using the $_SERVER['DOCUMENT_ROOT'] variable. Ex: include_once $_SERVER['DOCUMENT_ROOT'] . '/config/cesar.php';
Warning: include_once(../config/cesar.php): failed to open stream: No such file or directory
I ran into a similar problem once. You need to remember that (./config/xxx.php) and (config/xxx.php) mean the same thing but (../config/xxx.php) will go up a directory and find the config folder and xxx.php file in it.
You can also use $base_url as a prepend string to all your paths to make it clean. Where:
$base_url = 'http://' . $_SERVER['SERVER_NAME'];
Then replace your paths like
../config/xxx.php
with
$base_url.'/config/xxx.php'

Mysql is not connecting with php in wamp server.

require(../connect_db.php): failed to open stream: No such file or directory in C:\wamp\www\php\require.php on line 10 in wamp server. This is my first time trying to connect to mysql with php. Can anyone help me please?
You are using a relative path (../connect_db.php).
You need to take into consideration that the path is not relative to the file where the require is used, but to the file that is run.
So, if you have your main file index.php, you could have the following:
// index.php
include('config/config.php');
// config/config.php
include('config/db/connect.php'); // relative to index.php folder
include('db/connect.php') // WON'T WORK
The directory structure would be:
index.php
config/
config.php
db/
connect.php
Be sure that connect_db.php file exists in C:\wamp\www dir.
Also check folder permissions on that directory (I don't know which user uses wamp, but try first to give permissions to all users and then filter a little more).
be sure to check these following reasons:
file exists
file location is correct ( '../file') means upper location
failed to open stream: No such file or directory
Your path is incorrect.
For example, your code require('../connect_db.php'); does the following;
Go up one directory
Require connect_db.php
If we have the following tree structure, your code will work
/wamp/
www/
connect_db.php
php/
require.php
I would suggest moving connect_db.php into php/ and altering your require to become require('connect_db.php');. Or, paste your tree structure, and we can see where you're going wrong.

No such file or directory error using mkdir

I am encounter with a problem when i give full path to mkdir it says No such file or directory but when i am providing the same back but first going back to one directory it work
I am just want to ask why this is not working
$name = "4ftwx"; // dir name
$domain = $_SERVER['HTTP_HOST'];
mkdir($domain.'/project/'.$name);
//localhost/project/4ftwx
but it work when i call like this
mkdir('../project/'.$name);
both pointing same path then why it is not working
Maybe you should use $_SERVER['DOCUMENT_ROOT'] to get the document root directory under which the current script is executing.

Requiring files from the root directory

Is it possible to require files (using php require) from the directory where my website is placed?
For example, my website is in the directory mywebsite which is in the root directory. There is another directory there. Can I require files from this another directory?
Sure, you can require files from anywhere that has the appropriate permissions.
This requires the file from the current directory (NOT always where the current PHP script is, so be careful of that):
require("whatever.php");
This will require whatever.php from somefolder which is in the current directory.
require("somefolder/whatever.php");
Finally, you can give an absolute path like this:
require("/var/www/includes/whatever.php");
Require from parent directory:
require("../includes/watherver.php");
It doesn't matter really where you get it from, provided you have the permissions set correctly, and PHP is configured in such a way to allow you to do so.
I have been using
require_once $_SERVER['DOCUMENT_ROOT'] . '/myfile.php';
It should be no problem to require from an arbitrary existing and readable directory.
Image you have:
/
--folder1
--folder2
and in folder1 is your index.php and in folder2 is your to_require.php
Then you could write:
require('../folder2/to_require.php')
That's because you can go up in your directory tree with ..

Categories