Web Hosting Forum | Lunarpages


*
Welcome, Guest. Please login or register.
Did you miss your activation email?



Login with username, password and session length
April 16, 2014, 08:54:23 AM

Pages: [1]   Go Down
  Print  
Author Topic: Unlink() Not working  (Read 1959 times)
mschlauch
Space Explorer
***
Offline Offline

Posts: 7


« on: March 05, 2013, 01:24:37 PM »

Hello,

I've been trying for a full day now to figure out why this script isn't working. I've scoured the internet, and tried so many variations, changed permissions on files/folders manually, removed variables, tested variables, etc... why can't I get the unlink() function to delete the .jpg I want?

Here's the script:

<?php

if (file_exists('../prod_pics/image.jpg')) {

unlink('../prod_pics/image.jpg');
}

?>

The relative paths are correct when used in any other form, redirects, file uploads, etc...

Would appreciate it, tearing out what little hair I do have... thanks.
Logged
MrPhil
Senior Moderator
Berserker Poster
*****
Offline Offline

Posts: 5807



« Reply #1 on: March 05, 2013, 03:34:26 PM »

A couple of things to check:
1. What file and directory permissions? Both must be writable by PHP/owner (say, 644 and 755). AFAIK PHP runs as owner.
2. Any chance that another process or page or whatever is holding open the file? It won't go away until the last user releases it.
Logged

Visit My Site

E-mail Me
  
-= From the ashes shall rise a sooty tern =-
lexhair
Galactic Royalty
*****
Offline Offline

Posts: 427


« Reply #2 on: March 06, 2013, 07:31:05 AM »

Try not using the file_exists conditional. Suppress any errors by using the following code:

Code:
@unlink('../prod_pics/image.jpg');

If the file isn't there, the code will continue to run.
Logged
MrPhil
Senior Moderator
Berserker Poster
*****
Offline Offline

Posts: 5807



« Reply #3 on: March 06, 2013, 09:44:49 AM »

The problem is that unlink() isn't working in the first place, not that the code is crashing. Even with @, the file will still be there -- just any error message that the file wasn't there (or that something went wrong trying to delete it) will be suppressed.

I suspect that something has that file open while you're trying to delete it. Also confirm that PHP (probably = owner) can write to the file and to the directory.
Logged

Visit My Site

E-mail Me
  
-= From the ashes shall rise a sooty tern =-
lexhair
Galactic Royalty
*****
Offline Offline

Posts: 427


« Reply #4 on: March 06, 2013, 11:56:50 AM »

The problem is that unlink() isn't working in the first place, not that the code is crashing. Even with @, the file will still be there -- just any error message that the file wasn't there (or that something went wrong trying to delete it) will be suppressed.

I suspect that something has that file open while you're trying to delete it. Also confirm that PHP (probably = owner) can write to the file and to the directory.
Perhaps the conditional statement is holding the file in cache and won't release it to unlink until the conditional statement is done with it.
Logged
MrPhil
Senior Moderator
Berserker Poster
*****
Offline Offline

Posts: 5807



« Reply #5 on: March 06, 2013, 02:09:33 PM »

I've never heard of that happening, but I suppose it wouldn't hurt to try...
Logged

Visit My Site

E-mail Me
  
-= From the ashes shall rise a sooty tern =-
Pages: [1]   Go Up
  Print  
 
Jump to: