-2

I've this piece of code to rename a file by adding the uploading date, but then I want also to remove all spaces

rename("./uploads/" . "$directory/" . $mvcfile->FileName , "./uploads/" . "$directory/" . $data . "_" . $mvcfile->FileName);

I've tried with

str_replace (" ", "", $mvcfile->FileName);

but it doesn't work.

Any idea of why? I'm pretty new to PHP: what's "->"? Could be this the hitch? Thanks

Miwi
  • 774
  • 4
  • 15
  • 28
  • 2
    `->` is the object operator. It denotes an object property or method. In this case, `FileName` is a property of the object instance `$mvcfile`. See [this guide](http://stackoverflow.com/questions/3737139/reference-what-does-this-symbol-mean-in-php) – Michael Berkowski Mar 07 '12 at 13:29
  • 1
    When are you calling the `str_replace()`? You should call it before `rename()`. Your syntax is correct, and it should work.. – Michael Berkowski Mar 07 '12 at 13:30

3 Answers3

0

str_replace returns the stripped string - it does not work directly on parameter 3 (since the function signature of str_replace does not list any parameter as a reference)

Robert
  • 878
  • 6
  • 14
0

Something like this could work:

$data = time(); // gets the unix timestamp
rename("./uploads/$directory/" . $mvcfile->FileName , "./uploads/$directory/" . $data . "_" . str_replace(" ","",$mvcfile->FileName));

That's assuming what's in $mvcfile->FileName is just the filename, not a path.

enygma
  • 684
  • 4
  • 6
0

It should be :

$mvcfile->FileName=str_replace(" ", "", $mvcfile->FileName);

str_replace() returns a string , so you should initialize your variable to it again

Shai
  • 111,146
  • 38
  • 238
  • 371
Emad Samir Zaki
  • 393
  • 2
  • 10