-3

I have a file in a directory name.processing and I want rename this file in name.processed

I have this code:

$fn1 = str_replace(".processed", ".processing", $fn);
rename($fn1,$fn);

$fn contain the complete path.

Why the file did not got rename in directory?

I think this is not a duplicate because i know how replace a part of string but i don't know how replace an extension of file in directory

mydb
  • 45
  • 1
  • 9

2 Answers2

2

PHP is not always predictable in parameter sequence, you should consult the documentation.

// processing -> processed
// str_replace($search, $replace, $haystack)
$fn_new = str_replace(".processing", ".processed", $fn);
// rename($oldname, $newname)
$rename = rename($fn,$fn_new);
echo "old: $fn, fn_new: $fn_new, rename: ", $rename ? 'success' : 'failure';

http://php.net/manual/en/function.str-replace.php http://php.net/manual/en/function.rename.php


Edit: If it still doesn't work: https://stackoverflow.com/search?q=php+rename

Community
  • 1
  • 1
PaulH
  • 2,918
  • 2
  • 15
  • 31
1

Looks like you have the paramters for str_replace backwards.http://php.net/manual/en/function.str-replace.php

Try $fn1 = str_replace(".processing", ".processed", $fn);

Michael Hommé
  • 1,696
  • 1
  • 14
  • 18