Programming Forums

Programming Forums (http://www.programmingforums.org/forumindex.php)
-   PHP (http://www.programmingforums.org/forum29.html)
-   -   rename() problem (http://www.programmingforums.org/showthread.php?t=13860)

grimpirate Aug 27th, 2007 10:38 PM

rename() problem
 
I'm using the rename function and its behavior seems to vary dependent on the server. On my home computer when I use rename() and the new filename already exists it throws an error. However, on the server if the new filename is the same as the old filename it just overwrites the old file. Any suggestions how to work around this or fix this?

DaWei Aug 27th, 2007 11:00 PM

It depends upon the OS. Same would happen if you were doing it from a command line.

grimpirate Aug 27th, 2007 11:23 PM

Yeah I read up on the PHP and apparently it only occurs on Windows systems where rename fails (I'm running XP). I was kind of banking on that feature. I'm thinking I can use an fopen() function with the 'x' parameter to create the file and then just add the data.

grimpirate Aug 28th, 2007 12:38 AM

That actually worked beautifully. Even better than the rename function because I don't have to create a temporary file beforehand. Unfortunately, it means I also had to increase the php version requirement of my script from 4.3.0 to 4.3.2. Such is technology.

tAK Aug 28th, 2007 10:55 PM

Alternatively, you could have done some checking first. (of course, you would probably check a lot more, so you could get the exact message required).

:

$oldname = "foo";
$newname = "bar";
If (file_exists("$oldname") AND !file_exists("$newname"))
{
  rename()
} Else {
  print "File/Folder not found, or new File/Folder name already exists";
}


grimpirate Aug 29th, 2007 12:56 AM

Yeah I actually thought about that tAK but the problem is that since PHP can run multiple requests at one time I'm not sure that doing that would be "thread safe." If two processes were running at that very same moment doing that procedure it's possible (though slim) that they could overlap and thus one file might overwrite the other. I figure that fopen is safer in its usage because its coded that way.

ReggaetonKing Aug 29th, 2007 8:12 AM

Is there a function modifier in PHP that makes a function thread-safe?

grimpirate Aug 29th, 2007 12:39 PM

Not that I know of, they have an flock function but it isn't respected in all cases. The parameter I'll be using in fopen is 'x' which creates a file but if it already exists it produces an error. I figure the code for one function has to finish executing before another function can execute so this is my safest bet.


All times are GMT -5. The time now is 12:42 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC