unlink() returns TRUE but does not delete

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • code green
    Recognized Expert Top Contributor
    • Mar 2007
    • 1726

    unlink() returns TRUE but does not delete

    I have this piece of code to tidy up after a function that calls move_uploaded_f ile()
    Code:
    if(file_exists($destination.$filename))
    {
    	$exitmsg .= 
    		"<br>File still in temporary location
    		<br>$filename
    		<br>Attempting deletion ";
    	if(unlink($destination.$filename))
    		$exitmsg .= '<br>Success deleting. Please try again ';
    	else
    		$exitmsg .= '<br>Failed deleting. Please inform IT';
    	}
    Basically I am attempting to delete a file from a
    move_uploaded_f ile() 'destination' so that the file can upload. This piece of code returns TRUE but the file is still there
    Code:
    if(unlink($destination.$filename))
    I am running Apache Windows with IE which I suspect is the issue.
    Or am I completely mis-understanding unlink()?
    By the way it is a CSV file.
  • Markus
    Recognized Expert Expert
    • Jun 2007
    • 6092

    #2
    Strange. You sure you've got permissions to delete it?

    Furthermore, move_uploaded_f ile() will overwrite (I believe) the file if it already exists.

    Comment

    • prabirchoudhury
      New Member
      • May 2009
      • 162

      #3
      unlink() returns TRUE but does not delete

      you may set up directory permission, before calling unlink().

      Code:
      chmod("$destination", 0777);

      Comment

      • code green
        Recognized Expert Top Contributor
        • Mar 2007
        • 1726

        #4
        Markus
        move_upload_fil e() is called in a static class method.
        This method checks if the file is already there before moving.
        It then warns the user and handles the warning cleanly.

        It is after this method that I want to delete the file so the user
        can try again if they are happy.

        I am not getting a permissions warning as expected but will try changing.
        The strange thing is, if I refresh the page the file is then deleted.
        And I am physically checking the directory.

        The only things I can think of are a possible racing issue or
        unlink() does not work well in Windows

        Comment

        • hoopy
          New Member
          • Feb 2009
          • 88

          #5
          I have had no problems with using unlink() with WAMP so it could be an issue with Apache I guess and its permissions. move_uploaded_f ile() does indeed overwrite any file of the same name. Perhaps its a problem with the class have you tried a simple test to remove the same file like:

          echo unlink("Some Path Name");

          And see if it returns true or false?

          Its odd that it works on a refresh but not before.

          Comment

          • code green
            Recognized Expert Top Contributor
            • Mar 2007
            • 1726

            #6
            Tried prabirchoudhury 's suggestion. Wow that is hard to spell!
            And did
            Code:
            chmod($destination, 0777);
            I was previously using
            Code:
            chmod($destination, 0666);
            No idea what the difference is.
            Also re-jigged the code so that the user can take a simpler course of action,
            so it could be that I have inadvertently 'stepped over' the the problem.
            Seems to be working now anyway, thanks for all the help.

            Comment

            • prabirchoudhury
              New Member
              • May 2009
              • 162

              #7
              that sound great .. you could read more abt php chmod
              Last edited by Atli; May 22 '09, 07:14 AM. Reason: Removed the nz2 fromt he link... makes it a lot faster for those of us not from New Zealand ;)

              Comment

              • Markus
                Recognized Expert Expert
                • Jun 2007
                • 6092

                #8
                PHP Manual: read it.


                ;)

                Comment

                • code green
                  Recognized Expert Top Contributor
                  • Mar 2007
                  • 1726

                  #9
                  I don't see the relevance of this page http://php.net/ read it

                  Comment

                  • Markus
                    Recognized Expert Expert
                    • Jun 2007
                    • 6092

                    #10
                    Originally posted by code green
                    I don't see the relevance of this page http://php.net/ read it
                    Nevermind. T'were a joke.

                    Comment

                    Working...