views:

87

answers:

5

I am currently working on this project which requires me to make a function which dinamically decides the directory name and then creates a simple .txt file in that directory.

my code is as follows: ($destinatario is a string)

     $diretorio="../messages/".$destinatario;
if (is_dir($diretorio)) {
    ;
}else{
    mkdir($diretorio);
}
$path=$diretorio."/".$data.",".$assunto.",".$remetente.".txt";
$handle=fopen($path,'w+');

fwrite($handle, $corpo);

fclose($handle);

nevermind the portuguese, but the bottom line is that it should create a .txt file using the naming guidelines i've provided. The funny thing is that when i do this, php creates this weird file whose filename is "01.09.2010 04" (with no extension at all) which amounts to the first few characters of the actual filename i'd like to create...

edit($data is actually the output from a call to date("d.m.Y H:i"))

Been all over the web and found nothing, please help

thanks in advance from Brazil

A: 

Not related, but make sure your if statements don't have unused conditions:

if (!is_dir($diretorio)) {
    mkdir($diretorio);
}

This will also get rid of that blank line with a single terminator ;, I'm sure that isn't right.

ILMV
A: 

Some ideas:

  • have you tried not using commas in the filename?
  • Have you checked the return value if fopen and fwrite?

Just to try to isolate the problem

also you can simplify to:

if (!is_dir($diretorio)) {
  mkdir($diretorio);
}
sewa
+1  A: 

Make sure there's no bad characters at the end of $data. Call trim() on it.

If it's data taken from a file, it may have a '\r' or '\n' at the end of it.

it´s actually the output of a call to date("d.m.Y H:i")
Felipe Almeida
@Felipe Almeida Then the `:` is the problem.
jensgram
@jensgram Good call, : is illegal on Windows (but not linux). It's surprising that fopen() just chops your path at the illegal character instead of returning an error. Or is that a PHP-ism?
Yeah!!!It was the colon!!!
Felipe Almeida
THANK YOU guys !!
Felipe Almeida
@tchen That's definitely a stupid behavior (by PHP) to just hide the error. I'm not sure whether this is "to be expected" :-S
jensgram
+3  A: 

Per comment by OP:

[$data is] actually the output of a call to date("d.m.Y H:i")

The problem is the : character. (Still, there may be other illegal characters in the other parts composing the final file name.)


EDIT
The essence of the problem and solution is in the comments to @tchen's answer. Keep in mind that colon is a valid file name character on (some? all?) *nix platforms but is invalid on Windows.

jensgram
+1 That make really sense. You may want to filter characters out a filename too, using a whitelist (remove unless it is listed)
Lekensteyn
@Lekensteyn Indeed. Perhaps even the same strategy as applied when making titles "URL friendly", see e.g. http://stackoverflow.com/search?q=php+url+replace
jensgram
A: 

THANKS everyone for their input!

The problem was with the colon (:) in the middle of the filename

Apparently u can't use it.

Thanks everyone

Felipe Almeida