15

I make a site and it has this feature to upload a file and that file is uploaded to a server

Im just a newbie to php I download xampp and I run this site that i made in my local machine. My site is like this you upload a file then that file will be uploaded to a server, but when i tried unlink() because when i try to remove the filename to a database I also want to remove that pic on the server, but instead I got an error and it says "Permission denied".

question:
How can I got permission to use unlink();?

I only run this on my localmachine using xampp

user1628256
  • 319
  • 1
  • 3
  • 9

2 Answers2

13

Permission denied error happens because you're trying to delete a file without having enough/right permissions for doing that.

To do this you must be using superuser account or be the same user that have uploaded the file.

You can go to the directory from your command line and check the permissions that are set to the file.

The easiest solution is to loggin as administrator/root and delete the file.

Here is another work around:

// define if we under Windows
$tmp = dirname(__FILE__);
if (strpos($tmp, '/', 0)!==false) {
  define('WINDOWS_SERVER', false);
  } else {
  define('WINDOWS_SERVER', true);
}
  $deleteError = 0;
  if (!WINDOWS_SERVER) {
    if (!unlink($fileName)) {
      $deleteError = 1;
    }
  } else {
    $lines = array();
    exec("DEL /F/Q \"$fileName\"", $lines, $deleteError);
  }
  if ($deleteError) {
    echo 'file delete error';
  }


And some more: PHP Manual, unlink(), Post 106952

I would recommend, always first to check PHP Manual (in case your question concerns PHP), just go to the page with function that you have problems with and just click search CTRL+F in your browser and enter, for example, Windows, and as a result, in your case, you would find at least 7 related posts to that or very close to that what you were looking for.

hakre
  • 193,403
  • 52
  • 435
  • 836
Ilia Ross
  • 13,086
  • 11
  • 53
  • 88
4

Read this URL

How to use Unlink() function

I found this information in the comments of the function unlink()

Under Windows System and Apache, denied access to file is an usual error to unlink file. To delete file you must to change file's owern. An example:

<?php 

chown($TempDirectory."/".$FileName,666); //Insert an Invalid UserId to set to Nobody Owern; 666 is my standard for "Nobody" 
unlink($TempDirectory."/".$FileName); 

?>

So try something like this:

$Path = './doc/stuffs/sample.docx';

chown($Path, 666);

if ( unlink($Path) )
    echo "success";
else
    echo "fail";

EDIT 1

Try to use this in the path:

$Path = '.'.DIRECTORY_SEPARATOR.'doc'.DIRECTORY_SEPARATOR.'stuffs'.DIRECTORY_SEPARATOR.'sample.docx';
Community
  • 1
  • 1
Abid Hussain
  • 7,724
  • 3
  • 35
  • 53
  • Was going to down vote then saw [this](http://www.apachefriends.org/en/xampp-linux.html). XAMPP !== Windows – ficuscr Aug 27 '12 at 19:30
  • 2
    -1: `chown` to an invalid user is a strange way to attempt what is needed. The canonical solution is to use `chmod`. If you are logged into an account that has sufficient permission to change owner, then you are an administrator, and can unlink any file. If permissions interfere with the unlink, then change the read/write/execute **mode** of the file so that anyone can delete it: `chmod($Path, 0666);`. Note the leading `0`, since this is an octal value. Of course, you must be logged in as administrator to make such a change. – ToolmakerSteve Jul 16 '15 at 20:20