Jump to content

Create file named after variable


Tynen

Recommended Posts

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

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.

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.