dbmopen

(PHP 3, PHP 4, PECL)

dbmopen -- Opens a DBM database

Description

resource dbmopen ( string filename, string flags )

The first argument is the full-path filename of the DBM file to be opened and the second is the file open mode which is one of "r", "n", "c" or "w" for read-only, new (implies read-write, and most likely will truncate an already-existing database of the same name), create (implies read-write, and will not truncate an already-existing database of the same name) and read-write respectively.

Returns an identifier to be passed to the other DBM functions on success, or FALSE on failure.

If NDBM support is used, NDBM will actually create filename.dir and filename.pag files. GDBM only uses one file, as does the internal flat-file support, and Berkeley DB creates a filename.db file. Note that PHP does its own file locking in addition to any file locking that may be done by the DBM library itself. PHP does not delete the .lck files it creates. It uses these files simply as fixed inodes on which to do the file locking. For more information on DBM files, see your Unix man pages, or obtain GNU's GDBM.

注: 安全模式被激活时,PHP 将检查被操作的文件或者目录是否与正在执行的脚本有相同的 UID(所有者)。


add a note add a note User Contributed Notes
sitz at onastick dot net
17-Jul-2001 04:11
Want to use dbmopen() to open an NDBM DB under solaris? Want to have GDBM support as well? Tough; you can't. You *can* use dba_open(), but you need to jump through a couple of hoops first:

First, you need to re-roll libgdbm so that DBM/NDBM compatiblity are removed. For GDBM-1.7.3, change this line in the Makefile:

OBJS = $(DBM_OF) $(NDBM_OF) $(GDBM_OF)

to this:

OBJS = $(GDBM_OF)

Once that's installed, re-run ./configure --with-gdbm=/<prefix> --with-ndbm --with-db=/<prefix> <other args>, where <prefix> is the directory which holds the include/ directory containing the associated header files. make, make install, adjust your scripts to use dba_open() with the ndbm handler. Voila!
donotspam-dbirchall at cheaptickets dot com
07-Jul-2001 11:20
Like the last few posters, I noticed dbmopen() suddenly stop working when I went from PHP3 to PHP4.  Fortunately, I (unlike them? ;) noticed mavetju's post suggesting rebuilding PHP with --with-db and --with-ndb.  Unsurprisingly, this solved it.  Many thanks to mavetju, and I encourage other users to read answers that have already been posted. :)
mavetju at chello dot nl
11-Jan-2001 10:19
If you are running php >4.0.2 and you have suddenly problems with dbmopen (i.e. it complains that the function does not exist), recompile php with --with-ndb *and* --with-db
skipatrol at sprintmail dot com
13-Oct-1999 11:35
If using php3 on a win 95/98 machine, you must have the complete path to the db file.  And always remember that if you user the " mark, add one extra slash before any regular slashes.  Otherwise use the ' and normal slashes.

$dmb = 'd:\httpd\storit\users';

$modify = dbmopen($dmb, "w");
$username = dbmfetch($modify, $email, $username);
$password = dbmfetch($modify, $username, $password);

echo "Current username: $username";
echo "Current password: $password";

exit;
admin at cinenet dot com dot mx
28-Jul-1999 06:52
Most of the dbm functions can use the full-path of the dbm file as the argument instead of the db identifier generated by dbmopen() in a similar way it used to be on php2. The big exception is dbmclose() that MUST use the db identifier, otherwise the dbm file keeps open. Be aware of that if you're updating your php2 files.
i.e. this code works in the most lines, except to close the dbm file:

$fn="/full/path/of/dbmfile";
dbmopen($fn,"r");
$row=dbmfetch($fn,"MyKey");
dbmclose($fn);

Instead, allways prefer to use:

$fn="/full/path/of/dbmfile";
$db=dbmopen($fn,"r");
$row=dbmfetch($db,"MyKey");
dbmclose($db);