simple php question (hopefully)

jfall

Diamond Member
Oct 31, 2000
5,975
2
0
All I want to do is get the current directory it's running from and chdir to it

Anyone know the lines to do it?

I know getcwd() will get the dir, but how to I connect that with chdir()
 

statik213

Golden Member
Oct 31, 2004
1,654
0
0
What do you mean chdir to it? Do you plan on executing some script/program in that directory?

chdir
(PHP 3, PHP 4 )

chdir -- Change directory
Description
bool chdir ( string directory)


Changes PHP's current directory to directory. Returns TRUE on success or FALSE on failure..

See also getcwd().
 

statik213

Golden Member
Oct 31, 2004
1,654
0
0
also:

See Also
For related functions such as dirname(), is_dir(), mkdir(), and rmdir(), see the Filesystem section.

Table of Contents
chdir -- Change directory
chroot -- Change the root directory
dir -- directory class
closedir -- close directory handle
getcwd -- gets the current working directory
opendir -- open directory handle
readdir -- read entry from directory handle
rewinddir -- rewind directory handle
scandir -- List files and directories inside the specified path
 

jfall

Diamond Member
Oct 31, 2000
5,975
2
0
Basically this is part of a helpdesk application which I'm attempting to make an installer for (or at least make it easier to setup). Mail pipes to a file called new.php which then interacts with the database to log a ticket etc. the new.php file needs to have something like this in it:

chdir('/home/helpdesk/www/helpdesk/email');

I want it to work regarldess of where the files are (so the path doesn't have to be specified manually). To do this, I need the new.php script to find out it's current directory and then use chdir as shown above to change to that directory.
 

statik213

Golden Member
Oct 31, 2004
1,654
0
0
getcwd
(PHP 4 )

getcwd -- gets the current working directory
Description
string getcwd ( void )


Returns the current working directory.

See also chdir().

so chdir(getcwd()) should do it for you.
Or if you like you need to get to a directory to which you know the relative path use:
Description
string realpath ( string path)


realpath() expands all symbolic links and resolves references to '/./', '/../' and extra '/' characters in the input path and return the canonicalized absolute pathname. The resulting path will have no symbolic link, '/./' or '/../' components.

realpath() returns FALSE on failure, e.g. if the file does not exists.

Example 1. realpath() example

<?php
$real_path = realpath ("../../index.php");
?>




See also: basename(), dirname(), and pathinfo().

Also,
Note: Introduced in 4.1.0. In earlier versions, use $HTTP_SERVER_VARS.

$_SERVER is an array containing information such as headers, paths, and script locations. The entries in this array are created by the webserver. There is no guarantee that every webserver will provide any of these; servers may omit some, or provide others not listed here. That said, a large number of these variables are accounted for in the CGI 1.1 specification, so you should be able to expect those.

This is a 'superglobal', or automatic global, variable. This simply means that it is available in all scopes throughout a script. You don't need to do a global $_SERVER; to access it within functions or methods, as you do with $HTTP_SERVER_VARS.

$HTTP_SERVER_VARS contains the same initial information, but is not an autoglobal. (Note that $HTTP_SERVER_VARS and $_SERVER are different variables and that PHP handles them as such)

If the register_globals directive is set, then these variables will also be made available in the global scope of the script; i.e., separate from the $_SERVER and $HTTP_SERVER_VARS arrays. For related information, see the security chapter titled Using Register Globals. These individual globals are not autoglobals.

You may or may not find any of the following elements in $_SERVER. Note that few, if any, of these will be available (or indeed have any meaning) if running PHP on the command line.



'PHP_SELF'
The filename of the currently executing script, relative to the document root. For instance, $_SERVER['PHP_SELF'] in a script at the address http://example.com/test.php/foo.bar would be /test.php/foo.bar.

If PHP is running as a command-line processor, this variable is not available.

...


'SCRIPT_FILENAME'
The absolute pathname of the currently executing script.

...

'PATH_TRANSLATED'
Filesystem- (not document root-) based path to the current script, after the server has done any virtual-to-real mapping.

'SCRIPT_NAME'
Contains the current script's path. This is useful for pages which need to point to themselves.

...

Find which variable has the info you need then use...
so maybe chdir(dirname($_SERVER['SCRIPT_FILENAME']))?
see below:

Description
string dirname ( string path)


Given a string containing a path to a file, this function will return the name of the directory.

On Windows, both slash (/) and backslash (\) are used as path separator character. In other environments, it is the forward slash (/).

Example 1. dirname() example

<?php
$path = "/etc/passwd";
$file = dirname ($path); // $file is set to "/etc"
?>

/
Also, try doing a
var_dump($_SERVER);
var_dump($_ENV);

to see what else is available, it varies a little from server to server and apache/IIS.

g'luck!!!