Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP unlink error - path error

Tags:

php

unlink

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.

like image 745
user2060451 Avatar asked Aug 13 '26 00:08

user2060451


1 Answers

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"

like image 86
Aristona Avatar answered Aug 17 '26 13:08

Aristona