3

I am using same syntax as everyone else but... i can't get it to work.

I have tried quite a few options to unlink file from directory.

Summary: delete.php (the file that executes the action) is in the main folder. The file (image) to be deleted is under the sub-directory "upload".

$file = $name . '.' . $ext; 

$tmpfile = 'upload/'. $file; 
unlink($tmpfile);

Than this one too.

if (!unlink("upload/$file")) {
    echo "Error deleting ... $file  ... from directory";
}
else {
    echo "Deleted $file";
}

And many more. Here is the error log

unlink(upload/Glauber_3232_MAGNOLIA_ST__016.jpg) [function.unlink]: No such file or directory in /home2/braaasil/public_html/openhouse/delete.php on line 30

Therefore, I am not being able to exit the main directory (where delete.php) is and go to subdirectory "upload" where my images are. Any suggestion will be greatly appreciated.

EDIT

public_html

   openhouse (sub domain)

      delete.php (this is a file)

      upload (this is sub folder)

If this is not clear I can take a picture. delete.php and subfolder upload are on the same level, both kids of openhouse. Hope this is clear.

4
  • Are you sure that file /home2/braaasil/public_html/openhouse/upload/Glauber_3232_MAGNOLIA_ST__016.jpg exists? Note that file names are case sensitive. Commented Sep 25, 2013 at 20:38
  • Did you try "../upload/$file" ? Commented Sep 25, 2013 at 20:41
  • 1
    Not saying this is your problem, but you should always use relative paths based on the current directory: __DIR__ . '/upload/' . $file. Because sometimes the current working dir is not what you think it is. Commented Sep 25, 2013 at 20:43
  • I tried "../upload/$file" The problem is that it is trying to find the file inside the delete.php per error message as ../delete.php/my_image_to_be_delete.jpeg Commented Sep 25, 2013 at 20:46

2 Answers 2

4

Your script has no issues but I believe you're giving the wrong path to your application, so it is unable to find specified file under upload folder.

I think you're using a subfolder, but upload points to root folder, hence it can't find the folder.

Try: unlink (__DIR__ . '/upload/' . $file);

If it works, you can also use it as: "./upload/$file"

Sign up to request clarification or add additional context in comments.

12 Comments

@PeeHaa; You probably edited my comment while I was editing it myself.
@Imaqtpie this is now wrong again - __DIR__ doesn't have a trailing slash.
@GeorgeBrighton Sorry, it was accidently overwritten. I've added it.
I am new to PHP so pardon my ignorance. I used your script as it there. No change. Got this again. "No such file or directory in /home2/braaasil/public_html/openhouse/delete.php" for some reason i can't get this: home2/braaasil/public_html/openhouse/upload/ (then the image to be delete)"
Yes, it is a subfolder. delete.php is in the main folder. Images are in the sulfolder called "upload'.
|
0

I am sorry ! For some reason, DB started to accept both image.jpg and image.JPG even though I had the command strtolower for all entries.

Images are being deleted by the very code is posted in this question. I really appreciate your time and apologize one more time.

For all others: make sure that DB, file and search all all set to lower case since DB may be upper and lower case sensitive.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.