tommych Posted July 21, 2006 Share Posted July 21, 2006 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 More sharing options...
chrisprse Posted July 21, 2006 Share Posted July 21, 2006 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 Link to comment https://forums.phpfreaks.com/topic/15232-whats-wrong-with-this-script-fopenfwrite/#findComment-61563 Share on other sites More sharing options...
tommych Posted July 21, 2006 Author Share Posted July 21, 2006 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/"; Link to comment https://forums.phpfreaks.com/topic/15232-whats-wrong-with-this-script-fopenfwrite/#findComment-61566 Share on other sites More sharing options...
chrisprse Posted July 21, 2006 Share Posted July 21, 2006 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? Link to comment https://forums.phpfreaks.com/topic/15232-whats-wrong-with-this-script-fopenfwrite/#findComment-61570 Share on other sites More sharing options...
tommych Posted July 21, 2006 Author Share Posted July 21, 2006 the directory(ies) are checked and created before hand. And yes i just want to create the file with $data in it. Link to comment https://forums.phpfreaks.com/topic/15232-whats-wrong-with-this-script-fopenfwrite/#findComment-61575 Share on other sites More sharing options...
chrisprse Posted July 21, 2006 Share Posted July 21, 2006 Have you got permissions in the directory /2006/07/21/ to create files? Link to comment https://forums.phpfreaks.com/topic/15232-whats-wrong-with-this-script-fopenfwrite/#findComment-61576 Share on other sites More sharing options...
tommych Posted July 21, 2006 Author Share Posted July 21, 2006 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 monthif (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] Link to comment https://forums.phpfreaks.com/topic/15232-whats-wrong-with-this-script-fopenfwrite/#findComment-61578 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.