Forum Moderators: coopster

Message Too Old, No Replies

File Rename on Windows Server

php rename() file or equivalent on Windows Server?

         

andy_broom

2:14 pm on May 8, 2009 (gmt 0)

10+ Year Member



Hi, has anyone else had problems trying to rename a file on a windows server?

I am simply trying to change the name of an existing file using the rename() function but it just doesn't seem to work, does anyone have any suggestions that might help?

here is what I am using:

$flgRename = rename("pages/gps.php", "pages/new.php")
if($flgRename)
{
echo "success!";
}
else
{
echo "failed";
}

I just get 'failed' and no name change.

Cheers,

Andy

coopster

2:36 pm on May 8, 2009 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Have you checked the error logs to see if any errors were generated? I'm guessing perhaps it is a permissions issue.

andy_broom

2:44 pm on May 8, 2009 (gmt 0)

10+ Year Member



I tried changing all the permissions but still got nothing.

The file I am trying to rename was created with fopen("name", "w+"); which worked fine.

There were no error logs. I have a feeling it has something to do with it being on a Windows server, but not sure.

coopster

3:18 pm on May 8, 2009 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



I have never attempted to rename without the full file path. Have you tried that?

andy_broom

3:26 pm on May 8, 2009 (gmt 0)

10+ Year Member



Funny you should suggest that, I just thought the same and tried it but still no joy.

Just found this on php.net, "rename() fails with PHP4 and PHP5 under Windows if the destination file exists, regardless of file permission settings."
-http://us.php.net/manual/en/function.rename.php#60341

Tried out some other suggestions on there too but still getting nothing.

coopster

3:43 pm on May 8, 2009 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Yes, that is true because you cannot rename a file to an existing file name. You need to check for it's existence first and if you truly don't want the existing one, you will need to unlink it first.
if (file_exists("pages/new.php")) { 
unlink("pages/new.php");
}
rename("pages/gps.php", "pages/new.php");

I just tested this on a Windows install and it worked just fine as is running PHP 5.2.9-2

andy_broom

3:48 pm on May 8, 2009 (gmt 0)

10+ Year Member



Thanks, I'll give it a go. If I unlink the file first and then do rename() what happens to the content of the original file? I need the newly named file to have the exact same content as the old file.

coopster

3:53 pm on May 8, 2009 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Then you should be using copy(), not rename.

andy_broom

4:07 pm on May 8, 2009 (gmt 0)

10+ Year Member



Brilliant... I'm not absolutly sure yet but it looks like that is going to work! Obviously I will then have to unlink the old file.

I'm going to give it a go over the weekend and fingers crossed it will all work.

Thanks very much for your help and have a good weekend.