423,680 Members | 2,439 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 423,680 IT Pros & Developers. It's quick & easy.

unlink() returns TRUE but does not delete

code green
Expert 100+
P: 1,726
I have this piece of code to tidy up after a function that calls move_uploaded_file()
Expand|Select|Wrap|Line Numbers
  1. if(file_exists($destination.$filename))
  2. {
  3.     $exitmsg .= 
  4.         "<br>File still in temporary location
  5.         <br>$filename
  6.         <br>Attempting deletion ";
  7.     if(unlink($destination.$filename))
  8.         $exitmsg .= '<br>Success deleting. Please try again ';
  9.     else
  10.         $exitmsg .= '<br>Failed deleting. Please inform IT';
  11.     }
Basically I am attempting to delete a file from a
move_uploaded_file() 'destination' so that the file can upload. This piece of code returns TRUE but the file is still there
Expand|Select|Wrap|Line Numbers
  1. 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.
May 19 '09 #1
Share this Question
Share on Google+
9 Replies


Markus
Expert 5K+
P: 6,050
Strange. You sure you've got permissions to delete it?

Furthermore, move_uploaded_file() will overwrite (I believe) the file if it already exists.
May 19 '09 #2

prabirchoudhury
100+
P: 162
you may set up directory permission, before calling unlink().

Expand|Select|Wrap|Line Numbers
  1. chmod("$destination", 0777); 
  2.  
  3.  
May 20 '09 #3

code green
Expert 100+
P: 1,726
Markus
move_upload_file() 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
May 20 '09 #4

P: 88
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_file() 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.
May 20 '09 #5

code green
Expert 100+
P: 1,726
Tried prabirchoudhury's suggestion. Wow that is hard to spell!
And did
Expand|Select|Wrap|Line Numbers
  1. chmod($destination, 0777); 
I was previously using
Expand|Select|Wrap|Line Numbers
  1. 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.
May 21 '09 #6

prabirchoudhury
100+
P: 162
that sound great .. you could read more abt php chmod
May 22 '09 #7

Markus
Expert 5K+
P: 6,050
PHP Manual: read it.


;)
May 22 '09 #8

code green
Expert 100+
P: 1,726
I don't see the relevance of this page http://php.net/ read it
May 26 '09 #9

Markus
Expert 5K+
P: 6,050
@code green
Nevermind. T'were a joke.
May 26 '09 #10

Post your reply

Sign in to post your reply or Sign up for a free account.