Create file problem

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
rowlandk
Forum Newbie
Posts: 1
Joined: Mon May 24, 2004 10:42 am

Create file problem

Post by rowlandk »

I have a function that creates a directory and file if they do not already exist. However it always seems to return false. I till create the directory but not the file.

Here is my code. Any ideas?

Code: Select all

function makefile($mk_content, $mk_user_id, $mk_filename) {

	// Attemp to create the directory
	$filepath = "../users/" . $mk_user_id;
	if (! file_exists($filepath)) {
		$mkdirresult = mkdir($filepath, 0700);
	}

	// Convert filename to hex
	$mk_filename = bin2hex($mk_filename);

	// Attemp to create the file
	echo $filepath . "/" .$mk_filename . ".txt";
	if (! file_exists($filepath . "/" . $mk_filename . ".txt")) {
		$file = fopen($filepath . "/" . $mk_filename . ".txt", "w");
		fwrite($file, stripslashes($mk_content));
		fclose($file);
		return true;
	} else {
		return false;
	}


}
User avatar
delorian
Forum Contributor
Posts: 223
Joined: Sun May 04, 2003 5:20 pm
Location: Olsztyn, Poland

Post by delorian »

Is your www-server running as nobody? If so you should give the directory in which you are trying to create files write permission, the same with the directory you are creating in this script. e.g.:

Code: Select all

$mkdirresult = mkdir($filepath, 0707);
BTW: What for is the bin2hex function, it converts binary data into string, so if your filename is a string why do you want to convert it :?
Post Reply