Jump to content

whats wrong with this script (fopen,fwrite)


tommych

Recommended Posts

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?  :-\

[code]$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";
}

}[/code]

Thanks for looking at my code  ;)
Link to comment
https://forums.phpfreaks.com/topic/15232-whats-wrong-with-this-script-fopenfwrite/
Share on other sites

What does $uploaddir look like?

Try this:

[code=php:0]
<?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";

}

?>
[/code]

hth
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.

[code]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."/";
}[/code]

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.