Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
321 views
in Technique[技术] by (71.8m points)

php - How to get permission to use unlink()?

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...