#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2014
    Posts
    1
    Rep Power
    0

    Rename() not working sometimes and still returning true


    I am having a problem since I moved to a new server (with an upgraded PHP and Apache). The problem is rename(). I have code that creates a file and then writes into it and closes it. After a few seconds (user verifies values), it is renamed. The rename code looks like shown below. Sometimes it works and sometimes it does not. I see the "Done" message in all cases, and no error and no error_log created. If it fails, both renames fail and if it succeeds, both succeed. I never get it doing only one and yet it shows Done in all cases. I see the file I created (pre_specials.html) is there after the failure (but is not when it works since renamed). This exact same code worked on the older server with an older version of PHP. Any ideas what could be causing this?
    One clue: I think it relates to how long after the file was closed (and yes, I verified the close). Note that even if somehow related to the close, the rename of ../specials.html should have happened. So, it is all a big mystery to me. Thanks, paul

    if (is_file("pre_specials.html"))
    {
    if (rename("../specials.html", "specials.html.save") !== true)
    echo "Error: Cannot rename ../specials.html to .save<br>";
    if (rename("pre_specials.html", "../specials.html") !== true)
    echo "Error: Cannot rename pre_specials.html to ../specials.html<br>";
    echo "Done";
    }
    else
    echo "No file";
  2. #2
  3. --
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2012
    Posts
    3,959
    Rep Power
    1016
    The code doesn't make a lot of sense (apart from the fact that I can barely read it without [ PHP ] tags).

    First of all, the “Done” message is displayed in any case, because it's not bound to a condition. No matter how many errors you had, you always claim that everything was just fine.

    Secondly, you try the second statement in any case, even if it obviously depends on the first statement. This leads to weird messages and makes debugging difficult (as you can see).

    Get clear about the logic. What do you wanna do in which situation? Expressed as pseudo code, you'll probably want something like this:

    Code:
    if rename "../specials.html" to "specials.html.save" is successful:
    	// we can now rename the new file
    	if rename "pre_specials.html" to "../specials.html" is successful:
    		print "Done!"
    	else:
    		print "Could not rename."
    	end if.
    else:
    	print "Could not rename."
    end if.
    It's generally a good idea to express conditions in a positive way instead of using twisted “if not true then ...” statements.

    Oh, and it might be a good idea to always write if statements with curly braces. Apply has just demonstrated that.
    The 6 worst sins of securityHow to (properly) access a MySQL database with PHP

    Why can’t I use certain words like "drop" as part of my Security Question answers?
    There are certain words used by hackers to try to gain access to systems and manipulate data; therefore, the following words are restricted: "select," "delete," "update," "insert," "drop" and "null".
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2014
    Posts
    1
    Rep Power
    0

    Your response seems to be focused on syntax and not on semantics - why?


    I did not see the code tag scheme. So the indents disappeared. But, your entire response seems to be focused on syntax style and not on the problem.
    Done will NOT be displayed in "any case", only when the file exists, which was the point. Otherwise, people would say maybe the file does not exist. It does.
    I tried the !== true when "!rename()" did not work nor did "rename() or" thinking maybe PHP has some new problem around logic tests. The point is that rename is not returning an error and yet it is not renaming. I disagree that you keep it "positive". I am testing for errors - nothing positive about an error. if (!rename(...)) show_error(); is positive since the natural flow is that the show_error never happens.

    I also had the two renames dependent but split them to see what was happening. Your comment does not help in explaining why the rename() does not work some of the time. That IS the problem.

    The point is that I can run this script multiple times and it will work after a couple tries. There is no explanation as to why it fails when it does as it does not think it failed nor why it works when it does. It just does not rename. I can echo the filenames and they are correct each time.

    I know EXACTLY what I want to do. The code should be pretty obvious as to what I want I to. If the file exists, I want to rename it to another file after renaming that file to a save name. This is simple. I have been coding (HW and SW) for more than 30 years. This is not user error but something going on with PHP or the server. As I said, this code has been in use for 4 years with no problems until now. What changed is the version of PHP so I suspect it has some bug. Thanks, paul

    Originally Posted by Jacques1
    The code doesn't make a lot of sense (apart from the fact that I can barely read it without [ PHP ] tags).


    Get clear about the logic. What do you wanna do in which situation? Expressed as pseudo code, you'll probably want something like this:

    Code:
    if rename "../specials.html" to "specials.html.save" is successful:
    	// we can now rename the new file
    	if rename "pre_specials.html" to "../specials.html" is successful:
    		print "Done!"
    	else:
    		print "Could not rename."
    	end if.
    else:
    	print "Could not rename."
    end if.
    It's generally a good idea to express conditions in a positive way instead of using twisted “if not true then ...” statements.

    Oh, and it might be a good idea to always write if statements with curly braces. Apply has just demonstrated that.
  6. #4
  7. --
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2012
    Posts
    3,959
    Rep Power
    1016
    *sigh*

    I wonder why you know-it-alls even come here to ask us. If you've already decided that your code is perfect and that it's PHP which is at fault, what do you expect to hear from us?

    Write an entry in the PHP bug tracker or whatever.

    Comments on this post

    • kicken agrees
    Last edited by Jacques1; February 25th, 2014 at 06:30 PM.
    The 6 worst sins of securityHow to (properly) access a MySQL database with PHP

    Why can’t I use certain words like "drop" as part of my Security Question answers?
    There are certain words used by hackers to try to gain access to systems and manipulate data; therefore, the following words are restricted: "select," "delete," "update," "insert," "drop" and "null".
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2006
    Posts
    2,616
    Rep Power
    1766
    Originally Posted by pkimelma
    <...>
    I know EXACTLY what I want to do. The code should be pretty obvious as to what I want I to. If the file exists, I want to rename it to another file after renaming that file to a save name. This is simple. I have been coding (HW and SW) for more than 30 years. This is not user error but something going on with PHP or the server. As I said, this code has been in use for 4 years with no problems until now. What changed is the version of PHP so I suspect it has some bug. Thanks, paul
    As Jacques said, you will either get the Done output, or the No File. In addiotn, before the Done is shown you may get one or both Cannot rename messages.
    His suggestion about the php formatter is valid, but sadly I do not think it would have helped you out here. An if statement will execute either the command following it (note the singular!) if true or the command under an else (if present and the conditon 'fails'). Your opening if has code encased in { } which php takes at 'the command'. None of your other if statements do. That means that once it is determined the initial file exists then three commands WILL be executed: two if statements and the echo of Done.
    The moon on the one hand, the dawn on the other:
    The moon is my sister, the dawn is my brother.
    The moon on my left and the dawn on my right.
    My brother, good morning: my sister, good night.
    -- Hilaire Belloc

IMN logo majestic logo threadwatch logo seochat tools logo