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.
Related
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.
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'
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.
On localhost I normally do this
file_put_contents('/script.zip', file_get_contents('http://example.com/script.zip'));
But it seems on my vps the php file doesn't get doesn't 'detect' the current
directory. So I have to get document_root and manually set the path.
$root = $_SERVER['DOCUMENT_ROOT'];
file_put_contents($root.'/script.zip', file_get_contents('http://example.com/thescript.zip'));
I'm sure there must be a way to configure nginx or php to automatically
detect the current dir, but I can't seem to find how to do that. Maybe
I don't know how to search this specific issue.
If you want the file to be created in you current dir use the '.' as follows :
file_put_contents('./script.zip', file_get_contents('http://example.com/script.zip'));
The '.' will point to the current directory running the php script.
I'm trying to upload images to server, create a thumbnail from the uploaded image, and save the relative paths to mysql database . . . . The image path is
/mysite/images/
from my site root (which is wamp/www/mysite/images in my local testing environment)
Now, I'm performing a directory check with
if(!is_dir($path) && !is_writable($path)){
throw new Exception ("$path is not valid .. ");
}
where path is /mysite/images/ , thinking that initial '/' will denote the root.
But, the script was throwing an exception saying invalid path name, and my
move_uploaded_file($tmp_name, $path.$filename)
too was not working.
PS: the path mysite/images/ also throws an error and the file is not moved
Now, I have worked around by appending document root manually to path name for both directory checks and move file , which is now looking like
if(!is_dir($_SERVER['DOCUMENT_ROOT'].$path) && !is_writable($_SERVER['DOCUMENT_ROOT'].$path)){
throw new Exception ("$path is not valid .. ");
}
move_uploaded_file($tmp_name, $_SERVER['DOCUMENT_ROOT'].$path.$filename)
I also changed the path to
mysite/images/
removing the initial slash intended for site root. This is working perfectly.
But, I cannot understand why directory check and move uploaded file snippets are not taking site root relative links.
Any ideas is appreciated.
The problem is with the path representation.
In linux /mysite will try to access the directory in the root level of file system. Windows this might not create an error. for is_file() kind of API's it should the absolute path and not relative ones.
Your website path is not wamp/www/mysite/images.
You are using a web server, you cant access the drive like: c:/wamp ....
Once online, how will you access your files? Linux does not have c:/ ...
Your website path is: http://localhost/mysite/
Your image path is: http://localhost/mysite/images or ./images if your code is correctly written.
so, do this:
define('ROOT', $_SERVER['DOCUMENT_ROOT']);
include_once(ROOT."/mycfgfile.php");
or
include_once(ROOT."/includes/mycfgfile.php");