Jump to content


Photo

whats wrong with this script (fopen,fwrite)


  • Please log in to reply
6 replies to this topic

#1 tommych

tommych
  • New Members
  • Pip
  • Newbie
  • 5 posts

Posted 21 July 2006 - 08:26 AM

Some how my script won't write $data into a file.

The file doesn't exist, that why i chose "w" because it should make the file if it doesn't exist, but it doesn't  :(. Anyone got any ideas?  :-\

$filename = $uploaddir."logs.sdf";

	if (is_writable($filename)) {
	
		if (!$handle = fopen($filename, 'w')) {
			 echo "Cannot open file ($filename)";
			 exit;
		}
	
		if (fwrite($handle, $data) == FALSE) {
		
		echo "Success, wrote ($data) to file ($filename)";
		
		fclose($handle);
		$link = mysql_connect('localhost', $user, $password)
			or die('Could not connect: ' . mysql_error());
		echo 'Connected successfully';
		mysql_select_db($dbu) or die('Could not select database');
		$datelog = $mday.".".$mmonth.".".$myear;
		// Performing SQL query	
		$query = "INSERT INTO `users` (`id`, `user`, `datelog`, `dateupload`, `Time`, `IP`) VALUES (NULL, '".$name."', '".$datelog."',".date(" 'd.m.Y'").", CURDATE(), '".$_SERVER['REMOTE_ADDR']."' );";
		$result = mysql_query($query) or die('Query failed: ' . mysql_error());
		
		// Free resultset
		mysql_free_result($result);
		
		// Closing connection
		mysql_close($link);
	
		} else {
			echo "The file $filename is not writable";
		}

	}

Thanks for looking at my code  ;)

#2 chrisprse

chrisprse
  • Members
  • PipPipPip
  • Advanced Member
  • 39 posts
  • LocationUnited Kingdom

Posted 21 July 2006 - 09:48 AM

What does $uploaddir look like?

Try this:

<?php

$filename = $uploaddir . "logs.sdf";

if(is_writable($filename)) {

	if(!$handle = fopen($filename, 'w')) {

		echo "Cannot open file ($filename)";
		exit;

	}

	if(fwrite($handle, $data) === FALSE) {

		echo "Cannot write to file ($filename)";
		exit;

	}

	echo "Success, wrote ($data) to file ($filename)";

	fclose($handle);

	$link = mysql_connect("localhost", $user,  $password);
		mysql_select_db($dbu, $link);

	$datelog = $mday . "." . $mmonth . "." . $myear;

	$result = mysql_query("insert into `users` (`id`,`user`,`datelog`,`dateupload`,`Time`,`IP`)
				values (NULL,'$name','$datelog','".date("d.m.Y")."', CURDATE(), '".$_SERVER['REMOTE_ADDR']."')");
		mysql_free_result($result);

	mysql_close($link);

}

else {

	echo "The file is not writeable";

}

?>

hth

#3 tommych

tommych
  • New Members
  • Pip
  • Newbie
  • 5 posts

Posted 21 July 2006 - 09:59 AM

umm still no luck. did you just change "=== False" ?

$uploaddir = "/home/protoan/public_html/test/$year/$month/$day/";

e.g

$uploaddir = "/home/protoan/public_html/test/2006/07/21/";

#4 chrisprse

chrisprse
  • Members
  • PipPipPip
  • Advanced Member
  • 39 posts
  • LocationUnited Kingdom

Posted 21 July 2006 - 10:07 AM

No not just === FALSE was changed!

Does the whole directory already exist, you just want it to create the file?

Or are you wanting it to create the directory? E.g. /2006/07/21 as well?

#5 tommych

tommych
  • New Members
  • Pip
  • Newbie
  • 5 posts

Posted 21 July 2006 - 10:13 AM

the directory(ies) are checked and created before hand. And yes i just want to create the file with $data in it.

#6 chrisprse

chrisprse
  • Members
  • PipPipPip
  • Advanced Member
  • 39 posts
  • LocationUnited Kingdom

Posted 21 July 2006 - 10:15 AM

Have you got permissions in the directory /2006/07/21/ to create files?

#7 tommych

tommych
  • New Members
  • Pip
  • Newbie
  • 5 posts

Posted 21 July 2006 - 10:22 AM

this is the folder can't be found this script makes the folders with CHMOD 0777 (allows everything).
is really anoying, it doesn't even make the file :(

and all the variables check out.

if (is_dir($uploaddir.$myear) == TRUE) {
		// Check month
if (is_dir($uploaddir.$myear."/".$mmonth) == TRUE) {
			// Check day
			if (is_dir($uploaddir.$myear."/".$mmonth."/".$mday) == TRUE) {
				// if day exists use random name to make dir
				mkdir($uploaddir.$myear."/".$mmonth."/".$mday."/".$randomnr, 0777);
				$uploaddir = $uploaddir.$myear."/".$mmonth."/".$mday."/".$randomnr."/";
			} else {
				// day folder doesn't exist, make it
				mkdir($uploaddir.$myear."/".$mmonth."/".$mday, 0777);
				$uploaddir = $uploaddir.$myear."/".$mmonth."/".$mday."/";
			}
		} else {
			// month folder doesn't exist, make month folder and day folder
			mkdir($uploaddir.$myear."/".$mmonth, 0777);
			mkdir($uploaddir.$myear."/".$mmonth."/".$mday, 0777);
			$uploaddir = $uploaddir.$myear."/".$mmonth."/".$mday."/";
		}
	} else {
		// year folder doesn't exist, make year folder and month folder and day folder
		mkdir($uploaddir.$myear, 0777);
		mkdir($uploaddir.$myear."/".$mmonth, 0777);
		mkdir($uploaddir.$myear."/".$mmonth."/".$mday, 0777);
		$uploaddir = $uploaddir.$myear."/".$mmonth."/".$mday."/";	
	}





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users