Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

mkdir() in php code

Status
Not open for further replies.

petersJazz

Programmer
Jan 28, 2002
222
EU
hi,

on the web hotel I have my sites I can not create subdirectories from php.

mkdir('../xxx');
chmod('../xxx', 0777);

works good but

mkdir('../xxx/php');

failes. I dont understand why?

Peter
 
assuming you are using php5 you need to set the recursive flag. also advisable to set the umask

Code:
$um = umask(0);
mkdir('../xxx/php', 0777, true);
umask($um);

if you are using php4 or below then you will not have access to the recursive function. here is a small piece of code. i have not tested it and am not confident of it working well on windows paths that have the drive prepended, but otherwise it should be ok....

Code:
<?php
function mkdir_php4_recursive($folder){
	//reset the umask
	$um = umask(0);
	//change folder separators to unix types
	$folder = str_replace('\\', '/', $folder);
	//explode the path
	$_folder = explode('/', $folder);
	//create holding variable for iterative path
	$path = '';
	foreach ($_folder as $f){
		if (empty($f)) continue;
		$path .= $f;
		//see whether directory already exists
		if(!is_dir($path)){
			if (!mkdir($path, 0777)){	//note the prepended zero to force octal
				die ("cannot create directory");
			}
			//add a directory separator for the next iteration
			$path .= DIRECTORY_SEPARATOR;
		}
	}
	umask($um);
}
?>
 
Its php4 and it works only if I create ../xxx in web hotel file interface but not when I try to create ../xxx in php and after that ../xxx/php. I will ask the web hotel about it, it must be something with access rights. The directory has different owners when created in file interface/ftp vs php.

Peter
 
the second function will work with php4.

time to start looking for a php5 host. php4 end of life has been announced.
 
hi,

there is something strange about this. If the directory is created with php (and user/group httpd) I can not write to it, not even from php? Php code with copy dont work and fopen etc dont work. There must be some general security setting for php that limits the way it works.

Peter
 
there might well be security settings that interact but you have not given us any information that allows us to help you.

i provided code by way of a php4 compatible function. if you run it, what error messages do you receive?

if the folders are created, what permissions do they have?

personally, i have run into php related security issues before with a host, and they are so reluctant to make changes that, in the end, I wrote a script to log into the same server and same directory by ftp and create the directory that way (with which method the host had correctly set the permissions).
 
I dont work. There must be some security setting in PHP that dont allow these kind of commands. I can not write files to the newly created directories and copy() dont work.

regards
Peter
 
what are the permissions of the newly created directories?
 
ok, thank you for or help, here are some more details, I have isolated the problem into simpel code.

Code:
 // 1 - mkdir dont work
 $um = umask(0);
 if (!mkdir('testdir', 0777)) echo('Cannot create directory testdir');
 if (!mkdir('testdir/subdir', 0777)) echo('Cannot create directory testdir/subdir');
 umask($um);

 // 2 - copy dont work
 if (!copy('db.txt','testdir/db.txt')) echo('Cannot copy file to testdir');
 if (!copy('db.txt','testdir/subdir/db.txt')) echo('Cannot copy file to testdir/subdir');

 // 3 - write file dont work
 $f=fopen('testdir/xxx.txt','a');
 fwrite($f,'Text to put in file testdir/xxx.txt');
 fclose($f);
 $f=fopen('testdir/subdir/xxx.txt','a');
 fwrite($f,'Text to put in file testdir/subdir/xxx.txt');
 fclose($f);

Root directory (/ is owned by petersja, same group and mask 777. This is what I get:

Cannot create directory testdir/subdirCannot copy file to testdirCannot copy file to testdir/subdir

There is a directory testdir created with user/group httpd and mask 777. But no subdirectory and no file copied or written.

regards
Peter
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top