Tynen Posted January 13, 2010 Share Posted January 13, 2010 I'm trying to backup a .txt file and it's contents to another .txt file named after a user's ip address and time. I've written this little bit of code to create a file named after ip/time but it gets stuck. I can't figure out why! <?php // get ip $ip=$_SERVER['REMOTE_ADDR']; // set time zone to pacific time zone date_default_timezone_set('PST'); // j=day of month, M=Jan, Y=2010, G= 24-hour 0-23, i=minutes, s=seconds $time=date("G:i:s M-j, Y"); $backup = "$ip-$time"; $backup .= ".txt"; $fh = fopen("$backup", "x") or die('Could not create backup file!'); fwrite($fh, '') or die('Could not write to backup file!'); ?> Link to comment https://forums.phpfreaks.com/topic/188303-create-file-named-after-variable/ Share on other sites More sharing options...
Catfish Posted January 13, 2010 Share Posted January 13, 2010 What error message is it throwing to you? you could try: $backup = $ip."-".$time.".txt"; It could also be a file permissions problem and not related to your code. Link to comment https://forums.phpfreaks.com/topic/188303-create-file-named-after-variable/#findComment-994081 Share on other sites More sharing options...
Tynen Posted January 13, 2010 Author Share Posted January 13, 2010 I found the problem! It was this line: $time=date("G:i:s M-j, Y"); It's because of the :, file names in windows cannot contain the : symbol.. doh! I figured this out because I tried to create a file named what the variable would be at the point in the script where it names the file, and I get a warning in explorer telling me there were characters not allowed in the name. Link to comment https://forums.phpfreaks.com/topic/188303-create-file-named-after-variable/#findComment-994393 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.