Jump to content

Archived

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

tommych

whats wrong with this script (fopen,fwrite)

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  ;)

Share this post


Link to post
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

Share this post


Link to post
Share on other sites
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/";

Share this post


Link to post
Share on other sites
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?

Share this post


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

Share this post


Link to post
Share on other sites
Have you got permissions in the directory /2006/07/21/ to create files?

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites

×

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.