php mkdir windows relative path - php

I want to create a directory on windows from a PHP script.
My script is in the www/Test directory of Apache and I want to create a folder (fold1) inside www/downloads directory.
Inside the script, I'm using:
$dirName = "../downloads/fold1";
mkdir("{$dirName}");
If I use the full path of dirName like C:\Apache\www\downloads\fold1, it works fine.
But I want to use a relative path since this code will be sent to the client.

I would guess your current directory is different from your files folder, so you have to use a trick:
mkdir(dirname(__FILE__) . "/" . $relative_path);
dirname(__FILE___) returns the absolute path of your current php file. With this you can build an absolut 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.

PHP unlink file from root-relative path

My script is in http://localhost/path/test/index.php and the file I want to delete is in http://localhost/path/media/test.txt.
I want to have the path of the project as a constant PATH which would be path/ in this example. So I tried it with the root-relative path unlink("/" . PATH . "media/test.txt"), which didn't work.
Any ideas how to solve this path problem?
By putting a / at the begining of the unlink, you are telling PHP to dlete from the root of the server's file system, which is unlikely to be the same folder as your localhost (probably /var/www/)
Ideally in web applications, you should define the root of your application in the filesystem, e.g.:
$root = '/var/www/sites/project/';
Then you can unlink like:
unlink( $root . "media/test.txt" );
Alternatively you can unlink by relative, rather than absolute path (as above:)
unlink( '../media/test.txt' );
To get your root, see: this

Including file with backward slash is won't work in PHP?

Am running scripts using command-line .
Backward include path is doesn't work for me
eg: require("../../../../test.php");
(But in browser it works)
try
dirname(__FILE__); will give you real path to current directory and you're then about le to include your file
require(dirname(__FILE__)."/../../../../test.php");
From the command line, the base path should be the directory from which you are calling php. When running in the browser (through apache, etc) the calling path should be the webroot directory.
So you should specify the directory of the current script or call it from its own directory.
For instance:
require(__DIR__."../../../../test.php");
DIR is a PHP Magic Constant (http://php.net/manual/en/language.constants.predefined.php) which returns the directory of the currently running file.

PHP - Is there a decent way so that the calling of mkdir creates folders relative to the web root?

The directory and file structure is as follows:
C:\xampp\htdocs\PHP_Upload_Image_MKDIR\uploaded
C:\xampp\htdocs\testA.php // as follows
$userID = 's002';
$uploadFolder = '/PHP_Upload_Image_MKDIR/uploaded/';
$userDir = $uploadFolder . $userID;
mkdir($userDir, 0700);
If I call testA.php, then the following folder will be created.
C:\PHP_Upload_Image_MKDIR\uploaded\s002
However, the desired result should be the following:
C:\xampp\htdocs\PHP_Upload_Image_MKDIR\uploaded\s002
I would like to know a decent method so that the mkdir creates folder relative to the root of the web
C:\xampp\htdocs\
or
C:\wamp\www
Then in the future, I don't have problems to move this application to a web hosting site.
Thank you
You can:
Create a file with defines that's always included and where you define what's the server root (or whatever prefix) so that you prepend it to the directory.
Use a relative directory to the script. The directory of the script can be obtained with dirname(__FILE__).
Use a relative path to the current directory. The current directory, if not changed, is usually the directory of the PHP script that was originally called (usually not a good option).
Use $_SERVER[DOCUMENT_ROOT] (not a very good option, you might want your application in a subdirectory, and whether this value is available depends on the web server).
$uploadFolder = $_SERVER['DOCUMENT_ROOT'] . '/PHP_Upload_Image_MKDIR/uploaded/';

php paths that work on both local (mac) and remote server

I'm testing a website on my local machine and I'm wondering what would be the best way to write paths to make sure they work when I upload the site to its final location.
In general I'm a bit confused about paths, and in many cases I have to 'tweak' each path until it works, so wouldn't want to be forced to do the same on a production site!
I'm not clear when to use $_SERVER['DOCUMENT_ROOT'].
For example, I have a directory that I want to scan, which is just under the root. So why can't I just use "/dirname"?
$dir = $_SERVER['DOCUMENT_ROOT'] . '/uploads'; //this works
// $dir = "/uploads"; //this doesn't work
if (is_dir($dir)) {
//do something..
}
I'm not clear when to use
$_SERVER['DOCUMENT_ROOT']. For
example, I have a directory that I
want to scan, which is just under the
root. So why can't I just use
"/dirname"?
When you work with paths in the php file the root (/) is the root of the filesystem, not the root you get when you visit your website.
So $dir = "/uploads"; gives you the filesystem root.
To minify your problems I would declare a variable in a configuration file that specifies the root of your php application, and use that path+whatever more is needed.
As adamse mentioned, the reason you can't use the '/path' is because it points to the root of the filesystem.
However, instead of declaring a variable that defines the root, I recommend using dirname(__FILE__) to retrieve the full path to the directory that the calling file is in.
From there, append relative path information to the file you want and you end up with a complete path, fully dynamically.
For example, if you want to include the 'header.php' file in the directory above the file that you wish to include it in use:
include(dirname(__FILE__) . '/../header.php');
The beauty of that is that PHP will always automatically convert the forward slash to the directory separator required for the host OS.
I would define a variable/constant that describes the absolute filesystem path to the application. Something like this:
$appDir = rtrim(str_replace('\\', '/', realpath(dirname(__FILE__))), '/');
Then you have this base path you can address your application’s files from:
include $appDir.'/library/foo/bar.php';
Or you even change your include path to that directory:
set_include_path($appDir);

Categories